2011-05-30 26 views
0

我有一個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。

回答

2

視圖控制器已創建,但其視圖(即其筆尖)尚未加載,因此屬性尚未連接。您可以通過訪問控制器的視圖成員來強制筆尖加載:

- (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]; 
} 
+0

這樣做了!現在,如果我將它添加到我的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

+0

我不確定你想要完成什麼。也許你可以發佈更多的代碼。 – Hollance 2011-05-31 12:18:13