【问题标题】:Why is my app crashing when I try to set the value of this variable in prepareForSegue?当我尝试在 prepareForSegue 中设置此变量的值时,为什么我的应用程序会崩溃?
【发布时间】:2013-03-13 22:01:16
【问题描述】:

我正在尝试做的事情的要点是将窗口上的标签值设置为用户在前一个窗口中输入的文本。

用户通过单击“读取”来进行转义,这会将他们转义。

这是我的 prepareForSegue 方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ReadingViewController *destination = segue.destinationViewController;

    destination.textToRead = self.textInput.text;
}

textToRead 只是一个 NSString 对象,它保存用户输入的文本。 (然后使用viewDidLoad 方法将此文本设置为标签。)

segue 的窗口按预期设置为不同的视图控制器 (ReadingViewController),我通过从“Read”UIButton 控制拖动到下一个窗口来创建 segue。

我似乎无法弄清楚问题所在。这是它给出的错误:

2013-03-13 19:00:08.119 Project Lego[1523:c07] -[UINavigationController setTextToRead:]: unrecognized selector sent to instance 0x894d230
2013-03-13 19:00:08.122 Project Lego[1523:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setTextToRead:]: unrecognized selector sent to instance 0x894d230'
*** First throw call stack:
(0x1c92012 0x10cfe7e 0x1d1d4bd 0x1c81bbc 0x1c8194e 0x2949 0x45bb87 0x45bc14 0x10e3705 0x172c0 0x17258 0xd8021 0xd857f 0xd76e8 0x46cef 0x46f02 0x24d4a 0x16698 0x1beddf9 0x1bedad0 0x1c07bf5 0x1c07962 0x1c38bb6 0x1c37f44 0x1c37e1b 0x1bec7e3 0x1bec668 0x13ffc 0x244d 0x2375 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

【问题讨论】:

    标签: ios objective-c ios5 ios6 segue


    【解决方案1】:

    如果您想访问嵌入在 navigationVC 中的视图控制器,请执行以下操作:

     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
        {
            UINavigationController *nav = (UINavigationController*)segue.destinationViewController;
            ReadingViewController *destination = [nav.viewControllers objectAtIndex:0];
            destination.textToRead = self.textInput.text;
         }
    

    【讨论】:

    • 漂亮的答案,完全符合我的要求,谢谢。 (不过我可能会补充一点,UINavigationController 是您要查找的课程。)
    • 哈哈,是的,哇哦,我没有演员表就搞定了。。我的错。我的错,袖口打字
    【解决方案2】:

    您的 segue 引用的是 UINavigationController,而不是您的 viewcontroller 类。检查您的 Storyboard 以确保 segue 连接了正确的控制器。

    【讨论】:

    • 你说得对,确实如此。只是,我想让它嵌入到导航控制器中,这样它就可以有一个导航栏来放置我的取消按钮(这样我就可以关闭模​​式视图)。有没有办法将它嵌入到导航控制器中并且仍然可以进行这项工作?
    【解决方案3】:

    您似乎正在导航控制器上设置 textToRead。我假设你想在视图控制器中设置它。如果是这样,你需要

        if ([segue.destinationViewController 
                     isKindOfClass:[UINavigationController class]])
        {
           mvc = (ReadingViewController *) [segue.destinationViewController topViewController];
           mvc.textToRead = self.textInput.text; 
    
        } else {
    
           ReadingViewController *destination = segue.destinationViewController;
           destination.textToRead = self.textInput.text;
       }
    

    【讨论】:

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