0

我正在 XCode 上创建一个游戏,它有一个带有设置条件按钮的菜单(即“播放到 30”、“播放到 20”等)。我希望这些按钮与ViewController我的游戏相同,唯一的区别是在游戏结束之前必须达到多少分。ViewController为每个设置设置多个相同的倍数效率太低了。有没有解决的办法?

4

1 回答 1

0

In your game view controller create a custom initializer in as so:

// add in GameViewController.m
@implementation GameViewController
-(id)initWithLimit:(int)limit {
    self = [super initWithNibName:@"NibName" bundle:nil];
    if (self) {
       _limit = limit;
    }
    return self;
}

// add in GameViewController.h
@interface GameViewController : UIViewController
@property (nonatomic) int limit;
@end

implement the menu's button actions as so:

-(IBAction)play30 {
     GameViewController *game = [[GameViewController alloc] initWithLimit:30];

     // Handle game view here.
}

this answer assumes you create a new instance of GameViewController when the user taps the button. if you don't want to instantiate a new ViewControllerSubclass each time the button is taped then you can create a GameViewController property in you menu view controller and use lazy instantiation for the game view controller:

 - (GameViewController *)game {
      if (!_game) _game = ...;
      return _game;
 }

 -(IBAction)play20 {
     // Assuming game is a property.
     self.game.limit = 20;

     // Perform setup that expects the limit property to be set.
     [self.game setup];

     // Handle game view here.
}

Hope this helped :)

于 2013-06-21T19:16:52.423 回答