首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NavigationController中的ModalViewCOntroller

NavigationController中的ModalViewCOntroller
EN

Stack Overflow用户
提问于 2011-06-07 08:24:38
回答 1查看 331关注 0票数 0

我有一个NavigationController,它呈现一个带有按钮的视图(ShoppingController),我称之为ModalViewController:

代码语言:javascript
复制
    AddProductController *maView = [[AddProductController alloc] init];
maView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:maView animated:YES];

当我想将数据从我的模式视图交换到他的父级时,我会出错,因为self parentViewController引用的是我的NavigationController,而不是我的ShoppingController。

如何将数据从我的ModalView AddProductController发送到我的呼叫者ShoppingController?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-07 08:57:50

您可以使用委托模式。

在您的AddProductController类中,当处理按钮点击时,您可以向它的委托发送一条消息,并将其设置为您的ShoppingController。

因此,在AddProductController中:

代码语言:javascript
复制
-(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):

代码语言:javascript
复制
-(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)]

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6259505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档