0

私はiPhone

私のアプリにはたくさんのボタンがあり、各ボタンには 1 つUILabelあり、ユーザーがボタンをクリックすると、そのボタンからそのラベルの対応するテキストを取得したいと考えています。

これが私のコードスニペットです。

- (void) buttonClicked:(UIButton*)sender
{ 
    NSString *Txt=  sender.titleLabel.text;
    NSLog(@"Txt=%@",Txt);
}

しかし、私のログには次のように表示されます:Txt=null

どんな助けでも大歓迎です。

4

2 に答える 2

2

次のようなボタンのラベルをサブビューした可能性があると思います。

 UILabel *lblText = [[UILabel alloc]initWithFrame:button.frame];
 lblText.text = @"NameofButton";
 [button addSubview:lblText];
 [lblText release];

ボタンクリックイベントは次のようになります。

- (void) buttonClicked:(UIButton*)sender
{ 
  //NSArray *arrSubViews = [sender subviews];
  for (id anObject in [sender subviews]) {
      if([anObject isKindOfClass: [UILabel class]]){
          UIlabel *myLabel = anObject;
          NSString *Txt=  myLabel.text;
          NSLog(@"Txt=%@",Txt);
    }
  }

}
于 2012-07-21T08:47:16.067 に答える
0

次のようにタイトルを設定していることを確認してください。

[button setTitle:@"your title" forState:UIControlStateNormal];
于 2012-07-21T08:50:15.507 に答える