UISwitch返回NULL?
问题描述:
我有一个UISwitch,由于某种原因返回(null)
。下面是我的代码:UISwitch返回NULL?
AddAlbumViewController:
// .h
IBOutlet UISwitch *photostreamSwitch;
@property (nonatomic, retain) IBOutlet UISwitch *photostreamSwitch;
// .m
@synthesize photostreamSwitch;
photostreamSwitch = [[UISwitch alloc] init];
NSLog(@"photostreamSwitch: %@", photostreamSwitch); // returns a not-null value
SecondViewController:
//.m
- (IBAction)createAlbum:(id)sender {
AddAlbumViewController *addAlbumViewController = [[AddAlbumViewController alloc] initWithNibName:@"AddAlbum" bundle:[NSBundle mainBundle]];
NSLog(@"Test Switch: %@",addAlbumViewController.photostreamSwitch); // returns null
[addAlbumViewController release];
}
我想我拥有了一切树立正确的。如果这有帮助,AddAlbumViewController位于UINavigationController中,SecondViewController包含UINavigationController。
答
视图控制器已创建,但其视图(即其笔尖)尚未加载,因此属性尚未连接。您可以通过访问控制器的视图成员来强制笔尖加载:
- (IBAction)createAlbum:(id)sender {
AddAlbumViewController *addAlbumViewController = [[AddAlbumViewController alloc] initWithNibName:@"AddAlbum" bundle:[NSBundle mainBundle]];
UIView* tempView = addAlbumViewController.view;
NSLog(@"Test Switch: %@",addAlbumViewController.photostreamSwitch); // no longer null
[addAlbumViewController release];
}
这样做了!现在,如果我将它添加到我的viewDidLoad中,它会正确检测。 (ISON)。但如果我把它放在我的cellForRowAtIndexPath中,它不会。这里是我在我的cellForRowAtIndexPath中的代码:'photostreamSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199,8,0,0)]; [cell.contentView addSubview:photostreamSwitch]; [photostreamSwitch setOn:YES animated:NO];'谢谢。 – iosfreak 2011-05-30 21:04:05
我不确定你想要完成什么。也许你可以发布更多的代码。 – Hollance 2011-05-31 12:18:13