• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在方法objective-c之外保留数组数据

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:40:48 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个数组 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap