0

ここに、UILabelを持つ別のクラスにString値を渡し、それを表示するクラスがあります。ただし、別のクラスのラベルに表示することはできません。私はこれを2つの方法で行いました。

1)私の最初の方法、別のクラスからラベルを直接呼び出す。私の最初のクラスでは、メソッド

static void on_status_state(NSString *) stats
{
     DisplayViewController* display = [[DisplayViewController alloc] init];
        display.statusLabel.text = @"Sample Display";
}

UILabelを含むクラス。DisplayConn.hで

@property (retain, nonatomic) IBOutlet UILabel *statusLabel;

2)2番目の方法、メソッドを呼び出して、メソッドの最初のクラスのパラメーターに値を渡す

static void on_status_state(NSString *) stats
{
  DisplayViewController* display = [[DisplayViewController alloc] autorelease];
        [display toReceiveStatus:@"Sample Label"];
}

UILabelを含むクラス。DisplayConn.hで

@property (retain, nonatomic) IBOutlet UILabel *statusLabel;

次にDisplayConn.mで

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)toReceiveStatus:(NSString *) stats
{
    self.statusLabel.text = stats;
    NSLog(@"DISPLAY %@",stats); 
}

値を含むログが表示されるため、2番目の方法は機能しているように見えますが、値はまだlabel('statusLabel')に表示されません。ラベルの表示は、実行時に時々変更されます。原因は何ですか?

4

2 に答える 2

1

このことを考慮、

クラスAおよびB

UILabel はクラス B です

クラスAで、

B *newObject = [B alloc]init];
newObject.statusLabelValue = @"Simple Display";

そしてあなたのBhで

@property (retain, nonatomic) NSString *statusLabelValue;
@property (retain, nonatomic) IBOutlet UILabel *statusLabel;

Bmで

- (void)viewDidLoad
{
[super viewDidLoad];
statusLabel.text = statusLabelValue;
}
于 2012-07-12T04:38:03.127 に答える
0

DisplayViewControllerクラス のオブジェクトを初期化する必要があります 。

 static void on_status_state(NSString *) stats
{
        DisplayViewController* display = [[[DisplayViewController alloc]  init] autorelease];
        [display toReceiveStatus:@"Sample Label"];
}

- (void)toReceiveStatus:呼び出されたかどうかを確認する必要があります。DisplayConn.h で statusLabel を次のように宣言します。UILabel *statusLabel;

このようにしてみてください。参考になると思います。

- (void)viewDidLoad
{
    statusLabel  = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
    statusLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    statusLabel.font = [UIFont systemFontOfSize:15.0];
    [self.view addSubview:statusLabel];

}
于 2012-07-12T04:50:12.783 に答える