NavigationController 中的 ModalViewCOntroller

发布于 2024-11-13 11:33:49 字数 493 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻许诺言 2024-11-20 11:33:49

您可以使用委托模式。

在您的 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.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:

-(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];
}

Then, in ShoppingController (and don't forget to release 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
}

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)].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文