我对目标c很新。我已经查看了大量有关堆栈溢出的问题,试图找到答案,但没有任何帮助,所以我感谢任何人都可以提供帮助。
我正在尝试创建一个应用程序,它将从我的51个数字数组中加载选择器视图。将从数组生成随机对象,如果用户从选择器中选择该对象,则会显示警报。我打算做一个简单的小猜谜游戏,但是现在我只有加载的选择器但是没有文字的空白。警报确实有效。
我已经在视图控制器上为数据源和委托分配了选择器视图。
我需要帮助弄清楚为什么我的选择器视图没有加载我的阵列以及我可以做些什么来修复它。谢谢
这是我的文件:
#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