2013-03-13 48 views
2

我想要做的事情的要点是在窗口上设置一个标签的值,以使用户在前一个窗口中输入的文本。为什么当我尝试在prepareForSegue中设置这个变量的值时,我的应用崩溃了?

用户通过点击“阅读”进入游戏,游戏进行。

这里是我的prepareForSegue方法:

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

    destination.textToRead = self.textInput.text; 
} 

textToRead只是一个NSString对象认为,用户输入的文本。 (这个文本然后被设置为viewDidLoad方法的标签。)

被寻找的窗口被设置为不同的视图控制器(ReadingViewController),并且我通过控件从“Read “UIButton到下一个窗口。

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

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) 

回答

4

,做这样的事情:

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

美丽的答案,工作正是我想要的,谢谢。 (不过,我可能会补充说'UINavigationController'是你正在寻找的类。) – 2013-03-13 22:25:32

+1

哈哈是啊woops我没有cast.ill修正了..我的不错,打字的袖口 – 2013-03-13 22:26:04

1

你赛格瑞被引用UINavigationController,而不是你的ViewController类。检查您的Storyboard以确保Segue连接正确的控制器。

+0

你说得对,这是它确实如此。只有我想将它嵌入到导航控制器中,以便它可以有一个导航栏来放置我的取消按钮(这样我可以关闭模​​态视图)。有没有办法将它嵌入到导航控制器中,仍然有这项工作? – 2013-03-13 22:17:26

1

您似乎在导航控制器上设置了textToRead。我假设你想在视图控制器中设置它。如果如果你要访问嵌入navigationVC视图控制器,所以你需要

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; 
    } 
相关问题