0

我有一个NavigationController即呈现视图(ShoppingController)有一个按钮,一个我称之为ModalViewController:ModalViewCOntroller在NavigationController

AddProductController *maView = [[AddProductController alloc] init]; 
maView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
[self presentModalViewController:maView animated:YES]; 

当我想从我的模式以他的父母交换数据,我有一个错误,因为[self parentViewController]引用我的NavigationController而不是我的ShoppingController。

如何将数据从我的ModalView AddProductController发送到我的调用方ShoppingController?

回答

1

您可以使用委托模式。

在您的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)]

+0

完美! Thx nekno。 :-) – DjLeChuck 2011-06-07 11:45:37