-1

私は iOS 開発の基本に取り組んでいます。

これまでのところ、押すとテキストが表示されるボタンがあります。しかし、私がやりたいのは、それが押された後、ボタンのテキストを変更したいということです。これまでのところ、これは私が持っているものです:

- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.backgroundColor = [UIColor redColor];
button.titleLabel.textColor=[UIColor blackColor];
button.frame = CGRectMake(25, 100, 275, 60);
[button setTitle:@"Press this button to reveal the text!" forState:UIControlStateNormal];
[button addTarget:self action:@selector(button_method:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)button_method:(UIButton *)sender {
NSString *test = @"I am learning Objective-C for the very first time! Also, this is my first ever variable!";
// handle button press
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = test;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

[button setTitle:@"You pressed the button"];button_methodに追加しようとすると

これは機能しません...なぜですか?そして、どうすればそれを機能させることができますか?

4

2 に答える 2

1

button_method に追加しようとする[button setTitle:@"You pressed the button"];と、機能しません...なぜですか?

そのメソッドは には存在しないためですUIButton

そして、どうすればそれを機能させることができますか?

UIButton 実際に反応するメソッドを使用することによって。例えば:

[sender setTitle:@"You pressed the button" forState:UIControlStateNormal];

そして、関連するドキュメントを読んでください。

于 2012-11-26T21:23:14.220 に答える
0

コードはボタンのタイトルを変更しません。ただし、ビューのサブビューとしてラベルを追加するだけです。これは、あなたの望むことですか?

ボタンのテキストを変更したい場合はsender.title.text = test;、button_method で行います。

NSLog(@"Button pressed");ボタンを押すメソッドにa を追加して、呼び出されていることを再確認することもお勧めします。

于 2012-11-26T21:18:41.113 に答える