ios - UIPickerView 显示但不加载数据

标签 ios objective-c uipickerview

我对 objective-c 很陌生。我已经查看了大量关于堆栈溢出的问题,试图找到答案,但没有任何帮助,所以我感谢任何人可以提供的任何帮助。

我正在尝试制作一个应用程序,该应用程序将从我的 51 个数字数组中加载选择器 View 。将从数组中生成一个随机对象,如果用户从选择器中选择该对象,则会显示一个警报。我打算做一个简单的小猜谜游戏,但现在我只有加载的选择器,但它是空白的,没有文本。警报确实有效。

我已经将数据源和委托(delegate)分配给 View Controller 上的选取器 View 。

我需要帮助来弄清楚为什么我的选择器 View 没有加载我的数组,以及我可以做些什么来修复它。谢谢

这是我的h文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property (strong, nonatomic) IBOutlet UIPickerView *picker;

@end

这里是m文件:

#import "ViewController.h"

@interface ViewController ()

@property NSArray *numbers;
@property NSString * selection;
@property NSString *randomObject;
@property NSInteger rnd;


@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor darkGrayColor];

     [_numbers arrayByAddingObjectsFromArray:_numbers];

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:      (NSInteger)component{
    return _numbers.count;
}

 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row    inComponent:(NSInteger)component{

    _numbers = @[@0,@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13,@14,@15,@16,@17,@18,@19,@20,@21,@22,@23,@24,@25,@26,@27,@28,@29,@30,@31,@32,@33,@34,@35,@36,@37,@38,@39,@40,@41,@42,@43,@44,@45,@46,@47,@48,@49,@50];

    //create random object from array
_rnd = arc4random() % ([_numbers count]);

_selection = [NSString stringWithFormat:@"%ld",(long)row];
_randomObject = [_numbers objectAtIndex:self.rnd];
NSLog(@"%@",self.randomObject);


    //strings for alerts
NSString *right = [[NSString alloc]initWithFormat:@"You Guessed Right! Congratulations you've earned 3 points!"];
NSString *oneOff = [[NSString alloc]initWithFormat:@"You were so close! Only 1 off! The number was %lu. Congratulations you've earned 2 points!",(unsigned long)self.rnd];
NSString *twoOff = [[NSString alloc]initWithFormat:@"You were so close! Only 2 off! The number was %lu. Congratulations you've earned 1 point!",(unsigned long)self.rnd];
NSString *threeOff = [[NSString alloc]initWithFormat:@"Nice try! You were 3 off. The number was %lu. You earned -1 point.",(unsigned long)self.rnd];
NSString *fourOff = [[NSString alloc]initWithFormat:@"Good try! You were 2 off. The number was %lu. You earned -2 points.",(unsigned long)self.rnd];
NSString *fiveOff = [[NSString alloc]initWithFormat:@"You were no where close! The number was %lu. You earned -3 points!",(unsigned long)self.rnd];

    //alerts for guesses
UIAlertView *rightAlert = [[UIAlertView alloc]initWithTitle:@"Spot On!" message:right delegate:nil cancelButtonTitle:@"Sweet!" otherButtonTitles:nil, nil];
UIAlertView *oneOffAlert = [[UIAlertView alloc]initWithTitle:@"Only one off!" message:oneOff delegate:nil cancelButtonTitle:@"Ok!" otherButtonTitles:nil, nil];
UIAlertView *twoOffAlert = [[UIAlertView alloc]initWithTitle:@"Only Two Off!" message:twoOff delegate:nil cancelButtonTitle:@"Ok!" otherButtonTitles:nil, nil];
UIAlertView *threeOffAlert = [[UIAlertView alloc]initWithTitle:@"Three Off" message:threeOff delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
UIAlertView *fourOffAlert = [[UIAlertView alloc]initWithTitle:@"Four Off" message:fourOff delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
UIAlertView *fiveOffAlert = [[UIAlertView alloc]initWithTitle:@"No Where Close!" message:fiveOff delegate:nil cancelButtonTitle:@"Ok!" otherButtonTitles:nil, nil];


NSInteger a = [_selection integerValue];


    //guessed right
if (_selection == _randomObject)
{
    [rightAlert show];

}
    //one off
if (a == (_rnd + 1) || (a == (_rnd - 1)))
{
    [oneOffAlert show];

}
    //two off
if (a == (_rnd + 2) || (a == (_rnd - 2)))
{
    [twoOffAlert show];

}
    //three off
if (a == (_rnd + 3) || (a == (_rnd - 3)))
{
    [threeOffAlert show];

}
    //four off
if (a == (_rnd + 4) || (a == (_rnd - 4)))
{
    [fourOffAlert show];

}
    //five or more off
if ((_rnd + 5) >= a || (_rnd - 5) <= a ||(_rnd - 5) >= a || (_rnd + 5) <= a)
{
    [fiveOffAlert show];

}
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return _numbers[row];
}

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

@end

最佳答案

问题是这一行

_numbers = @[@0,@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13,@14,@15,@16,@17,@18,@19,@20,@21,@22,@23,@24,@25,@26,@27,@28,@29,@30,@31,@32,@33,@34,@35,@36,@37,@38,@39,@40,@41,@42,@43,@44,@45,@46,@47,@48,@49,@50];

出现在错误的地方。它位于 pickerView:didSelectRow:inComponent: 方法中,仅在用户做出选择后调用。因此,您的 _numbers 数组将始终为空。

将其移至viewDidLoad 方法,替换以下目前完全没有任何作用的语句:

[_numbers arrayByAddingObjectsFromArray:_numbers];

此外,您需要更改此方法:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
   return [_numbers[row] stringValue];
}

因为 _numbers[row] 是一个 NSNumber,而不是一个 NSString

关于ios - UIPickerView 显示但不加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32236999/

相关文章:

ios - 尝试将 Crashlytics 更新到 v3.10.3 时没有此类模块错误

ios - 具有多个预览的AVCaptureSession

iphone - 两个 UIButton 的一种方法

objective-c - EXC_BAD_ACCESS 指向我一行代码

ios - 我不能使用在子类中声明的方法

ios - 阻止用户选择自定义 UIPickerView Swift 2.x

iphone - 应用程序加载器 (Apple) 卡在 "Sending API usage to iTunes Connect"

objective-c - 子类化 NSArray 和 NSMutableArray

iphone - 如何更改 iPhone 中 UIPickerView 的背景颜色

iphone - 显示图像和字符串的 UIPIckerView