How to pass the parameters from .m to another .m in Objective-C
I am writing the iPhone application and I would like to ask about the the passing para开发者_StackOverflowmeters 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, but I don't know how to pass the parameters from a class (MyViewController.m) to another class (TableView.m).
In TableView.h, declare a method:
- (void)doWhateverWithArray: (NSArray *)anArray;
In TableView.m, implement the method to do whatever you need it to do.
In MyViewController.m, near the top of the file (outside of
@implementation
...@end
) write#import "TableView.h"
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. :)
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;
精彩评论