简体   繁体   中英

Using a Mutable Array from another View Controller

I've been looking around and found some answers but none of them worked, I don't know if i'm missing something. For some reason punish.castigos as well as playersView.players is nil at this moment in the code:

in GameViewController.m:

#import "SetPlayersViewController.h"
#import "SetPunishmentsViewController.h"

SetPlayersViewController *playersView = [[SetPlayersViewController alloc] init];
SetPunishmentViewController *Punish = [[SetPunishmentViewController alloc] init];
NSMutableArray *punishments = Punish.castigos;
NSMutableArray *playersOn = playersView.players;
int pickCard = arc4random() % numCards;
int playr = arc4random() % [playersOn count];//here is where i get EXC_ARITHMETIC(code=EXC = i386 DIV..)
int punsh = arc4random() % [punishments count];

in SetPlayersViewController.h:

@interface SetPlayersViewController : UIViewController {
NSMutableArray *players;
}
@property (nonatomic,retain) NSMutableArray *players;

in SetPlayersViewController.m:

@synthesize players;

//in view did load
players = [[NSMutableArray alloc] initWithObjects:@"A",@"B",nil];

It happens because of UIViewController's the lazy initialization.

The view of UIViewController will be loaded only after some one call the view property. So, the viewDidLoad method will be called only after some one calls the view property too.

You should initialize your array in init method of view controller fe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM