2013-05-03 15 views
1

我的問題是當FirstViewController中的實例SecondViewController爲變量ID分配一個值時,問題是我無法使用Segue和NaviagtioController .. 也許解決方案使用靜態變量?,怎麼做,或者告訴我一個小費?如何在不使用segues和NaviagationController的情況下在兩個ViewControllers之間傳遞數據

我做這樣的代碼:

// In the class FirstViewController.m 
#import "SecondViewController" 

NSString *identifier = [NSString stringWithFormat:@"%@", [self.cat objectAtIndex:indexPath.row]]; 
// Start the code causes an error 
ViewControllerOne *instance = [[ViewControllerOne alloc]init]; 
instance.ID = [[NSString alloc] initWithFormat:@"%@",identifier]; 
// End code causes error 

// In the SecondViewController.h  
@property (nonatomic, retain) NSString *ID; 

// In the SecondViewController.m 
@synthesize ID; 

調試錯誤:

2013-05-03 09:01:42.126 TheFreeApp[19880:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' * First throw call stack: (0x1cbc012 0x10f9e7e 0x1cbbdeb 0x53d748 0x558b1b 0x558646 0x11eff8 0x11f232 0x5588d5 0xbf5b 0x11ce1e 0xb74e 0xed285 0xed4ed 0xaf75b3 0x1c7b376 0x1c7ae06 0x1c62a82 0x1c61f44 0x1c61e1b 0x1c167e3 0x1c16668 0x3dffc 0x2bcd 0x2af5) libc++abi.dylib: terminate called throwing an exception (lldb)

回答

3

試試這個

[[NSUserDefaults standardUserDefaults]setValue:yourVal forKey:@"myValue"]; 
NSString *myVal = [[NSUserDefaults standardUserDefaults]valueForKey:@"myValue"]; 

希望我幫助

3

錯誤的原因是很清楚的從日誌終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因是:

ViewControllerOneUICollectionViewController子類的UICollectionView必須以非空佈局參數進行初始化「。它應該用非零布局參數進行初始化。

UICollectionViewLayout *layout= [UICollectionViewFlowLayout new]; 
ViewControllerOne *instance = [[ViewControllerOne alloc]initWithCollectionViewLayout:layout]; 

但是,在視圖控制器之間傳遞數據仍然存在。

+0

我強烈懷疑,你的意思是'SecondViewController'代替'ViewControllerOne'嗎?你如何從第一個VC到第二個VC? – 2013-05-03 07:55:53

相關問題