我有一个NavigationController,它呈现一个带有按钮的视图(ShoppingController),我称之为ModalViewController:
AddProductController *maView = [[AddProductController alloc] init];
maView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:maView animated:YES];当我想将数据从我的模式视图交换到他的父级时,我会出错,因为self parentViewController引用的是我的NavigationController,而不是我的ShoppingController。
如何将数据从我的ModalView AddProductController发送到我的呼叫者ShoppingController?
发布于 2011-06-07 08:57:50
您可以使用委托模式。
在您的AddProductController类中,当处理按钮点击时,您可以向它的委托发送一条消息,并将其设置为您的ShoppingController。
因此,在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];
}然后,在ShoppingController中(别忘了发布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
}如果你想变得更花哨,你可以把receiveData:作为协议的一部分。然后,您的ShoppingController可以实现该协议,而不是使用[self.delegate respondsToSelector:@selector(x)]进行检查,而是检查该[self.delegate conformsToProtocol:@protocol(y)]。
https://stackoverflow.com/questions/6259505
复制相似问题