how do I pass data to a child UINavigationController which is presented modally (i.e. via initWithRootViewController)
How do I pass data to a child UINavigationController which is presented modally via "[[UINavigationController alloc] initWithRootViewController:newItemController];"?
That is the way with this method of creating the child controller (i.e. newItemController in this case), it is initialised via the UINavigationController initWithRootViewController method, hence there doesn't seem to be the ability to call a custom newItemController init method here? Nor have access to the newItemController instance itself to call a custom "setMyData" type method?
NewItemController *newItemController = [NewItemController alloc];
newItemController.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newItemController];开发者_如何转开发
[self.navigationController presentModalViewController:navController animated:YES];
Code in your question is missing the init called to NewItemController. For example:
NewItemController *newItemController = [[NewItemController alloc] init];
Now, when you created NewItemController you can create your own init:
-(id)initWithStuff:(NSString *)example {
self = [super init];
if (self) {
// do something with the example data
}
return self;
}
or you can add property to the NewItemController class
// header file
@property (nonatomic, copy) NSString *example;
// .m file
@synthesize example;
// when you create the object
NewItemController *item = [[NewItemController alloc] init];
item.example = @"example string data";
The key is that you don't pass the data to the navigation controller, you pass it to the navigation controller's root view controller.
精彩评论