2011-12-14 14 views
0

UILabelに問題があります。ここには2つのファイルのViewControllerMainClass があります。最初はnibファイル用のコントローラで、label1アウトレットです。 2番目のファイルは、別のラベルmainLabel( "MainClassのinitメソッドから設定")を含むクラスです。 mainLabellabel1に設定してViewControllerに設定します。別のクラスからUILabelテキストを設定する方法

はMainClassのinit方法では、私はmainLabel.text

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

のためのテキストを設定し、その後、私はNSLog(@"%@",mainLabel.text);を呼び出しますが、コンソールには、私がnull

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

を持っているし、その後の後、私が持っている- (void)viewDidLoadlabel1 = 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 
を見つけた別の間違い
関連する問題