标签不会相应更改

时间:2012-01-08 15:34:46

标签: ios button tags label viewcontroller

我遇到了以下问题:标签总是更改为标签9001中定义的标签。有人可以帮助我发现错误吗?

ViewController.m:

- (IBAction)switch0:(id)sender

{(button.tag = 9001);
UIButton *buttonPressed = (UIButton *)sender;
SecondViewController *second =[[SecondViewController alloc] initWithNibName:nil     bundle:nil];
 [self presentModalViewController:second animated:YES];
second.buttonTag  = buttonPressed.tag;
 }


- (IBAction)switch2:(id)sender2

{ (button2.tag = 9002); 
UIButton *buttonPressed = (UIButton *)sender2;
SecondViewController *third =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:third animated:YES];
second.buttonTag  = buttonPressed.tag;

}

SecondViewcontroller.m

  - (void)viewDidLoad
 {
[super viewDidLoad];

if (buttonTag == 9001) {
self.label1.text = [[NSString alloc] initWithFormat:@"Radnomtext"];
self.label2.text = [[NSString alloc] initWithFormat:@"Randomtext"];
self.label3.text = [[NSString alloc] initWithFormat:@"Randomtext?"];
}
if (buttonTag == 9002) {
self.label1.text = [[NSString alloc] initWithFormat:@"Radnomtext2"];
self.label2.text = [[NSString alloc] initWithFormat:@"Randomtext2"];
self.label3.text = [[NSString alloc] initWithFormat:@"Randomtext2?"];

2 个答案:

答案 0 :(得分:1)

下面是一个例子,你两次呈现视图我在这个例子中取出了模态:

   - (IBAction)switch0:(id)sender

    {
    UIButton *buttonPressed = (UIButton *)sender;
    SecondViewController *second =[[SecondViewController alloc] initWithNibName:nil     bundle:nil];

 NSInteger tagToSet = 9001;
    second.buttonTag  = tagToSet;;
      [self.navigationController pushViewController:second animated:YES];
     }

并且使用关键字switch并不好。它是Objective C中的保留字。

答案 1 :(得分:1)

也许是因为您忘记关闭第一个if的结尾括号?

if (buttonTag == 9001) {
    self.label1.text = [[NSString alloc] initWithFormat:@"Radnomtext"];
    self.label2.text = [[NSString alloc] initWithFormat:@"Randomtext"];
    self.label3.text = [[NSString alloc] initWithFormat:@"Randomtext?"];
} // bracket supposed to be here

if (buttonTag == 9002) {

或者您可能将buttonTag设置为不正确的实例?

- (IBAction)switch2:(id)sender2

{
UIButton *buttonPressed = (UIButton *)sender2;
SecondViewController *third =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:third animated:YES];
second.buttonTag  = buttonPressed.tag; // supposed to be third?
[self.navigationController pushViewController:third animated:YES];
(button2.tag = 9002); 
}

为安全起见,您应在buttonTag之前设置presentModalViewController