2012-01-08 14 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

答えて

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

+0

ビューをプッシュする前にタグを設定する必要があります。 –

+0

secondButton.tag = 9001; secondButton.tag = 9002;あなたのビューがプッシュされる前に。 –

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

と安全のために、あなたはpresentModalViewControllerbuttonTagを設定する必要があります。

+0

グッドアイマン。私はここにいくつかのコンパイラの警告があったと思います。 –

+0

コンパイラにありますので、ここでコピーするのを忘れてしまいました。だから、(悲しいことに)間違いではなく、それを指摘するためのthxです。 – Blade

+0

@Blade私の編集をご覧ください。 – tia

関連する問題