views:

64

answers:

2

Hi, everyone,

I am writing the iPhone application and I would like to ask about the the passing parameters in objective C.

I create 2 view controller. In the first one, I have a button, when a user press the button, it will call the -(IBAction) pressButton (user-defined), and after 5-6 second (have to process and retrieve the data in the NSMutableArray *), it will display a table. However, I don't know how to pass the NSMutableArray to the table class. Can I do this?

// situation
// ---------------------------------------------
// In MyViewController.m

// class variable
NSMutableArray * arr;
- (IBAction) pressButton: (id)sender {...}
// I retrieve the data and store in the arr

// In TableView.m
// I want to pass the arr to here and use

I know how to create the table from this website, but I don't know how to pass the parameters from a class (MyViewController.m) to another class (TableView.m). Could any one help me? Thank you very much.

+1  A: 
  1. In TableView.h, declare a method:

    - (void)doWhateverWithArray: (NSArray *)anArray;
    
  2. In TableView.m, implement the method to do whatever you need it to do.

  3. In MyViewController.m, near the top of the file (outside of @implementation ... @end) write #import "TableView.h"

  4. Send -doWhateverWithArray: to your table view when necessary.

Function calls are a fundamental part of procedural programming languages like C; message dispatches (AKA method calls) are a fundamental part of object-oriented programming languages like Objective-C.

The nature of this question suggests you're just starting to get into programming (if I'm wrong, please don't take this as an insult--it's not.) I'm sure folks can recommend any number of introductory texts to C or Objective-C, but I'd go further if I were in your shoes.

If available to you, I recommend that you take college-level programming courses, or even enroll in a computer science degree at the university level if you have the time and dedication. :)

Jonathan Grynspan
(I'd also suggest you look into learning C fully before starting on Objective-C.)
chpwn
Learning C fully? Knowledge of C asymptotically approaches 1. ;)
Jonathan Grynspan
Agree with @chpwn. The question hints at a lack of understanding about the fundamentals of object oriented programming.
Dave DeLong
@Dave DeLong: Not like C is going to help there, though ;).
chpwn
Thank you for your reply. I want to ask how to call another function in another class? I have already import the .h and define the header in the tableView.h. When I call the method in the MyViewController class, it have some error. Would you mind to show the calling statement to me?
Questions
Well, what's the error?
Jonathan Grynspan
A: 

In your TableView ViewController create NSMutableArray like this

IBOutlet NSMutableArray  *PassedArray;
@property(nonatomic,retain) NSMutableArray  *PassedArray;
@synthesize PassedArray;
[PassedArray release];

Now you can pass NSMutableArray value from your Myviewcontroller to tableviewcontroller using this

tableviewcontroller *objInstance = [[tableviewcontroller alloc]initWithNibName:@"tableviewcontroller" bundle:nil];
objInstance.PassedArray = NSMutableArray;//Here you declare your passed NSMutableArray
[self.navigationController pushViewController:objInstance animated:YES];
[objInstance release];

Another method is Declare NSMutableArray *yourArray is global in MyViewController.h and extern those variables in tableviewcontroller.m like this

extern NSMutableArray *yourArray;
dragon