我遇到了一个小问题(毫不奇怪,因为我刚开始使用xcode)。我试图用if语句解决它,但他们显然是错误的路要走。两个ViewControllers - 第一个有x个按钮,第二个有x个标签
这是我想要做的:在第一个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,这是真的。所以它会更改标签两次。 – NJones 2012-01-08 15:04:42
你说得对,对不起,但我不想因为它而开一个新的问题。我改变了你的建议,但现在他只显示标签9001:( – Blade 2012-01-08 15:11:17