2011-12-24 16 views
2

こんにちは私は目標Cに新しいです、私は問題があります。 私は自分のゲームのレベル1と呼ばれるView Controllerを持っています。どのように他のクラスから呼び出しViewControllerに引数を渡すことができます

GameViewController *level1 = [self.storyboard instantiateViewControllerWithIdentifier:@"GameIdentifier"]; 
[self.navigationController pushViewController:level1 animated:YES]; 

正常に動作します。

... しかし私は同じのViewController(GameViewController)を使用して、2つの異なるレベル(LEVEL_2とLEVEL_3)を作りたい(私は同じクラスからそれらを呼び出します)、 が、私は合格する方法がわかりませんGameViewControllerへの引数(例えば、int)(その引数は現在のレベル(2または3など)になります)。

答えて

13

GameViewControllerクラスにインスタンス変数(およびそれを取得/設定するアクセサ)を追加し、レベル番号を "level1"または "level2"インスタンスに割り当てる必要があります。ここで

はあなたGameViewControllerクラスを記述する必要があり、どのようを示す、いくつかのサンプルコードです:

// GameViewController.h 

@interface GameViewController : UIViewController 
@property (nonatomic, readwrite, assign) int level; 
@end 

// GameViewController.m 

@implementation Test 
@synthesize level; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    if (level == 1) 
    { 
     // Do something for level 1 
    } 
    else if (level == 2) 
    { 
     // Do something for level 2 
    } 
} 

@end 

その後、あなたはあなたのビューコントローラにレベル番号を渡す必要があります。

GameViewController *level1 = [self.storyboard instantiateViewControllerWithIdentifier:@"GameIdentifier"]; 
level1.level = 1; 
[self.navigationController pushViewController:level1 animated:YES]; 
+0

おかげでルーク、できます!!!! – user1114232

+0

心配はいりません!あなたのゲームで幸運: –

+1

@ user1114232あなたは答えがあればそれを受け入れるべきです – MadhavanRP

関連する問題