您可以使用委托模式。
在您的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)]
。
完美! Thx nekno。 :-) – DjLeChuck 2011-06-07 11:45:37