2015-08-28 13 views
1

SetScoringTableViewController.h:如何顯示另一個類的變量?

@interface SetScoringTableViewController : UITableViewController 
@property (nonatomic, strong) NSString *name1; 
@end 

SetScoringTableViewController.m:

@implementation SetScoringTableViewController 
@synthesize name1; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    name1 = @"Hello World" 
} 

GameDetailsTableViewController.m

if (indexPath.section == 0 && indexPath.row == 0) { 

    SetScoringTableViewController *setScoring = [[SetScoringTableViewController alloc]init]; 

    static NSString *CellIdentifer1 = @"GameDetailsSetScoringCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer1]; 
    label = (UILabel *)[cell viewWithTag:0]; 


    label.text = [NSString stringWithFormat: @" %@", setScoring.name1]; 

      return cell; 
} 

當我嘗試運行它,我得到的是空。你能幫我找到如何在我的標籤上顯示「Hello World」。任何幫助將不勝感激。

回答

2

viewDidLoad方法可能還沒有被調用。覆蓋在你的SetScoringTableViewControllerinit方法和設定值有:

- (instancetype)init { 
    if (self = [super init]) { 
     _name = @"Hello World"; 
    } 
    return self; 
} 

爲什麼你會實例化從cellForRowAtIndexPath方法的視圖控制器?它在之後立即被釋放。

+0

謝謝你這工作:) – av993

1

當您在GameDetailsTableViewController.m中調用setScoring.name1時,您正在調用剛在SetScoringTableViewController *setScoring = [[SetScoringTableViewController alloc]init];中創建的對象的屬性。您的viewDidLoad從未執行過,或者即使是setScoring也是SetScoringTableViewController類的另一個實例。

在調用之前,您首先需要爲name1指定一些值。例如:

if (indexPath.section == 0 && indexPath.row == 0) { 

SetScoringTableViewController *setScoring = [[SetScoringTableViewController alloc]init]; 

static NSString *CellIdentifer1 = @"GameDetailsSetScoringCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer1]; 
label = (UILabel *)[cell viewWithTag:0]; 

setScoring.name1 = @"Hello World" 

label.text = [NSString stringWithFormat: @" %@", setScoring.name1]; 

     return cell; 
} 

現在您的標籤將具有正確的文本。但在這種情況下,它根本沒有任何意義。也許我可以給你一個更好的解釋,如果你描述你到底想做什麼。

相關問題