2013-03-13 49 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; 
    } 
相關問題