ios - 通过导航 Controller 时丢失指向委托(delegate)的指针

标签 ios delegates uinavigationcontroller segue popover

我不完全确定如何解释我的问题,但我会尝试。

所以我一直在尝试学习如何使用委托(delegate)和协议(protocol),但遇到了问题。我有一个协议(protocol):ButtonInPopOverWasPressed,有一种方法:

   - (void)buttonWasPressed:(NSString *)buttonValue;

我还有一个主视图 Controller 和一个具有以下属性的自定义弹出框类:

   @property (retain, nonatomic) id <ButtonInPopoverWasPressed> delegate;

在我的主视图 Controller 中,我有一个按钮和一个文本标签。当按下按钮时,它会转到正常的弹出窗口。然后,我将 segue.destinationViewController 的委托(delegate)分配为主视图 Controller ,如下所示:

   [segue.destinationViewController setDelegate:self];    

然后,当选择弹出窗口中的按钮时,通过调用主视图监听的协议(protocol)方法,它将显示在主视图的文本标签中:

   [self.delegate buttonWasPressed:sender.currentTitle]; // sends the title of the button pressed to the delegate

这一切都运行良好。当我想在按下按钮时将弹出窗口转换到不同的 View 而不是将信息发送回主视图时,我的问题就开始了。但是,当我创建一个导航 Controller 作为弹出窗口并将与旧弹出窗口的关系设置为所有内容时,一切都崩溃了。

当我分配segue的委托(delegate)(如上所示)时,它在另一侧显示为空,因此我完全失去了将其传递给其他 subview 并将信息返回主视图的能力。导航 Controller 会吸收委托(delegate)吗?如何让委托(delegate)通过 NC 获得实际 View ?

我知道这是协议(protocol)和委托(delegate)基础知识,但我环顾四周,似乎永远找不到对我有用的答案。

最佳答案

尝试以下代码。

    NSArray *temp = [[segue destinationViewController] childViewControllers];
    PopoverViewController *popoverViewController = (PopoverViewController)[temp objectAtIndex:0];
    popoverViewController.delegate = self;

关于ios - 通过导航 Controller 时丢失指向委托(delegate)的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11250385/

相关文章:

ios - "cannot convert value of type"- Firebase Swift 中的错误

ios - 一个带有两个导航栏的 Controller

ios - 我在使用 swift 包管理器时遇到问题

ios - 无法使 UiTableView 工作

python - 线程和委托(delegate) - 类的多线程

ios - 使用全局 Tint 颜色作为 UINavigationBars backgroundColor

objective-c - 在我可以使用我的方法本地 NSOpenSavePanelDelegate 之前,ARC 会杀死它。有解决办法吗?

c# - 为什么这个 OrderBy 参数是错误的?

ios - 推送 UIViewController 时实例成员不能在类型上使用

swift - 使导航栏透明后如何将其更改回半透明