【问题标题】:ModalViewCOntroller in NavigationControllerNavigationController 中的 ModalViewCONtroller
【发布时间】:2011-09-09 17:04:43
【问题描述】:

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

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

当我想从我的模态视图交换数据到他的父级时,我遇到了一个错误,因为 [self parentViewController] 指的是我的 NavigationController 而不是我的 ShoppingController。

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

【问题讨论】:

    标签: ios objective-c iphone uinavigationcontroller modalviewcontroller


    【解决方案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)]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多