开发者

Can't load an NSMutableArray with UIView objects

On my game board (which is开发者_Python百科 called GameViewController), I have six Seats (in the xib) which are nothing more than subclassed UIViews (so I also have Seat.h and .m files in my project). When the game board gets initialized these seats also get created (thanks to the xib). I want to have the seats loaded into an NSMutableArray so that I can use them later. For some reason I can't get it to work.

In my GameViewController header file I've added NSMutableArray *seats; as an instance variable and included @class Seat; above the interface declaration.

In my awakeFromNib method of the GameViewController I have seats = [[NSMutableArray arrayWithCapacity:6] retain]; So the array should be initialized when the game board appears.

Then in my Seat header file I've included GameViewController *controller; as an instance variable and included @class GameViewController above the interface declaration. I've also added and synthesized a property for the GameViewController.

In the Seat's awakeFromNib method I have [controller registerSeat:self];

This calls a method in my GameViewController that has only one line: [seats addObject:seat]; This should add the seat to the array. But for some reason this method never seems to get called. Or if it does, I can never tell. When I debug, focus never goes to the registerSeat method even though the seats do get added to the board. I hope this all makes sense. If the code is needed, I can provide it. It might be easier to do that anyway. What do you guys think? I'm stumped at the moment.

The method declaration is as follows: - (void) registerSeat:(Seat *)seat;

GameViewController.h:

#import <UIKit/UIKit.h>

@class Seat;

@interface GameViewController : UIViewController {

  NSMutableArray *seats;

}

- (void) registerSeat:(Seat *)seat;

@end

GameViewController.m

#import "GameViewController.h"
#import "Seat.h"

@implementation GameViewController

- (void)awakeFromNib {

 seats = [[NSMutableArray arrayWithCapacity:6] retain];

}

- (void) registerSeat:(Seat *)seat {

  [seats addObject:seat];
  NSLog(@"seat has been registered");

}

@end

Seat.h:

#import <UIKit/UIKit.h>

@class GameViewController;

@interface Seat : UIView {

  GameViewController *controller;

}

@property (nonatomic, retain) IBOutlet GameViewController *controller;

@end

Seat.m:

#import "Seat.h"
#import "GameViewController.h"

@implementation Seat

@synthesize controller;

- (void) awakeFromNib {

  [controller registerSeat:self];

}

@end


What you're doing here is what Xcode 4's IBOutletCollection is designed to solve. In your NIB, select all of your Seat views (these should be called SeatView, BTW. Seat should be a model class, not a view class). Drag the selected group to your GameViewController header and request an IBOutletCollection. This will wire them all as a random-ordered array. (Why they chose to make it a random-ordered array is beyond me; it's a somewhat insane construct, but it exactly matches what you're trying to do above.)

As @highlycaffeinated notes, the most likely reason for your current code failing is that you've failed to wire your controller and it's nil. When "nothing happens" in Objective-C, it's almost always because you're talking to nil.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜