NavigationController 中的 ModalViewCOntroller
我有一个 NavigationController,它呈现一个带有按钮的视图(ShoppingController),我将其称为 ModalViewController :
AddProductController *maView = [[AddProductController alloc] init];
maView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:maView animated:YES];
当我想要将数据从我的模态视图交换到他的父级时,我遇到错误,因为 [selfparentViewController] 引用我的 NavigationController 而不是我的购物控制器。
如何将数据从 ModalView AddProductController 发送到调用者 ShoppingController ?
I have a NavigationController that present a view (ShoppingController) with a button which one I call a ModalViewController :
AddProductController *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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用委托模式。
在您的 AddProductController 类中,当处理按钮点击时,您可以向其委托发送一条消息,您将其设置为 ShoppingController。
因此,在 AddProductController 中:
然后,在 ShoppingController 中(并且不要忘记释放 maView):
如果您想变得更奇特,您可以使 receiveData: 成为协议的一部分。然后,您的 ShoppingController 可以实现该协议,而不是使用
[self.delegate respondsToSelector:@selector(x)]
检查,而是检查[self.delegateconfesToProtocol:@protocol(y )]
。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:
Then, in ShoppingController (and don't forget to release maView):
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)]
.