PIcker View

Hi  everyone, here we are learning picker view.For that we are giving demo bellow.

Here we are giving short- simple and easy demo.


Step 1- Start Xcode and create single page application. Here product name giving             “PIckerViewDemo”.

Step 2-Now, go on ‘Main.storyboard’ and take objects from ‘Objects library’ picker.

here pickers are two types 
    1. Date Picker
    2.Picker View
We need ‘Picker View’ so that pick from object library and put on ‘Main.storyboard’.We learning about ‘Date Picker’ later.
Step 3- After that pick all objects same as show giving bellow type ‘UIImageView’,’TextField’ and ’Picker View’.
Step 4-Now give properties by right click press and drag and drop into ‘ViewController.h’ same as bellow type.
Step 5-Then give ‘datasource’ and ‘delegate’ by right click on picker view and drag on ‘ViewController’ here that is giving bellow.

Step 6- Now go on ‘ViewController.h’ and write bellow type properties also give datasource and delegate of picker.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate>
{
    
    IBOutlet UIImageView *imageView;
    IBOutlet UIPickerView *myPickerView;
    IBOutlet UITextField *nameTextField;
    IBOutlet UITextField *teamTextField;
    
    NSArray *photoArray;
    NSArray *teamArray;
    NSArray *nameArray;

}
@end
Step 7- Now we are going on ‘ViewController.m’ and write code into bellow type.


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{

    NSArray *pickerContent;

}
- (void)viewDidLoad {
    [super viewDidLoad];

    teamArray   = [[NSArray alloc]initWithObjects:@"Chess",
                   @"Cricket",@"Football",@"Tennis",@"Volleyball", nil];
    
    photoArray   = [[NSArray alloc]initWithObjects:@"Sachin-Tendulkar.jpg",@"Shahid-Afridi.jpg",@"shoaib-akhtar.jpg",@"MS-Dhoni.jpg",@"wasim-akram.jpg",@"Brian-Lara.jpg",@"Chris-Gayle.jpg",@"Lasith-Malinga.jpg",@"Muttiah-Muralidharan.jpg",@"brett-lee.jpg", nil];
    
    nameArray = [[NSArray alloc]initWithObjects:@"Sachin Tendulkar",@"Shahid Afridi",@"Shoaib Akhtar",@"M S Dhoni",@"Wasim Akram",@"Barin Lara",@"Chris Gayle",@"Lasith Malinga",@"Muttiah Murlidharan",@"Brett Lee", nil];

    pickerContent = [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"Sachin-Tendulkar.jpg"],     [UIImage imageNamed:@"Shahid-Afridi.jpg"],
                     [UIImage imageNamed:@"shoaib-akhtar.jpg"],
                     [UIImage imageNamed:@"MS-Dhoni.jpg"],
                     [UIImage imageNamed:@"wasim-akram.jpg"],
                     [UIImage imageNamed:@"Brian-Lara.jpg"],
                     [UIImage imageNamed:@"Chris-Gayle.jpg"],
                     [UIImage imageNamed:@"Lasith-Malinga.jpg"],
                     [UIImage imageNamed:@"Muttiah-Muralidharan.jpg"],
                     [UIImage imageNamed:@"brett-lee.jpg"],nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Picker View Data source
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    //Two columns
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    //set number of rows
    if(component== 0)
    {
        return photoArray.count;
    }
    else if(component== 1)
    {
        return [teamArray count];
    }
    else if(component == 2)
    {
        return [nameArray count];
    }
    return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if(component == 0)    {
        return [photoArray objectAtIndex:row];
    }
    else if(component == 1)
    {
        return [teamArray objectAtIndex:row];
    }
    else if(component == 2)
    {
        return [nameArray objectAtIndex:row];
    }
    return @"";
}

#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component==0)
        [imageView setImage:[UIImage imageNamed:[photoArray objectAtIndex:row]]];
    else if (component==1)
        [teamTextField setText:[teamArray objectAtIndex:row]];
    else if (component==2)
        [nameTextField setText:[nameArray objectAtIndex:row]];
}
@end
Here we are make return 3 because we are take 3 content on picker view those are image,team name and player name.
#pragma mark - Picker View Data source
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    //Two columns
    return 3;
}


Step 8-Now run your project then you show bellow type window.
Step 9-Here when you pick image, team name and player name then your simulator will be made bellow type.


If you can't understand so go to GitHub demo .

Otherwise if you are tire So go to fun HERE.





Police & Law Enforcement





Comments

Popular posts from this blog

Dera Sacha Sauda chief Gurmeet Ram Rahim Singh - News Upword