Ios 标签不';我不会相应地改变
我遇到了以下问题:标签总是更改为标签9001中定义的标签。有人能帮我找出错误吗 ViewController.m:Ios 标签不';我不会相应地改变,ios,button,tags,label,viewcontroller,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:n
- (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?"];
下面是一个示例,您两次展示视图,我在这个示例中取出了模态:
- (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中是一个保留字。可能是因为您忘记关闭第一个
的结束括号,如果
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) {
或者您将按钮标记设置为不正确的实例
- (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);
}
为了安全起见,您应该在按下view.secondButton.tag=9001之前设置presentModalViewController
您需要设置标签;和secondButton.tag=9002;在推送视图之前。如果将其设置为senders标记,则可以在interface builder或情节提要中设置标记。请参阅上面编辑的代码,如果希望使用ModalView,只需将推送方法更改为presentModalViewController:animated;谢谢你的代码,我实现了它,但遗憾的是它仍然没有改变到另一个标签,除了9001Good eye man的标签。我想这里可能会有一些编译器警告。它在编译器中,我只是忘了把它复制到这里。所以,这(很遗憾)不是错误,而是thx指出的:)谢谢,我确实忘记了,仍然没有做这个把戏。另外,在另一个按钮之前设置buttontag也没有帮助:(我也注意到了这一点:sender2应该是sender。我编辑它以显示正确的代码!谢谢大家的帮助