Set a UIPickerView row when loading a View? HOW?

Discussion in 'iOS Programming' started by ethana, Aug 11, 2008.

  1. ethana macrumors 6502a

    Joined:
    Jul 17, 2008
    Location:
    Seattle, WA
    #1
    How do I set a UIPickerView row when loading a View? I've tried but it's just not working. The row is always set to 0 when the View appears. By the way, I call "self" because I set the View to be the <UIPickerViewDelegate>.

    Here's what my code is currently:

    Code:
    - (void)viewWillAppear:(BOOL)animated {
    	[self selectRow:2 inComponent:0 animated:YES];
    }
    
    - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated {
    	[self reloadAllComponents];
    }
    
    - (void)reloadAllComponents {
    }
    
    Please help!

    Thanks.

    Ethan
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    self is a pointer to the object that method belongs to. So if your object is not the UIPickerView or subclass then this won't work. You need a pointer to the UIPickerView instance.
     
  3. ethana thread starter macrumors 6502a

    Joined:
    Jul 17, 2008
    Location:
    Seattle, WA
    #3
    How do I get the instance of it? I just dragged it over on Interface Builder, so it's not like I created it programmatically.
     
  4. DipDog3 macrumors 65816

    DipDog3

    Joined:
    Sep 20, 2002
    #4
    You need to add an instance variable to your View Controller file to start.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Well if you are using IB then you'd add an outlet to the class and connect that to the picker.
     
  6. ethana thread starter macrumors 6502a

    Joined:
    Jul 17, 2008
    Location:
    Seattle, WA
    #6
    I've tried both creating an instance of the View Controller as well as creating an Outlet for the PickerView and then making an instance of that, but neither has worked.

    Can someone post some quick working code for this functionality? I am at a loss as to why this isn't working for me.

    Thanks,

    Ethan
     
  7. rahulvyas macrumors newbie

    Joined:
    May 6, 2009
    #7
    here how it works....

    declare an instance variable in your header file when you are using interface builder like this

    Code:
    IBOutlet UIPickerView *AlarmSoundPicker;
    in your viewDidLoad or viewWillAppear Methods put this line
    Code:
    [AlarmSoundPicker selectRow:Rowindex inComponent:0 animated:YES];
    where Rowindex = row number of picker starts from 0 to the length you have provided.
     

Share This Page