2011-12-14 72 views
0

我有問題UILabel。這裏有兩個文件的ViewControllerMainClass 首先是控制器的筆尖文件與label1插座。第二個文件是另一個標籤mainLabel,它包含文本(「從MainClass中的init方法設置」)的類。我想在ViewController中設置mainLabellabel1如何設置另一個類的UILabel文本

在MainClass的init方法我設置文本爲mainLabel.text

mainLabel.text = @"Set from init method in MainClass"; 

之後,我打電話NSLog(@"%@",mainLabel.text);但在控制檯我有空

2011-12-14 10:31:31.048 ClassTask[1076:f803] (null) 

,然後後,我在- (void)viewDidLoad調用label1 = newClass.mainLabel;我有在我的iPhone模擬器中用label1沒有文字查看。 // ViewController.h

#import <UIKit/UIKit.h> 
#import "MainClass.h" 
@interface ViewController : UIViewController { 
    UILabel *label1;  
    MainClass *newClass; 
} 
@property (nonatomic, retain) IBOutlet UILabel *testLabel; 
@end 

// ViewController.m

#import "ViewController.h" 
@implementation ViewController 
@synthesize label1; 
-(void) dealloc{ 
    [label1 release]; 
    [super dealloc]; 
} 
#pragma mark - View lifecycle 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    newClass = [MainClass new]; 
    label1 = newClass.mainLabel; 
} 
@end 

// MainClass.h

#import <UIKit/UIKit.h> 
@interface MainClass : NSObject { 
    UILabel *mainLabel; 
} 
@property (nonatomic, retain) IBOutlet UILabel *mainLabel; 
@end 

// MainClass.m

#import "MainClass.h" @implementation MainClass @synthesize mainLabel; 
-(id) init { 
    if (self = [super init]) { 
     mainLabel.text = @"Set from init method in MainClass"; 
     NSLog(@"%@",mainLabel.text); 
    } 
    return self; } 
-(void)dealloc { 
    [mainLabel release]; 
    [super dealloc]; 
} 
@end 

回答

1

你爲什麼不讓它的NSString,它基本上是一個文本你想在另一個ViewController中訪問。

另一個錯誤,我發現

#import "ViewController.h" 
@implementation ViewController 
@synthesize label1; 
-(void) dealloc{ 
    [label1 release]; 
    [super dealloc]; 
} 
#pragma mark - View lifecycle 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    newClass = [MainClass new]; 
    **label1 = newClass.mainLabel.Text; // its a label, so whenever you retrieve its value it would be like labelName.Text** 
} 
@end 
相關問題