Not able to use two UIPickerView in the same Controller - ios

I'm trying to use more than two UIPickerViews together in one ViewController. Each UIPickerView has different data array. I am using interface builder to link the pickers up. What I want is, after button click, my PickerView will show up and user can select the data from it.That selected data will come in the text field. It is working fine for first PickerView but when I click for the second PickerView, the value is coming from first array and I am not able to write condition. Here's all my code:
.h file
#import <UIKit/UIKit.h>
#interface Tabbar2ViewController : UIViewController<UITextViewDelegate,UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>{
IBOutlet UIPickerView *genderPickerView;
IBOutlet UIPickerView *pickerViewoption;
IBOutlet UIButton *BrandPickerBtn;
IBOutlet UIButton *genderPickerfBtn;
NSArray *brands;
NSArray *gender;
BOOL isCheckedpickerView;
IBOutlet UILabel *BrandLabelfield;
IBOutlet UILabel *GenderLabelfield;
}
- (IBAction)action:(id)sender;
- (IBAction)genderAction:(id)sender;
#property(nonatomic,retain)NSArray *brands;
#property(nonatomic,retain)NSArray *gender;
#end
.m file
#import "ttViewController.h"
#interface ttViewController ()
#end
#implementation ttViewController
#synthesize countries,size,brands,gender,activity,condition,color;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
isCheckedpickerView=FALSE;
pickerViewoption.hidden=YES;
genderPickerView.hidden=YES;
activityPickerView.hidden=YES;
conditionPickerView.hidden=YES;
sizeandquantityPickerView.hidden=YES;
BrandLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 812, 253, 35)];
BrandLabelfield.textColor = [UIColor lightGrayColor];
BrandLabelfield.text=#"Brand";
BrandLabelfield.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"header_01.png"]];
BrandLabelfield.font = [UIFont fontWithName:#"Open Sans" size:14];
[tabView1 addSubview:BrandLabelfield];
//GenderTextfield with Label //
GenderLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 865, 253, 35)];
GenderLabelfield.textColor = [UIColor lightGrayColor];
GenderLabelfield.text=#"Gender";
GenderLabelfield.font = [UIFont fontWithName:#"Open Sans" size:14];
[tabView1 addSubview:GenderLabelfield];
//ColorTextfield with Label //
ConditionLabelfield = [[UILabel alloc] initWithFrame:CGRectMake(20, 1113, 253, 35)];
ConditionLabelfield.textColor = [UIColor lightGrayColor];
ConditionLabelfield.text=#"Color";
ConditionLabelfield.font = [UIFont fontWithName:#"Open Sans" size:14];
[tabView1 addSubview:ConditionLabelfield];
brands=[[NSArray alloc]initWithObjects:#"Nike",#"Bata",#"Adidas", nil];
gender=[[NSArray alloc]initWithObjects:#"Male",#"Female",#"Unisex", nil];
activity=[[NSArray alloc]initWithObjects:#"BASEBALL",#"BASKETBALL",#"FOOTBALL",#"GOLF",#"RUNNING",#"SKATING",#"SOCCER",#"SPORTS & OUTDOOR",#"TENNIS",#"TRAINING",#"VOLLEY BALL",#"WRESTLING", nil];
// Custom PICKER VIEWS //
pickerViewoption=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 852, 290, 41)];
pickerViewoption.showsSelectionIndicator = YES;
pickerViewoption.delegate = self;
pickerViewoption.dataSource=self;
pickerViewoption.hidden=YES;
[pickerViewoption setBackgroundColor:[UIColor lightGrayColor]];
[tabView1 addSubview:pickerViewoption];
genderPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 906, 290, 41)];
genderPickerView.showsSelectionIndicator = YES;
genderPickerView.delegate = self;
genderPickerView.dataSource=self;
genderPickerView.hidden=YES;
[genderPickerView setBackgroundColor:[UIColor lightGrayColor]];
[tabView1 addSubview:genderPickerView];
activityPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(13, 954, 290, 41)];
activityPickerView.showsSelectionIndicator = YES;
activityPickerView.delegate = self;
activityPickerView.dataSource=self;
activityPickerView.hidden=YES;
[activityPickerView setBackgroundColor:[UIColor lightGrayColor]];
[tabView1 addSubview:activityPickerView];
pickerViewoption.tag=1;
genderPickerView.tag=2;
activityPickerView.tag=3;
BrandPickerBtn.tag=000;
genderPickerfBtn.tag=111;
activityPickerBtn.tag=222;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component{
NSString *string;
if (pickerViewoption.tag==1) {
[tabView1 addSubview:pickerViewoption];
string=[self.brands objectAtIndex:row];
}
else {
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
else if (genderPickerView.tag==2){
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
else if (activityPickerView.tag==3){
[tabView1 addSubview:activityPickerView];
string=[self.activity objectAtIndex:row];
}
return string;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if (pickerViewoption.tag==1){
BrandLabelfield.text=#"";
BrandLabelfield.text=[brands objectAtIndex:row];
BrandLabelfield.textColor=[UIColor whiteColor];
pickerViewoption.hidden=YES;
}
else if (genderPickerView.tag==2){
GenderLabelfield.text=#"";
GenderLabelfield.text=[gender objectAtIndex:row];
GenderLabelfield.textColor=[UIColor whiteColor];}
else if (activityPickerView.tag==3){
ActivityLabelfield.text=#"";
ActivityLabelfield.text=[activity objectAtIndex:row];
ActivityLabelfield.textColor=[UIColor whiteColor];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
if (pickerViewoption.tag==1)
{
return [brands count];
}
else if (genderPickerView.tag==2)
{
return [gender count];
}
else if (activityPickerView.tag==3)
{
return [activity count];
}
return 0;
}
- (IBAction)action:(id)sender; {
if (isCheckedpickerView==FALSE)
{
pickerViewoption.hidden=NO;
isCheckedpickerView=TRUE;
}
else
{
isCheckedpickerView=FALSE;
pickerViewoption.hidden=YES;
}
}
- (IBAction)genderAction:(id)sender {
if (isCheckedpickerView==FALSE)
{
genderPickerView.hidden=NO;
isCheckedpickerView=TRUE;
}
else
{
isCheckedpickerView=FALSE;
genderPickerView.hidden=YES;
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Currently you are checking wrong condition in delegate methods, replace following 3 methods in .m file :
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component{
NSString *string;
if (pickerView.tag==1) {
[tabView1 addSubview:pickerViewoption];
string=[self.brands objectAtIndex:row];
}
else if (pickerView.tag==2){
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
else if (pickerView.tag==3){
[tabView1 addSubview:activityPickerView];
string=[self.activity objectAtIndex:row];
}
else {
[tabView1 addSubview:genderPickerView];
string=[self.gender objectAtIndex:row];
}
return string;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if (thePickerView.tag==1){
BrandLabelfield.text=#"";
BrandLabelfield.text=[brands objectAtIndex:row];
BrandLabelfield.textColor=[UIColor whiteColor];
pickerViewoption.hidden=YES;
}
else if (thePickerView.tag==2){
GenderLabelfield.text=#"";
GenderLabelfield.text=[gender objectAtIndex:row];
GenderLabelfield.textColor=[UIColor whiteColor];
}
else if (thePickerView.tag==3){
ActivityLabelfield.text=#"";
ActivityLabelfield.text=[activity objectAtIndex:row];
ActivityLabelfield.textColor=[UIColor whiteColor];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
if (pickerView.tag==1)
{
return [brands count];
}
else if (pickerView.tag==2)
{
return [gender count];
}
else if (pickerView.tag==3)
{
return [activity count];
}
return 0;
}

Related

How to prevent UIPickerView from closing when the user selects a field?

I am trying to prevent my UIPickerview from closing when a field is selected by a user. I want the user to be able to select a field without UIPickerview dismissing automatically. I have tried so many things such as trying the following but none of them helped:
_txtfield.hidden=NO;
[_pickerView endEditing:NO];
[pickerView endEditing:NO];
[_txtfield endEditing:NO];
[self endEditing:NO];
_pickerView.hidden=NO;
-----------------Here is more code----------------
#implementation FieldWithPickerView {
void(^pickerCallback)(NSInteger row);
CGRect myFrame;
}
-(void)viewDidAppear{
}
-(void)commonInit:(CGRect)frame{
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[view]|" options:0 metrics:nil views:#{#"view":self.InputViewPicker}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|" options:0 metrics:nil views:#{#"view":self.InputViewPicker}]];
[self.layer setCornerRadius:10.0f];
[self setClipsToBounds:YES];
_pickerView = [[UIPickerView alloc] init];
_pickerView.delegate=self;
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(removePicker)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[self.txtfield setInputAccessoryView:toolBar];
self.txtfield.inputView = _pickerView;
[_txtfield.inputView setBackgroundColor:[UIColor clearColor]];
[_txtfield setBackgroundColor:[UIColor clearColor]];
_txtfield.text=#"--Select---";
_label_view.textColor=[UIColor whiteColor];
_label_view.backgroundColor=SECURUSBLUE;
_label_view.textAlignment = NSTextAlignmentCenter;
_txtfield.textAlignment = NSTextAlignmentCenter;
}
-(void)removePicker
{
[_txtfield resignFirstResponder];
}
-(void)pickerListenner:(void(^)(NSInteger row))handler
{
pickerCallback=handler;
}
- (id)initWithFrame:(CGRect)frame
{
// NSLog(#"initwithframe picker");
self = [super initWithFrame:frame];
if (self)
{
[self commonInit:frame];
}
return self;
}
#pragma mark - Picker View Data source
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [_pickerData count];;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if(_pickerData.count == 0)
return #"There is nothing";
return [_pickerData objectAtIndex:row];
}
#pragma mark -
#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// Code logic
NSLog(#"selected row --->%ld!!!!",(long)row);
pickerCallback(row);
// _txtfield.text=[_pickerData objectAtIndex:row];
_txtfield.hidden=NO;
}
- (NSInteger)selectedRowInComponent:(NSInteger)component
{
return component;
}
You need to implement the UIPickerView delegate methods, so add <UIPickerViewDelegate> to your ViewController.
in your viewDidLoad method conform to the protocol like so:
pickerView.delegate = self;
Then add the following method to detect when a row has been selected:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// do nothing because it will not dismiss
}
You will need to implement the numberOfRowsInComponent, numberOfComponents, and titleForRow delegate methods as well, but once those are implemented your picker view should not dismiss when a row is selected. You will need to manually dismiss the picker view in your didSelectRow delegate method.

While using picker view

enter image description here $ The data of assigned array is not showing in the pickerView. What mistake I am doing as I haven't being able to identify my mistake.
Its .m file code:
// PickerViewController.m
// dropDownButtonTry
//
#import "PickerViewController.h"
#interface PickerViewController ()
{
NSArray *genderArray;
NSArray *cityArray;
NSArray *currentArray;
UITextField *currentTextField;
}
#end
#implementation PickerViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
cityArray = [[NSArray alloc]initWithObjects:#"Delhi",#"Mumbai",#"Chennai", nil];
genderArray = [[NSArray alloc]initWithObjects:#"Male",#"Female",#"Transgender", nil];
self.pickerView.hidden = YES;
self.btnDoneOutlet.hidden = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
//Needed to prevent keyboard from opening
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
currentTextField = textField;
if (textField == self.textFieldGenderOutlet) {
currentArray = genderArray;
}
if (textField == self.textFieldCityOutlet) {
currentArray = cityArray;
}
// do here everything you want
NSLog(#"Pressed on TextField!");
self.pickerView.hidden = NO;
self.btnDoneOutlet.hidden = NO;
[self.view endEditing:YES]; // Hide keyboard
NSLog(#"****current array**** %#",currentArray);
return NO;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [currentArray count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[currentTextField setText:[currentArray objectAtIndex:row]];
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [currentArray objectAtIndex:row];
}
- (IBAction)textFieldGenderAction:(id)sender {
self.pickerView.hidden = NO;
self.btnDoneOutlet.hidden = NO;
}
- (IBAction)btnDone:(id)sender {
self.pickerView.hidden = YES;
self.btnDoneOutlet.hidden = YES;
}
- (IBAction)textFieldCityAction:(id)sender {
}
#end
You need to reload picker view :
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
currentTextField = textField;
if (textField == self.textFieldGenderOutlet) {
currentArray = genderArray;
}
if (textField == self.textFieldCityOutlet) {
currentArray = cityArray;
}
// do here everything you want
NSLog(#"Pressed on TextField!");
self.pickerView.hidden = NO;
self.btnDoneOutlet.hidden = NO;
[self.thePicker reloadAllComponents];
[self.view endEditing:YES]; // Hide keyboard
NSLog(#"****current array**** %#",currentArray);
return NO;
}
currentArray is assigned value in textFieldShouldBeginEditing but it is not initialized anywhere in your code.
Just initialize currentArray in viewDidLoad like below:
currentArray = [NSArray new];

Using UIPickerView multiple times. iOS Objective-C

I'm a beginner in iOS Objective-C programming so first of all - sorry for being silly.
I'm using UIPickerView to fill UITextField and it works perfectly for the first use.
photo
Lets assume that user made a mistake using pickerview for the first time. He wants to correct this and opens the same pickerview again. Pickerview is empty.
photo
Also okayButton is missing. What am I doing wrong?
Here's my code:
#import "LoginViewController.h"
#interface LoginViewController ()
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
marksArray =[[NSMutableArray alloc] init];
fuelTypeArray =[[NSMutableArray alloc] init];
inputArray =[[NSMutableArray alloc] init];
marksArray = [NSMutableArray arrayWithObjects:#"Alfa Romeo", #"Aston Martin", #"BMW", #"Cadillac", #"Chevrolet", #"Chrysler", #"Citroen", #"Dacia", #"Daewoo", #"Dodge", #"Ferrari", #"Ford", #"Honda", #"Hummer", #"Hyundai", #"Infiniti", #"Jaguar", #"Jeep", #"Kia", #"Lamborghini", #"Lancia", #"Land Rover", #"Lexus", #"Maserati", #"Mazda", #"Mercedes-Benz", #"Mini", #"Mitsubishi", #"Nissan", #"Opel", #"Peugeot", #"Polonez", #"Pontiac", #"Porshe", #"Renault", #"Rover", #"Saab", #"Seat", #"Skoda", #"Smart", #"Subaru", #"Suzuki", #"Toyota", #"Volkswagen", #"Volvo", nil];
fuelTypeArray = [NSMutableArray arrayWithObjects:#"Benzyna", #"Benzyna+LPG", #"Diesel", #"Elektryczny", #"Hybryda", nil];
okayImageView.hidden = true;
okayButton.hidden= true;
//makes fuelTypePickerView as input console for markTextField
fuelTypePickerView.hidden = true;
fuelTypePickerView = [[UIPickerView alloc] init];
fuelTypePickerView.delegate = self;
fuelTypePickerView.showsSelectionIndicator = YES;
fuelTypeTextField.inputView = fuelTypePickerView;
//makes markPickerView as input console for markTextField
markPickerView.hidden = true;
markPickerView = [[UIPickerView alloc] init];
markPickerView.delegate = self;
markPickerView.showsSelectionIndicator = YES;
markTextField.inputView = markPickerView;
// scrolling login view controller
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 615)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
okayImageView.hidden = false;
okayButton.hidden= false;
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if([pickerView isEqual: markPickerView]){
return 45;
}else if([pickerView isEqual: fuelTypePickerView]){
return 5;
}else{
return 0;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if([pickerView isEqual: markPickerView]){
self->inputArray = self->marksArray;
return inputArray[row];
}else if([pickerView isEqual: fuelTypePickerView]){
self->inputArray = self->fuelTypeArray;
return inputArray[row];
}else{
return 0;
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if([pickerView isEqual: markPickerView]){
markTextField.text = [inputArray objectAtIndex:row];
}else if([pickerView isEqual: fuelTypePickerView]){
fuelTypeTextField.text = [inputArray objectAtIndex:row];
}
}
-(IBAction) okayButtonPressed:(id)sender{
if(markPickerView.hidden!=YES){
okayImageView.hidden = true;
okayButton.hidden= true;
markPickerView.hidden=YES;
[markTextField resignFirstResponder];
}else if(fuelTypePickerView.hidden!=YES){
okayImageView.hidden = true;
okayButton.hidden= true;
fuelTypePickerView.hidden=YES;
[fuelTypeTextField resignFirstResponder];
}
}
#end
Thanks!
Try adding the following lines in your code
#interface LoginViewController () <UITextFieldDelegate>
then in viewDidLoad
fuelTypeTextField.delegate = self;
markTextField.delegate = self;
Now implement UITextField Delegate in your LoginViewController
-(void)textFieldDidBeginEditing:(UITextField *)sender{
okayImageView.hidden = NO;
okayButton.hidden = NO;
if([sender isEqual:fuelTypeTextField])
{
fuelTypePickerView.hidden = NO;
}
else{
markPickerView.hidden = NO;
}
}
In your Code after click on OK button you are hiding the PickerView, ImageView and Button. But on next time editing the textField you are not unhiding them thats why they are not shown.

PickerView is not dismissing when Done button is pressed

I have created a text field that upon entry will open a picker view with a toolbar that contains a done button. However, when the done button is pressed the picker view doesn't dismiss. Everything else works just as I want except this. I've tried several options to no avail. Please review and let me know what I'm missing.
My code is below:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{IBOutlet UITextField *productDescription; IBOutlet UIPickerView *productPicker; NSArray *productListArray}
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)addPickerView{
productListArray = [[NSArray alloc]initWithObjects:
#"myArray", nil];
productDescription.delegate = self;
[self.view addSubview:productDescription];
[productDescription setPlaceholder:#"Product Description"];
productPicker = [[UIPickerView alloc]init];
productPicker.dataSource = self;
productPicker.delegate = self;
productPicker.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self action:#selector(resignFirstResponder)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(50, 320, 50, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
productDescription.inputView = productPicker;
productDescription.inputAccessoryView = toolBar;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self addPickerView];
}
#pragma mark - Text field delegates
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
([textField.text isEqualToString:#""]);
}
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [productListArray count];
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
[productDescription setText:[productListArray objectAtIndex:row]];
}
- (void)doneButton:(UIBarButtonItem *)sender{
NSLog(#"Done Touched");
[productPicker setHidden:YES];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
(NSInteger)row forComponent:(NSInteger)component{
return [productListArray objectAtIndex:row];
}
#end
.M File
Xib file in take Textfield and set delegate with connect.
#import "YourViewController.h"
#interface YourViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
UIPickerView *productPicker;
NSArray *productListArray;
IBOutlet UITextField *productDescription;
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
[self addPickerView];
}
-(void)addPickerView
{
productListArray = [[NSArray alloc]initWithObjects:#"myArray",#"Rohit",#"Change",#"Your view", nil];
[productDescription setPlaceholder:#"Product Description"];
productPicker = [[UIPickerView alloc]init];
productPicker.dataSource = self;
productPicker.delegate = self;
productPicker.showsSelectionIndicator = YES;
UIToolbar* toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;
toolBar.tintColor = nil;
[toolBar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButton:)];
[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];
productDescription.inputView = productPicker;
productDescription.inputAccessoryView = toolBar;
}
- (IBAction)doneButton:(id)sender
{
NSLog(#"Done Touched");
[productPicker removeFromSuperview];
[productPicker resignFirstResponder];
[self.view endEditing:YES];
}
#pragma mark - Text field delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField {
productDescription.inputView = productPicker;
}
#pragma mark - Text field delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField {
productDescription.inputView = productPicker;
}
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [productListArray count];
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[productDescription setText:[productListArray objectAtIndex:row]];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [productListArray objectAtIndex:row];
}
I hope this will help you great.

how to disable rolling effect and shadow effect in UIPICKER VIEW in ios?

i am developing the one example of UIPICKERVIEW and i want to disable or off the shadow effect and rolling effect but not able to do it.do you have any idea for that ?
Source code:
pickerViewController.h file
#import <UIKit/UIKit.h>
#interface pickerViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
{
UIPickerView *languageSelect;
NSMutableArray *pickerData;
}
#property (nonatomic, retain) UIPickerView *languageSelect;
#property (nonatomic, retain) NSArray *pickerData;
#end
pickerViewController.m file
#import "pickerViewController.h"
#interface pickerViewController ()
#end
#implementation pickerViewController
#synthesize languageSelect, pickerData;
- (void)viewDidLoad
{
[super viewDidLoad];
pickerData= [[NSMutableArray alloc] initWithObjects:#"English",#"Spanish",#"French",#"Greek",
#"Japaneese",#"Korean",#"Hindi",#"English",#"Spanish",#"French",#"Greek",
#"Japaneese",#"Korean",#"Hindi", nil];
languageSelect = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 50, 300, 1000)];
languageSelect.showsSelectionIndicator = YES;
languageSelect.hidden = NO;
languageSelect.delegate = self;
[self.view addSubview:languageSelect];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [pickerData count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
NSString* labelText = [NSString stringWithFormat:#"%#",[pickerData objectAtIndex:row]];
if (pickerLabel == nil) {
CGSize size = [labelText sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(250, 216) lineBreakMode:NSLineBreakByCharWrapping];
CGRect frame = CGRectMake(0.0, 0.0, 250, size.height + 25);
pickerLabel = [[UILabel alloc] initWithFrame:frame];
[pickerLabel setTextAlignment:NSTextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
[pickerLabel setLineBreakMode:NSLineBreakByWordWrapping];
// trying to mess with the UIView of the row itself... to no avail
//CGRect rowFrame = view.frame;
//NSLog(#"%f",rowFrame.size.height);
//rowFrame.size.height = size.height + 25;
//view.frame = rowFrame;
}
[pickerLabel setText:[pickerData objectAtIndex:row]];
//NSLog(#"%f",[pickerView rowSizeForComponent:component].height);
return pickerLabel;
}
-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
}
Output:
I want to do something like this...!!
This is a lot of custom work. If you want to achieve that look, you are probably better of trying to use a UITableView. You'll still have to put in quite some effort though.
In pic you can see structure of UIPickerView.
http://i.stack.imgur.com/QFAKV.png
Maybe removing some layers help you
http://i.stack.imgur.com/C2xDO.png

Resources