【问题标题】:Label doesn't change accordingly标签没有相应改变
【发布时间】:2012-01-08 15:34:46
【问题描述】:

我遇到了以下问题:标签总是更改为标签 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?"];

【问题讨论】:

  • 我对其进行了编辑以显示正确的代码!谢谢大家的帮助

标签: ios button tags label viewcontroller


【解决方案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 中的保留字。

【讨论】:

  • 推送视图前需要设置标签
  • secondButton.tag = 9001;和 secondButton.tag = 9002;在您的视图被推送之前。
  • 您将其设置为发件人标签,如果您在界面生成器或情节提要中设置标签,这很好。
  • 查看上面的编辑代码,如果你想要一个 ModalView,只需将 push 方法更改为 presentModalViewController:animated;
  • 感谢代码,我实现了它,但遗憾的是它仍然没有更改为另一个标签,除了来自 9001 的标签
【解决方案2】:

可能是因为您忘记关闭第一个 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

【讨论】:

  • 好眼力的人。我想这里会有一些编译器警告。
  • 在编译器里,我只是忘了复制到这里。所以这(可悲)不是错误,但谢谢指出:)
  • 谢谢,我确实忘记了这一点,仍然没有成功。在另一个之前设置按钮标签也没有帮助。 :(
  • 我也注意到了这一点:sender2 应该只是发件人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-13
  • 2019-11-22
  • 1970-01-01
  • 2023-01-25
  • 2019-10-14
相关资源
最近更新 更多