Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Picker View

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 288
    Comment on it

    A picker view is a funky way to allow users to select one item from a group of items. It consists of a rotating wheel which contains the given options. The user can turn the wheel up or down by swiping motion and the option in front of the wheel will be selected. The picker view can be used to make the app funky and can provide an alternative to boring drop down lists.

    The UIPicekrView makes use of an array to populate the options on the wheel and iOS provides the delegates and data source methods to make the operations easy to implement and use.

    The first step is to include the UIPickerVeiwDataSource and UIPickerViewDelegates and define the picker view and the array in the .h file of the view controller.

     IBOutlet UIPickerView *myPicker;
        NSArray *nameArray;
    

    Moving on to the .m file of the view controller. In ViewDidLoad populate the array with objects that have to be displayed in the wheel of the picker.

     nameArray = [NSArray arrayWithObjects:@"India",@"Japan",@"korea",@"china",@"Africa",@"Austraila",@"America",@"Zimbabwe",@"Nigeria",@"UAE",@"Indonesia",@"Thailand",@"England", nil];
    

    And then use the UIPickerView's Data Source methods.

    #pragma mark picker view data source methods
    
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        return [nameArray count];
    }
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 1;
    }
    
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        // use the selected option to update a text field or a label 
    }
    
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [nameArray objectAtIndex:row];
    }
    

    As said above the UIPicker can be used just like the drop down lists to update a label or a text field and is different to look at. The programmer can even modify the picker and use more than one column with related data, so that the user can select the correct information without a keyboard. A more advanced form of the picker view is the UIDatePicker, which allows the user to input date and time in correct format using a combination of multiple rotating wheels.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: