当我尝试在prepareForSegue中设置此变量的值时,为什么我的应用程序崩溃了?

时间:2013-03-13 22:01:17

标签: ios objective-c ios5 ios6 segue

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

用户通过单击“读取”进行分段,将其分割。

这是我的prepareForSegue方法:

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

    destination.textToRead = self.textInput.text;
}

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

被调整的窗口被设置为不同的视图控制器(ReadingViewController),我通过控制从“读取”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) 

3 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您似乎在导航控制器上设置了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;
   }