ModalViewCOntroller in NavigationController
I have a NavigationController that present a view (ShoppingController) with a button which one I call a ModalViewController :
Add开发者_如何学CProductController *maView = [[AddProductController alloc] init];
maView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:maView animated:YES];
When I want exchange data from my modal view to his parent, I have an error because [self parentViewController] refer to my NavigationController and not my ShoppingController.
How can I send data from my ModalView AddProductController to my caller ShoppingController ?
You could use the delegate pattern.
In your AddProductController class, when handling the button tap, you can then send a message to its delegate, which you set as your ShoppingController.
So, in AddProductController:
-(void)buttonHandler:(id)sender {
// after doing some stuff and handling the button tap, i check to see if i have a delegate.
// if i have a delegate, then check if it responds to a particular selector, and if so, call the selector and send some data
// the "someData" object is the data you want to pass to the caller/delegate
if (self.delegate && [self.delegate respondsToSelector:@selector(receiveData:)])
[self.delegate performSelector:@selector(receiveData:) withObject:someData];
}
Then, in ShoppingController (and don't forget to release maView):
-(void)someMethod {
AddProductController *maView = [[AddProductController alloc] init];
maView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
maView.delegate = self;
[self presentModalViewController:maView animated:YES];
[maView release];
}
-(void)receiveData:(id)someData {
// do something with someData passed from AddProductController
}
If you want to get fancy, you can make receiveData: part of a protocol. Then, your ShoppingController can implement the protocol, and instead of checking with [self.delegate respondsToSelector:@selector(x)]
, you check that [self.delegate conformsToProtocol:@protocol(y)]
.
精彩评论