简体   繁体   中英

retain array data outside of method objective-c

I have an array, players , with two strings inside it: player1 and player2 . Here is my .h file:

#import <UIKit/UIKit.h>

@interface hardOne : UIViewController {
        UISwitch *hard1ON;
        NSMutableArray *players;
        NSString *player1, *player2;
}

@property (nonatomic, retain) IBOutlet UISwitch *hard1ON;
@property (nonatomic) BOOL switchState;
@property (nonatomic, retain) NSMutableArray *players;

- (IBAction) switchValueChanged;
@end

The array is initialized in the viewDidLoad then the data is entered into that array in two IBActions in my .m file:

#import "hardOne.h"

@interface hardOne () <UITextFieldDelegate>

@property (nonatomic, strong) IBOutlet UITextField *textFieldOne;
@property (nonatomic, strong) IBOutlet UITextField *textFieldTwo;

@end

@implementation hardOne
@synthesize hard1ON;
@synthesize players;
@synthesize textFieldOne;
@synthesize textFieldTwo;

BOOL switchState;
int counter = 0;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [hard1ON setOn:switchState animated:NO];
    //read player names to user defaults
    [textFieldOne setText:[[NSUserDefaults standardUserDefaults] stringForKey:@"player1"]];
    [textFieldTwo setText:[[NSUserDefaults standardUserDefaults] stringForKey:@"player2"]];
    self.players = [[NSMutableArray alloc] init];
    NSLog(@"%@",self.players);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction) switchValueChanged
{
    counter += 1;
    if (counter % 2 == 0) {
        switchState = 0;
    } else {
        switchState = 1;
    }
    if (hard1ON.on) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"theChange" object:nil];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"theChange2" object:nil];
    }
}

- (IBAction) returnKey1
{
    player1 = [textFieldOne text];
    [self.players addObject:(player1)];
    //set player1's name to user defaults
    [[NSUserDefaults standardUserDefaults] setValue:[textFieldOne text] forKey:@"player1"];
}

- (IBAction) returnKey2
{
    player2 = [textFieldTwo text];
    [self.players addObject:(player2)];
    //set player2's name to user defaults
    [[NSUserDefaults standardUserDefaults] setValue:[textFieldTwo text] forKey:@"player2"];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

@end    

If I use NSLog in the second IBAction, once it is complete, the array is correctly displayed in the console with the strings player1 and player2 , however if I try to use the array anywhere else it is null . Could anyone point me in the right direction?

You've got two definitions for players.

One is a property. It's never initialized and so it's null. You use it as self.players and backed by the instance variable _players.

One is an instance variable. It's initialized in viewDidLoad. It's not nil.

This is almost surely a mistake.

I would try adding the array as a private instance variable ie add it to the .m file in the @interface with

NSMutableArray *players;

then you should be able to access the array just by using "players" instead of self.players. This should be then be available throughought the whole of your class. If this doesn't work then I would say the problem doesn't lie within the scope of your variable but rather the with some other code.

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