Objective-c PickerView 显示内存位置而不是来自单例的对象变量名称

Objective-c PickerView displays memory locations instead of object variable names from singleton

概述:我有一个 MapKit 地图、一个 PickerView 和一个静态武器对象数组(每个武器都有一些成员变量数据)。目的是使用选择器视图并在我的地图上显示一些信息(范围、名称)。

需要一些关于存储在我的单例中的对象数组的帮助。我已成功将数组传递给地图,但选择器显示的是对象的内存位置而不是名称。

SingletonFile.m 我在其中创建对象数组

static dispatch_once_t pred;
static SingletonFile *shared = nil;
dispatch_once(&pred, ^{
    shared = [[SingletonFile alloc] init];
    shared.theWeapons = [NSMutableArray arrayWithArray:
                         @[[[weapon alloc] initWithName:@"M16" weaponPicName:@"M16 Pic Name"],
                        [[weapon alloc] initWithName:@"M20" weaponPicName:@"M20 Pic Name"],
                           [[weapon alloc] initWithName:@"M3" weaponPicName:@"MyName"]
                           ]];

    //This is my attempt to create a second array with only the weaponPicName's from theWeapons array created above.
    shared.theWeaponNameArray = [shared.theWeapons valueForKey:@"weaponPicName"];
    //This line produces the error, not key value coding-compliant for the key weaponName
    //weaponName and weaponPicName are a part of the weapon object
});
return shared;

Weapon.m 武器对象的构造函数

-(id)initWithName:(NSString*)weaponName weaponPicName:(NSString*)weaponPicName {

    self = [super init];
    if (self) {
        _weaponName = weaponName;
        _weaponPicName = weaponPicName;
    }
    return self;
}

MapViewController.m 是我从我的单例中获取武器数组的地方 class

//Initialize tableNames to the singleton name array for use in picker
self.tableNames = [SingletonFile weaponSingleton].theWeapons;

MapViewController.m(再往下)是我的选择器代码。

- (IBAction) pickType:(id)sender {

    arrayPickerRows = self.tableNames;

    [ActionSheetStringPicker showPickerWithTitle:@"Select"
                                            rows:arrayPickerRows
                                initialSelection:0
                                       doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
                                           selectedRow = [arrayPickerRows objectAtIndex:selectedIndex];
                                           [self typePickerDone:sender];
                                       }
                                     cancelBlock:^(ActionSheetStringPicker *picker) {
                                         NSLog(@"Block Picker Canceled");
                                     }
                                          origin:sender];
}
- (void) typePickerDone:(UIButton*) sender {


    [self.WeaponPickerbutton setTitle:selectedRow forState:UIControlStateNormal];
}

我确定答案与访问对象的武器名称有关。我尝试使用 valueForKey 从对象数组创建一个 weaponNames 数组,但它说我的键不符合值。

(我也知道我可以用 .plist 完成所有这些,但我正在尝试学习单例并使用对象数组)

您的 arrayPickerRows 存储了一系列武器。当您为按钮设置标题时,因此当您使用 [arrayPickerRows objectAtIndex:selectedIndex] 时,您将在索引 selectedIndex 处获得武器的地址。

在您的代码中,您没有变量 selectedRow 的定义。但是,它应该是 class weapon。设置按钮名称时,应使用以下代码:

[self.WeaponPickerbutton setTitle:selectedRow.weaponName forState:UIControlStateNormal];