如何在不使用segues和NaviagationController的情况下在两个ViewController之间传递数据

时间:2013-05-03 07:10:51

标签: iphone ios objective-c viewcontroller

我的问题是当FirstViewController中的实例SecondViewController为变量ID赋值时,问题是我无法使用Segue和NaviagtioController .. 也许解决方案使用静态变量?,怎么做,或者告诉我一个提示?

我这样做代码:

// 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] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'UICollectionView必须使用非零布局参数进行初始化'   * 第一次抛出调用堆栈:   (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调用抛出异常   (lldb)

2 个答案:

答案 0 :(得分:3)

试试这个

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

希望我帮助

答案 1 :(得分:3)

错误原因非常清楚终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:'UICollectionView必须使用非零布局参数初始化'

您的ViewControllerOneUICollectionViewController子类。它应该使用非零布局参数进行初始化。

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

但是在视图控制器之间传递数据仍然存在。