gpt4 book ai didi

ios - NavigationController 中的 ModalViewCOntroller

转载 作者:行者123 更新时间:2023-11-29 05:03:32 25 4
gpt4 key购买 nike

我有一个 NavigationController,它呈现一个带有按钮的 View (ShoppingController),我将其称为 ModalViewController :

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

当我想要将数据从模态视图交换到他的父 View 时,出现错误,因为 [selfparentViewController] 引用的是我的 NavigationController 而不是我的 ShoppingController。

如何将数据从 ModalView AddProductController 发送到调用者 ShoppingController ?

最佳答案

您可以使用委托(delegate)模式。

在您的 AddProductController 类中,当处理按钮点击时,您可以向其委托(delegate)发送一条消息,您将其设置为 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: 成为协议(protocol)的一部分。然后,您的 ShoppingController 可以实现该协议(protocol),而不是使用 [self.delegate respondsToSelector:@selector(x)] 检查,而是检查 [self.delegateconfesToProtocol:@protocol(y )]

关于ios - NavigationController 中的 ModalViewCOntroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6259505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com