【发布时间】:2012-01-08 12:36:06
【问题描述】:
我遇到了一个小问题(不足为奇,因为我刚开始使用 xcode)。我试图用 if 语句来解决它,但它们显然是错误的方法。
这是我想要做的:在第一个 ViewController 中,我有例如 4 个按钮。如果用户按下第一个按钮,他将进入 ViewController2 并且标签显示“您按下了第一个按钮”。如果用户按下第二个按钮,他会到达 ViewController2 并且标签显示“你按下了第二个按钮”等等。
我尝试使用标记语句来解决它,例如: FirstViewController.m
- (IBAction)switch:(id)sender;
{
UIButton *buttonPressed = (UIButton *)sender;
SecondViewController *second =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
second.buttonTag = buttonPressed.tag;
[self.navigationController pushViewController:second animated:YES];
(button.tag = 9001);
- (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;
[self.navigationController pushViewController:third animated:YES];
(button2.tag = 9002);
这就是我在 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?"];
他总是给我 ButtonTag 9001 的标签 - 有人知道为什么吗?
【问题讨论】:
-
您的“编辑”是一个单独的问题。你的意思是说:
(buttonTag == 9001)。您的单个等号正在分配 buttonTag,而不是检查是否相等。所以当执行到if ((buttonTag = 9002))时,buttonTag 被分配给 9002,这是真的。所以它会改变你的标签两次。 -
你是对的,对不起,但我不想因为它打开一个新问题。我把它改成了你的建议,但现在他只显示标签 9001 中的标签:(
标签: ios label switch-statement viewcontroller