我在 xcode 6 中创建了一个包含 xib 文件的新 UIViewController 类。 我这样设置一切
ViewController 下的.h 文件
@interface NextViewController : UIViewController
@property (nonatomic,assign) int gameID;
@end
ViewController 下的.m 文件
@synthesize gameID;
.m 文件之前的 View Controller
NextViewController *nextViewController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
nextViewController.gameID = -1;
[self.navigationController pushViewController:nextViewController animated:YES];
当我通过调试器运行它时,gameID
设置在 .h 文件中,但是当我想访问 viewDidLoad
方法中的值时,我得到了 0
代表 gameID
而不是 -1
最佳答案
您的代码看起来非常好,它应该按预期工作,也就是应该在 -(void) viewDidLoad
中显示“-1”。
我能想到的另一种方法是尝试重写 -init
方法,如下所示:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withGameId:(int)gameIdParam {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.gameID = gameIdParam;
}
return self;
}
在 .h
文件中声明此方法,然后使用此方法创建 NextViewController
的新实例,并将 gameID
作为参数传递。
请注意,您的代码本身是正确的。我在 Mac 上试过了,它运行良好。
关于ios - 无法将值传递给下一个 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27451536/