2011-05-31 16 views
0

我啓動我的cellForRowAtIndexPath。它出現了,但是當我嘗試從不同的類中引用它時,它會返回(null)。如果我在viewDidLoad中啓動它,它可以工作,但它需要在UITableView中。在cellForRowAtIndexPath中啓動時無法獲取UISwitch的狀態?

AddAlbumViewController:

// .h 
IBOutlet UISwitch *privateSwitch; 
@property (nonatomic, retain) IBOutlet UISwitch *privateSwitch; 

// .m 
@synthesize privateSwitch; 

privateSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)]; 
[privateSwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside]; 
[cell.contentView addSubview:privateSwitch]; 

SecondViewController:

// .m 

TableViewAppDelegate *dataCeter = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate]; 
AddAlbumViewController *addAlbumViewController = [[AddAlbumViewController alloc] initWithNibName:@"AddAlbum" bundle:[NSBundle mainBundle]]; 
UIView *tempView = addAlbumViewController.view; 

NSLog(@"privateSwitch: %@", addAlbumViewController.privateSwitch); 

請幫助謝謝。

回答

1

假設驗證碼:

privateSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)]; 
[privateSwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside]; 
[cell.contentView addSubview:privateSwitch]; 

實際上是在你的tableView:的cellForRowAtIndexPath :,然後該代碼可能已當你想看看privateSwitch甚至還沒有被調用。 (我假設你在嘗試記錄它之前從你的AddAlbumViewController的新實例中讀取privateSwitch,但是你忘了該代碼。)

在你的SecondViewController中,你正在創建一個AddAlbumViewController的新實例並加載該視圖從筆尖,但從你張貼的代碼你實際上沒有實際上在屏幕上展示(除非你離開了嗎?)

此外,順便說一句,你聲明瞭一個名爲privateSwitch屬性,但你沒有使用它。

+0

我宣佈privateSwitch,因爲我需要稍後閱讀它。我以後在初始化時使用它?此外,SecondView包含一個UINavigationController,並且AddAlbum是它包含的內容。當所有的內容都被加載時,動作就會發生。 – iosfreak 2011-05-31 02:57:21

+0

但是您沒有在您發佈的代碼中使用該屬性。也許發佈更多的代碼?在另一個問題上,僅僅因爲視圖被加載並不意味着該表已經填充了單元格。這是兩回事。在tableView:cellForRow中放置一個日誌或斷點...以便您可以看到它何時被調用。此外,也許發佈更多的代碼,以便人們可以看到你想要做的事情的結構。 – 2011-05-31 03:01:53

+0

當按鈕被按下時,tableView被填充。我想不出任何更多的代碼發佈。這是很多東西... – iosfreak 2011-05-31 03:07:29