3

so I've been trying to get this pickerview working but I just can't 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 is correct, so that's not the 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];
}

Thanks.

user3347446
  • 133
  • 2
  • 9

3 Answers3

1

Try removing [textField becomeFirstResponder]; from the following method:

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

The error is not relating to the picker field. The becomeFirstResponder is called automatically when the text field is selected. So there is no need for it to be called here as it would have already been called when you clicked the text field.

Basically your telling the text field that is active, to become active... Give it a go and let me know what the result is.


In relation to the picker view not showing up, make sure you have the IBOutlets connected up properly if using storyboards, also edit the following at the top of you .m file so it looks like the below:

Before:

@interface TestViewController ()

After:

@interface TestViewController () <UIPickerViewDataSource, UIPickerViewDataSource>
Rob
  • 1,233
  • 13
  • 24
  • Yes it works, but the pickerView is not shown and that;s where I'm stuck at right now – user3347446 Aug 18 '14 at 10:58
  • Can you update your question to show your .h file please? Make sure you have your IBOutlets connected properly, also I want to ensure you have set the in your .h file as they are not specified in the .m file above. – Rob Aug 18 '14 at 15:23
1

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.

sreekanthk
  • 362
  • 2
  • 21
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];
    }
}

And Make sure your TextField Delegate is calling ... Means <UITextFieldDelegate> must be in your .h file like <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource> Hope This Helps You...

If this doesn't work then Try this

    self.picker = [[UIPickerView alloc]initWithFrame:CGRectZero];
    [self.picker setDataSource:self];
    [self.picker setDelegate:self];
    [self.classField setInputView:self.picker] 
Mubin Mall
  • 546
  • 10
  • 25