i have a DetailViewController and a Messages class(this is a TableViewController class). I parse some web information in the first class and want to use some values amongst them in the second class. As i looked arround in here and google for a few haurs and by now i think i need to define some extern variables in my second class and initialize them with the objects of the first class.. I tried a few ways but all failed.
In my first clas i have an NSMutableArray variable called messID, in the second class i do this:
#import DetailViewController
.
.
extern NSMutableArray *myArray;
DetailViewController *myObject;
myArray=myObject.messID;
But i got the error below:
Undefined symbols for architecture i386:
"_myArray", referenced from:
-[messages tableView:cellForRowAtIndexPath:] in messages.o
What am i doing wrong and what can i do please can any one help?..
EDIT
I’ve imported needed class,
in the .h file of second class i use this:
@interface messages : UITableViewController{
BNT_1DetailViewController *myObject;
}
@property(retain, nonatomic)BNT_1DetailViewController *myObject;
And its .m file is:
…
@sysnthesize myObject;
viewDidLoad{
myObject=[[BNT_1DetailViewController alloc]init];}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return myObject.mesID.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//extern NSMutableArray *messID;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}NSLog(@" -> %@",myObject.mesID );
cell.textLabel.text= [myObject.mesID objectAtIndex:indexPath.row];
// Configure the cell...
// [tableView reloadData];
return cell;
}
I would not use
externfor what you are trying to do, which means you are also defining some global variables (and trying to access them from the files where you use theexternkeyword). For the records, anyway, the error you are getting depends on the fact thatexternis just a directive that does not define your objects — it simply declares them as defined elsewhere. So you should add in some .m file the proper definition for your objects, without forgetting to also initialize them at some point in time so that they point to meaningful objects.A better way to do what you are trying to do is through public properties declared in your first class allowing access to the
NSMutableArrayfrom the second class.An example of this could be:
This solution is better because it does not use global variables and has a better encapsulation.
Actually, you could try and define a Model (like in model-view-controller) which contains all of your data and have them shared among all the controllers that need it. This would be an better approach.
EDIT: on how to connect one controller to another…
Say that at some point you create a new controller:
Now,
detailViewControllerneeds retrieving its data frommessages, which is by the way the controller which is creating it. You have several options for doing that. An easy one is having a public property inmessageDetailthat links tomessages:(don’t forget to synthesize this property in
messageDetailimplementation).Then, when you instantiate
messageDetail, you simply do this:Once you do this, your
detailViewControllerwill have a pointer, correctly initialized, to point to the other controller.If your other controller exposes (like I explained above) a property with the NSArray, your are done.