2012-01-08 24 views
0

我得到了以下问题:标签始终更改为标签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?"]; 
+0

我编辑它显示正确的代码!谢谢大家的帮助 – Blade 2012-01-08 22:33:01

回答

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]; 
    } 

并使用关键字切换不好。它是目标C中的保留字。

+0

我做了,但它没有帮助。 – Blade 2012-01-08 15:54:01

+0

您需要在推送视图之前设置标签。 – 2012-01-08 15:56:11

+0

secondButton.tag = 9001; secondButton.tag = 9002;在推动你的观点之前。 – 2012-01-08 15:58:45

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); 
} 

而且是安全的,你应该presentModalViewController之前设置buttonTag

+0

好眼睛的男人。我会在那里发生一些编译器警告。 – 2012-01-08 16:25:01

+0

它在编译器中,我忘了在这里复制它。所以这(可惜)不是错误,但thx指出:) – Blade 2012-01-08 16:32:29

+0

@Blade请参阅我的编辑。 – tia 2012-01-08 16:47:02