NSInvalidArgumentException - 无法识别的选择器发送到实例? UITextView / UIPickerView

时间:2013-08-29 22:49:25

标签: ios uitextview uipickerview

我正在尝试设置一个文本字段,用用户输入填充pickerView。每次启动应用程序时都会出现此错误:

NSInvalidArgumentException', reason: '-[UITextField isEqualToString:]: unrecognized   selector sent to instance.

@synthesize pickerView;
@synthesize textArray;
@synthesize textField;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textArray = [[NSMutableArray alloc] init];

    PFUser *currentUser = [PFUser currentUser];
    if (currentUser) {
    NSLog(@"Current user: %@" , currentUser.username);
}
else {
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}
    self.textField.delegate = self;

    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

[nc addObserver:self selector:@selector(keyboardWillShow:) name:
 UIKeyboardWillShowNotification object:nil];

[nc addObserver:self selector:@selector(keyboardWillHide:) name:
 UIKeyboardWillHideNotification object:nil];

self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                            action:@selector(didTapAnywhere:)];
}

-(void)didTapAnywhere: (UITapGestureRecognizer *) note {
    [self.textField resignFirstResponder];
}
-(void) keyboardWillShow:(NSNotification *) note {
    [self.view addGestureRecognizer:self.tapRecognizer];
}

-(void) keyboardWillHide:(NSNotification *) note
{
    [self.view removeGestureRecognizer:self.tapRecognizer];
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:   (NSInteger)component
{
    if ([self.textArray count] == 0)
        return 1;
    return [self.textArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:   (NSInteger)component {
    return [self.textArray objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row forComponent:    (NSInteger)component{
    NSLog(@"You selected this: %@", [self.textArray objectAtIndex:row]);
}

-(void)textFieldDidEndEditing:(UITextField *)textField{

    [self.textArray addObject:self.textField.text];

    [self.textField resignFirstResponder];

}

- (IBAction)logout:(id)sender {
    [PFUser logOut];
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}
@end

我对所有事情都很陌生,所以如果它是可能的基本内容,请对我轻松一点。干杯!

2 个答案:

答案 0 :(得分:2)

您必须在

中收到此错误
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component {
    return [self.textArray objectAtIndex:row];
}

当您返回文本字段而不是文本(标题)时。

viewDidLoad中,更改以下行:

[self.textArray addObject:self.textField];

[self.textArray addObject:self.textField.text];

确保有一些文字,因此标题不会返回为空。那么你究竟想把它放在数组中呢?是文本还是文本域?

修改

首先,我看到的是 textfield delegate 。在viewDidLoad中设置self.textField.delegate = self;,以便调用textFieldDidEndEditing。在其中,重新加载pickerview,以便可以使用当前文本正确填充它。

-(void)textFieldDidEndEditing:(UITextField *)textField{

    [self.textArray addObject:self.textField.text];

    [self.pickerView reloadAllComponents];

}

对于选择器标题,如果数组中没有数据,则返回1.您应该返回注释中所说的内容。

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:   (NSInteger)component {

if(self.textArray.count == 0)
return @"There is nothing";
else
    return [self.textArray objectAtIndex:row];
}

确保取消键盘,当用户按“输入键”时,您应该拨打[self.textfield resignFirstResponder]。用你的文本字段连接一些方法,例如“hidekeyboard”。

-(IBAction) hidekeyboard:(id) sender

{
[sender resignFirstResponder];
}

希望它可以帮助你......!

enter image description here

答案 1 :(得分:1)

您正在将文本字段添加到textArray,因此当您尝试使用textArray在titleForRow:forComponent中提供标题时:您会收到该错误。 textArray应包含文本,而不是文本字段。

如果你想使用文本字段来填充选择器,那么你应该实现文本字段委托方法textFieldDidEndEditing :(也许还有其他人),并在该方法中将text字段的文本添加到textArray中。