【问题标题】:send a message to a parent object form popover向父对象表单弹出框发送消息
【发布时间】:2012-02-19 22:32:32
【问题描述】:

我的 iOS 应用程序使用故事板

它有 2 个视图控制器: - 主故事板视图控制器 - 和弹出视图控制器,其中包含一些对象

我在主视图控制器上有一个按钮,它会在我每次运行应用程序时创建程序:

*CGRect buttonFrame = CGRectMake(10., 10., 120., 50.);

oneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[oneButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"someImage.png", img]] forState:UIControlStateNormal];
[oneButton setTag:img];
[oneButton setFrame:buttonFrame];
[oneButton addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:oneButton];*

此按钮的操作会像这样显示我的弹出视图:

*- (void) pressButton:(id)sender {

popoverViewController *popoverFrame = [self.storyboard instantiateViewControllerWithIdentifier:@"myPopoverView"];

popoverWithObjects = [[UIPopoverController alloc] initWithContentViewController:popoverFrame];

[popoverWithObjects presentPopoverFromRect:[sender frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];

}*

从现在开始,我无法向我的按钮发送任何消息或结果。 我想对我的程序按钮说(注意 - 我只有这个按钮的发送者 action:@selector(pressButton:) )弹出框返回一些结果或弹出框中对象的一些动作发送任何东西(例如字符串) 或者换句话说,当我与弹出视图上的按钮等任何对象交互时,我想更改父按钮标题标签

【问题讨论】:

    标签: ios uibutton storyboard parent popover


    【解决方案1】:
    【解决方案2】:

    您需要使用委托。从SO 中查看我对这个类似问题的回答。

    编辑:链接到故事板上的tutorial 和委托模式使用。我对这个SO的原始回答@

    【讨论】:

    • 你知道,我试过了。我无法在情节提要应用程序中进行委派...无论如何感谢您的回答。对于链接,它真的很有帮助
    • 如何为父对象进行委托(在我的例子中是按钮)
    • 我认为您对 appDelegate 和其他类的委托模式感到困惑。虽然 appDelegate 在 Storyboard 下不可用,但使用委托模式进行回调仍然是有效的技术。
    • 查看您的代码,我猜“popoverViewController”或“myPopoverView”是您的子视图控制器。你的主视图控制器(你有按钮的地方)是你的父视图控制器。有了这两条信息,您应该能够遵循其他提到的 SO 答案中的模式。我刚刚还编辑了上面的答案,在 Storyboard 上添加了另一个教程链接,该链接说明了如何使用委托模式。
    • @peter,有关设置委托和委托协议的详细说明,请参阅我的答案 - stackoverflow.com/questions/9043801/…
    【解决方案3】:

    在你的主视图控制器上写一个函数来做你想做的事情怎么样。然后从弹出窗口调用该函数? (即使用'prepare for segue'向弹出框发送主视图控制器的ID,然后使用该ID从弹出框调用主视图上的函数)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      相关资源
      最近更新 更多