文本字段崩溃成为pickerview的第一响应者

时间:2014-08-18 10:07:48

标签: ios uitextfield uipickerview

所以我一直在尝试让这个挑选视图工作,但我无法让它工作。当我运行应用程序时,它崩溃而没有任何堆栈跟踪,并在[textField becomeFirstResponder]上显示线程1:EXC_BAD_ACCESS。 pickerArray是正确的,所以这不是问题。

#import "TestViewController.h"
#import "FindClasses.h"

@interface TestViewController ()
@property UIPickerView *picker;
@property NSArray *pickerArray;
@property (nonatomic, strong) FindClasses *finder;
@end

@implementation TestViewController

@synthesize finder = _finder;

- (FindClasses *)finder
{
    if (!_finder) _finder = [[FindClasses alloc] init];
    return _finder;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.pickerArray = [self.finder findClassesInTimetable];

    self.classField.delegate = self;
    self.picker = [[UIPickerView alloc] init];
    self.picker.delegate = self;
    self.picker.dataSource = self;
    self.classField.inputView = self.picker;
    // Do any additional setup after loading the view.
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField  {
    [textField becomeFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}

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

#pragma mark - UIPickerView method implementation

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


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


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

感谢。

3 个答案:

答案 0 :(得分:1)

尝试从以下方法中删除[textField becomeFirstResponder];

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField  {
    [textField becomeFirstResponder];
    return YES;
}

错误与选择器字段无关。选择文本字段时会自动调用becomeFirstResponder。因此,没有必要在此处调用它,因为单击文本字段时已经调用它。

基本上你告诉文本字段是活跃的,变得活跃...试一试,让我知道结果是什么。


关于未显示的选择器视图,请确保在使用故事板时正确连接IBOutlets,并在您.m文件的顶部编辑以下内容,如下所示:

在:

@interface TestViewController ()

后:

@interface TestViewController () <UIPickerViewDataSource, UIPickerViewDataSource>

答案 1 :(得分:1)

将你的pickerview作为你的第一响应者,使用

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

     if([textField isEqual:classField]) {

        [textField setInputView:picker]; //edited ones
        [picker becomeFirstResponder];

        }
    }

使用这种方法, 它会帮助你。

答案 2 :(得分:0)

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([self.classField isEditing]) {
        [self.picker selectRow:0 inComponent:0 animated:YES]; //This is not necessary but I prefer it for my purpose
        [self.picker reloadAllComponents];
    }
}

并确保您的TextField代理正在调用...方式<UITextFieldDelegate>必须位于您的.h文件中<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource> 希望这有助于你...

如果这不起作用,请尝试使用

    self.picker = [[UIPickerView alloc]initWithFrame:CGRectZero];
    [self.picker setDataSource:self];
    [self.picker setDelegate:self];
    [self.classField setInputView:self.picker]