Search code examples
selectdefaultuipickerview

UIPickerView select value when View is loaded


So I implemented a PickerView and everything works fine when I pick some value, but if I don't pick anything the

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

is not called. And I want if user doesn't pick a value, the first value is picked.

So I implemented:

-(void)viewDidAppear:(BOOL)animated
{
    [pickerView selectRow:0 inComponent:0 animated:YES];
}

but it doesn't work. Why isn't selectRow:inComponent: method called after this?


Solution

  • Make a new function to be called when you start. If you want the code input to be read when you start the app, you need to put it under the viewDidLoad

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    [updateWheel];
    {
        [super viewDidLoad];
        }
    
     #Pragma Mark pickerWheel Delegate
    - (void)updateWheel
    {NSString *wheel1 = [wheel1 objectAtIndex:[picker selectedRowInComponent:0]];
    aLabelUWantToHaveOutputIn.text = wheel1; 
    } //Whatever code you want to happen
    

    If this doesn't help, take a look at my project which uses some of the same mechanics. http://pastebin.com/WLGq3jvv