首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode UIPickerView "NSInvalidArgumentExpression"?

Xcode UIPickerView "NSInvalidArgumentExpression"?
EN

Stack Overflow用户
提问于 2014-12-29 00:53:03
回答 1查看 109关注 0票数 1

我试图在一个UIPickerView中实现一个FlipsideViewController,这只是一个设置选项卡。

下面是.h文件中的相关代码:

代码语言:javascript
复制
@class FlipsideViewController;

@protocol FlipsideViewControllerDelegate<UIPickerViewDataSource, UIPickerViewDelegate>
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

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

@property (strong, nonatomic) NSArray *categoriesData;

@property (strong, nonatomic) NSMutableArray* dataArray;

- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

- (NSInteger)Categories:(UIPickerView *)Categories numberOfRowsInComponent:(NSInteger)component;

- (NSString*)Categories:(UIPickerView *)Categories titleForRow:(NSInteger)row forComponent:(NSInteger)component;

下面是.m文件中的相关代码:

代码语言:javascript
复制
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

     _Categories = [[UIPickerView alloc] initWithFrame:CGRectMake((pickerWidth*4/3)/2-pickerWidth/2, 50.0f, pickerWidth, 200.0f)];

        _Categories.showsSelectionIndicator = true;

        [_Categories selectRow:2 inComponent:0 animated:YES];

        [self.view addSubview: _Categories];

        _dataArray = [[NSMutableArray alloc] init];

        [_dataArray addObject:@"One"];
        [_dataArray addObject:@"Two"];
        [_dataArray addObject:@"Three"];
        [_dataArray addObject:@"Four"];
        [_dataArray addObject:@"Five"];

        _categoriesData = _dataArray;

         _Categories.dataSource = (id)self;

         _Categories.delegate = (id)self;

}

- (int)numberOfComponentsInPickerView:(UIPickerView *)Categories
{
    return 1;
}

// The number of rows of data
- (NSInteger)Categories:(UIPickerView *)Categories numberOfRowsInComponent:(NSInteger)component
{
    return _categoriesData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)Categories:(UIPickerView *)Categories titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_categoriesData objectAtIndex:row];
}

抱歉,如果你读的太多了。关键是,在最后一部分中,当运行- (NSInteger)Categories:(UIPickerView *)Categories numberOfRowsInComponent:(NSInteger)component时,它返回一个"NSInvalidArgumentException“。我尝试在每种方法中重新初始化dataArray和categoriesData,但都没有效果。我不确定我该怎么做..。请帮帮忙。谢谢!

编辑:

发现了错误。那是在方法呼叫中。与其指定我试图访问的UIPickerView,相反,我应该只添加一个通用的"UIPickerView“。它应该是这样的:

代码语言:javascript
复制
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{}

不是这样的:

代码语言:javascript
复制
- (NSString*)pickerView:(UIPickerView *)Categories titleForRow:(NSInteger)row forComponent:(NSInteger)component

- (NSInteger)pickerView:(UIPickerView *)Categories numberOfRowsInComponent:(NSInteger)component{}

希望这能帮助任何人发现类似的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-29 01:38:13

看起来,categoriesData没有正确地分配值。尝试在下面更改这一行并检查:-

代码语言:javascript
复制
self.categoriesData = _dataArray;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27682172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档