2014-12-18 42 views
1

我想從viewController A設置一個NSString到viewController B.我嘗試使用下面的代碼,但它不工作。準備for segue不讓我設置下一個viewController的NSString

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"segueSubCat"]) 
    { 
     NSIndexPath *indexPath = [tableViewCat indexPathForSelectedRow]; 

     ViewControllerSubCat *subCatVC = [segue destinationViewController]; 
     subCatVC.cellNameCat = [categories objectAtIndex:indexPath.row]; 
     subCatVC.navigationItem.title = subCatVC.cellNameCat; 
    } 
} 

我試過了,每次去第二個視圖時,應用程序都會崩潰。所以我用下面的代碼替換它:

wat = @"wat"; 
[[segue destinationViewController] setContentsName:wat]; 

它做了同樣的事情。即它崩潰了。 我不知道我在做什麼錯。

注意:當我拿走prepareForSegue中的代碼時,它就起作用了。

錯誤消息

2014-12-18 15:36:16.382 myApp[1503:42438] -[UINavigationController setCellNameCat:]: unrecognized selector sent to instance 0x7fb9714e7240 
2014-12-18 15:36:16.388 myApp[1503:42438] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setCellNameCat:]: unrecognized selector sent to instance 0x7fb9714e7240' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010094cf35 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x00000001005e5bb7 objc_exception_throw + 45 
    2 CoreFoundation      0x000000010095404d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x00000001008ac27c ___forwarding___ + 988 
    4 CoreFoundation      0x00000001008abe18 _CF_forwarding_prep_0 + 120 
    5 myApp  0x00000001000b3887 -[ViewController prepareForSegue:sender:] + 343 
    6 UIKit        0x000000010128b71c -[UIStoryboardSegueTemplate _perform:] + 151 
    7 UIKit        0x0000000100e21360 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1242 
    8 UIKit        0x0000000100e214d4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219 
    9 UIKit        0x0000000100d5c331 _applyBlockToCFArrayCopiedToStack + 314 
    10 UIKit        0x0000000100d5c1ab _afterCACommitHandler + 516 
    11 CoreFoundation      0x0000000100881dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    12 CoreFoundation      0x0000000100881d20 __CFRunLoopDoObservers + 368 
    13 CoreFoundation      0x0000000100877b53 __CFRunLoopRun + 1123 
    14 CoreFoundation      0x0000000100877486 CFRunLoopRunSpecific + 470 
    15 GraphicsServices     0x0000000103f1b9f0 GSEventRunModal + 161 
    16 UIKit        0x0000000100d39420 UIApplicationMain + 1282 
    17 myApp  0x00000001000b4523 main + 115 
    18 libdyld.dylib      0x0000000102edc145 start + 1 
    19 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 
+2

什麼是崩潰消息? – Paulw11 2014-12-18 23:30:49

+0

給出崩潰錯誤消息。 – kRiZ 2014-12-18 23:37:59

回答

1

首先檢查是否分配了UIViewController類在故事板ViewControllerSubCat

編輯:

從你的崩潰日誌很顯然,你得到一個UINavigationController作爲[segue destinationViewController];結果。因此,更改代碼如下:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"segueSubCat"]) 
    { 
     NSIndexPath *indexPath = [tableViewCat indexPathForSelectedRow]; 
     UINavigationController *navController = [segue destinationViewController]; 
     ViewControllerSubCat *subCatVC = (ViewControllerSubCat *)navController.topViewController; 
     subCatVC.cellNameCat = [categories objectAtIndex:indexPath.row]; 
     subCatVC.navigationItem.title = subCatVC.cellNameCat; 
    } 
} 
+0

這不起作用 – Horray 2014-12-18 23:42:57

+0

@Mike:你在那裏得到一個導航控制器,所以你需要使用UINavigationController * navController = [segue destinationViewController]; ViewControllerSubCat * subCatVC =(ViewControllerSubCat *)navController.topViewController; – 2014-12-18 23:45:52

+0

它工作! :)你能解釋一下爲什麼嗎? – Horray 2014-12-18 23:57:04

0

正如其他海報所說,目標視圖控制器不是您的自定義類。這聽起來像你已經整理出來了。

你可能要面對的另一個問題:

在prepareForSegue被調用的時候,目的地VC的視圖層次結構還沒有被加載。

這條線:

subCatVC.navigationItem.title = subCatVC.cellNameCat; 

可能不會工作,因爲我懷疑你的新的風險投資navigationItem已經被加載。免責聲明:我從來沒有嘗試在prepareForSegue方法中操作目標視圖控制器的navigationItem,所以我不確定它是否會被定義,但我強烈懷疑不是。爲了測試這個,在prepareForSegue中記錄subCatVC.navigationIte的值(一旦你理清了指向正確的VC的指針)

+0

我不是%100肯定你說什麼,但如果我是正確的,我想你想說當我從viewController A傳遞一個項目到viewController B,並且我將它傳遞給navBar的標題時,我認爲你說這不起作用? – Horray 2014-12-19 01:06:44

+0

正確。這就是我所說的。 – 2014-12-19 01:21:38

+0

它爲我工作。 – Horray 2014-12-19 01:25:32

相關問題