2012-01-20 37 views
1

我有一個名爲SecondViewController的TableViewController。單元格的名字是好的,並且一切正常,但是在加載詳細視圖時(在我的情況下稱爲ArticleViewController),它不想與詳細視圖的屬性進行通信。所以基本上,如果我想在詳細視圖中更改標籤,它就不起作用。TableView單元格不會與其DetailView進行通信。

下面是代碼:

SecondViewController.h

@interface SecondViewController : UITableViewController 
{ 
    NSMutableArray *json; 
} 

@end 

SecondViewController.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    ArticleViewController *article = [self.storyboard instantiateViewControllerWithIdentifier:@"ArticleViewController"]; 
    NSDictionary *info = [json objectAtIndex:indexPath.row]; 
    NSLog(@"%@",[info objectForKey:@"username"]); 
    [article.usernameLabel setText:[info objectForKey:@"username"]]; 

    [self.navigationController pushViewController:article animated:YES]; 


} 

ArticleViewController.h

#import <UIKit/UIKit.h> 

@interface ArticleViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *usernameLabel; 
@property (weak, nonatomic) IBOutlet UILabel *articleTitle; 

@end 

ArticleViewController.m

--synthesizing properties--

我迷上了標籤和ArticleViewController,但標籤永遠不會改變。

+2

不要將信息保存在UI變量中。創建名稱和文章的特殊變量,並在需要時加載(例如viewWillAppear :) – OdNairy

+0

非常感謝!這工作:)。我只是想知道爲什麼......但謝謝! –

+0

這太重要了4你明白爲什麼 - 閱讀更多關於加載視圖,關於延遲初始化等。 如果說一個簡短的視圖,網點會比你的視圖控制器晚得多。 – OdNairy

回答

0

我猜想在你做article.usernameLabel setText:時,article還沒有加載它的筆尖。所以article.usernameLabel仍然是nil。您可以通過執行[article view]來強制article加載它的筆尖。但OdNairy的建議更好。

相關問題