Crash on textfield becomes first responder with pickerview

so i am trying to run this pickerview but i just cannot get it to work. When I run the app, it crashes without any stacktrace and shows Thread 1: EXC_BAD_ACCESS on [textField becomeFirstResponder]. The pickerArray rule is correct, so the problem is not a problem.

#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];
}

      

Thank.

+3


source to share


3 answers


Try to remove [textField becomeFirstResponder];

from the following method:

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

      

The error is not related to the select field. becomeFirstResponder

is called automatically when a text field is selected. So there is no need for it to be called here, as it would already have been displayed when you clicked the textbox.

Basically, you are telling the active active text field to become active ... Let it know and let me know what the result is.




Due to the selection view not showing, please make sure the IBOutlets are properly connected, if you are using storyboards, also edit the following at the top of the .m file so that it looks like this:

Before:

@interface TestViewController ()

      

After:

@interface TestViewController () <UIPickerViewDataSource, UIPickerViewDataSource>

      

+1


source


to have ur pickerview as your first responder use

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

     if([textField isEqual:classField]) {

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

        }
    }

      



use this method, it will help you.

+1


source


-(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];
    }
}

      

And make sure your TextField delegate calls ... The values <UITextFieldDelegate>

should be in your .h file, for example <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>

Hope this helps you ...

If it doesn't work try this

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

      

0


source







All Articles