OGeek|极客世界-中国程序员成长平台

标题: ios - 在方法objective-c之外保留数组数据 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:40
标题: ios - 在方法objective-c之外保留数组数据

我有一个数组 players,其中包含两个字符串:player1player2。这是我的 .h 文件:

#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

数组在 viewDidLoad 中初始化,然后在我的 .m 文件中的两个 IBAction 中将数据输入到该数组中:

#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)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)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 addObjectplayer1)];
    //set player1's name to user defaults
    [[NSUserDefaults standardUserDefaults] setValue:[textFieldOne text] forKey"player1"];
}

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

- (BOOL)textFieldShouldReturnUITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

@end    

如果我在第二个 IBAction 中使用 NSLog,一旦完成,该数组将正确显示在控制台中,并带有字符串 player1player2,但是如果我尝试在其他任何地方使用该数组,它就是 null。谁能指出我正确的方向?



Best Answer-推荐答案


玩家有两个定义。

一个是属性。它从未初始化,因此为空。您将其用作 self.players 并由实例变量 _players 支持。

一个是实例变量。它在 viewDidLoad 中初始化。不是零。

这几乎肯定是个错误。

关于ios - 在方法objective-c之外保留数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15459488/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4