How to create two picker views for two text boxes? - ios

I have created two UITextFields and want to create two different UIPickerViews for both the textFields with different values and how to map the individual textFields to each pickerView specifically?
any help ?

how about textField.inputview, to select the option for your respectively like this.
textField.inputView = pickerView1;
textField1.inputView = pickerView2;
If I write a code in this situation, then instead of creating two pickerView, I will create a one pickerview and change the datasource for each textfield

First of all create two array that contain different value that you want to display in the diff picker, we say aray1 and ary2 .
Now add this in .h file
#interface YourViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate, UIPickerViewDataSource>
{
UIToolbar* keyboardDoneButtonView;
UIPickerView *pickerView;
NSString * txtFieldSelected;
NSArray *ary2;
NSArray *ary1;
}
And in .h file
- (void)viewDidLoad
{
[super viewDidLoad];
firstTF.delegate = self;
secondTF.delegate = self;
ary1 =[NSArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8", nil];
ary2 =[NSArray arrayWithObjects:#"a",#"s",#"d",#"f",#"g",#"h",#"x",#"c", nil];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField == firstTF) {
txtFieldSelected = #"firstTF";
}else if (textField == secondFF) {
txtFieldSelected = #"secondTF";
}
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
keyboardDoneButtonView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
keyboardDoneButtonView.barStyle = UIBarStyleBlackTranslucent;
[keyboardDoneButtonView sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
[keyboardDoneButtonView setItems:barItems animated:YES];
textField.inputAccessoryView = keyboardDoneButtonView;
textField.inputView = pickerView;
return YES;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if (txtFieldSelected == #"firstTF") {
return ary1.count;
}else if (txtFieldSelected == #"secondTF") {
return ary2.count;
}
return 1;
}
-(void)doneButtonPressed:(id)sender{
if (txtFieldSelected == #"firstTF") {
[firstTF resignFirstResponder];
}else if (txtFieldSelected == #"secondTF") {
[secondTF resignFirstResponder];
}
}
-(void)cancelButtonPressed:(id)sender{
if (txtFieldSelected == #"firstTF") {
[firstTF resignFirstResponder];
}else if (txtFieldSelected == #"secondTF") {
[secondTF resignFirstResponder];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// called when a row is selected
if (txtFieldSelected == #"firstTF") {
firstTF.text = [ary1 objectAtIndex:row];
}else if (txtFieldSelected == #"secondTF") {
secondTF.text = [ary2 objectAtIndex:row];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (txtFieldSelected == #"firstTF") {
return [ary1 objectAtIndex:row];
}else if (txtFieldSelected == #"secondTF") {
return [ary2 objectAtIndex:row];
}
return #"Good Day";
}

textField1.inputView = pickerView1;
textField2.inputView = pickerView2;
pickerView.tag = 1;
pickerView.tag = 2;
Use UIPickerView delegate methods with tags.

Related

UItextfield with pickerview as firstresponder ios

I had a UITextField which when tapped need to show a UIPickerView. Also the data selected in pickerview will be inserted in the textfield. To Achiceve it I am now using subhajit's SHPickerField.
I do have created a subclass of UITextField, it is easy to implement and use. Here is the GitHub link:
Visit https://github.com/subhajitregor/SHPickerFieldExample
Please see the example to see how to use. The Readme file is also updated now.
Try this
let myPicker = UIPickerView()
#IBOutlet var myText: UITextField!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
myText.inputView = myPicker
}
#IBAction func textPiker(_ sender: UITextField) {
sender.inputView = myPicker
}
With the help of this code you can also open multiples text fields.
.h
UIPickerView *monthPickerView;
#property (strong, nonatomic)NSArray *monthArray;
#property (retain, nonatomic) IBOutlet UITextField *monthTextField;
.m
self.monthArray=[[NSArray alloc] initWithObjects:#"one",#"two", nil];
self.monthTextField.delegate=self;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self showPicker:textField];
else if(textField==_monthTextField){
if(self.monthTextField.text.length>=1){
[monthPickerView selectRow:[self.monthArray indexOfObject:self.monthTextField.text] inComponent:0 animated:NO];
}
}
return YES;
}
//**************show picker**************
- (IBAction)showPicker:(id)sender {
UITextField *textField=(UITextField *)sender;
monthPickerView = [[UIPickerView alloc] init];
monthPickerView.showsSelectionIndicator = YES;
monthPickerView.dataSource = self;
monthPickerView.delegate = self;
pickerToolbar = [[UIToolbar alloc] init];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
//to make the done button aligned to the right
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[pickerToolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
if(textField==_monthTextField){
monthPickerView.tag=;
self.monthTextField.inputView = monthPickerView;
self.monthTextField.inputAccessoryView = pickerToolbar;
}
}
-(void)doneClicked:(id) sender
{
[self.view endEditing:YES];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView.tag==1){
self.monthTextField.text = [_monthArray objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if(pickerView.tag==1){
return [_monthArray count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if(pickerView.tag==1){
return [_monthArray objectAtIndex:row];
}
return 0;
}

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 do I add a UIPickerView when tapping inside a text field?

I have this code to add a datepicker:
// Initiate datepicker and assign to due date
_datePicker = [[UIDatePicker alloc] init];
[_datePicker addTarget:self action:#selector(dateChanged:)
forControlEvents:UIControlEventValueChanged];
_datePicker.minuteInterval = 15;
_dueDate.inputView = _datePicker;
I tried to do the same thing with a pickerview but I had no success, could somebody explain why?
Here is my code:
// Initaite the pickerview and assign to priority text field
_priorityPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
// Array otions for priority picker
NSArray *array = [[NSArray alloc] initWithObjects:#"High",#"Medium",#"Low", nil];
self.priorityPickerData = array;
// Connect text field to pickerview
_priority.inputView = _priorityPickerView;
Here is the data set in the picker view:
// Picker View functions
-(NSInteger)numberOfComponantsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [_priorityPickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.priorityPickerData objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
int select = row;
if(select == 0){
_priority.text = #"High";
}else if(select == 1){
_priority.text = #"Medium";
}else if(select == 2){
_priority.text = #"Low";
}
}
Many thanks in advance,
Peter
Set the delegate and datasource to self:
_priorityPickerView.delegate = self;
_priorityPickerView.dataSource = self;
Also at the top of your .m file:
#interface YourViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

Why isn't my IBAction firing?

I have a custom class (FFFuelQuantityPickerVC) Storyboard View Controller that contains a UIPickerView and a UIButton. The UIButton is wired to an IBAction called fullButtonPressed in FFFuelQuantityPickerVC. When this View Controller is presented using a storyboard popover segue, the fullButtonPressed IBAction is fired when the UIButton is "touched up inside."
However, when I programmatically init and present FFFuelQuantityPickerVC in a popup, pressing the UIButton does not fire the IBAction. Why would that be? Here is the code that does the programmatic presentation when a button in a UITableViewCell is pressed. (self.poc is a reusable popover controller):
-(void)thisHelperPressed:(UIButton *)thisHelper inCell:(UITableViewCell *)thisCell{
if ([thisHelper.titleLabel.text isEqualToString:#"FuelPicker"]){
//init the fuel quantity picker VC
FFFuelQuantityPickerVC *fuelQuantityPickerVC;
fuelQuantityPickerVC =[[UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:nil]
instantiateViewControllerWithIdentifier:#"FuelQuantityPickerVC"];
fuelQuantityPickerVC.delegate = self;
[self.poc setContentViewController:fuelQuantityPickerVC];
} else if...
}
self.poc.delegate = self;
//present it
[self.poc presentPopoverFromRect:thisHelper.frame inView:thisCell permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
.
.
.
Here is FFFuelQuantityPickerVC:
// FFFuelQuantityPickerVC.h
#import <UIKit/UIKit.h>
#protocol fuelPickerDelegate <NSObject>
-(void) fuelQuantityChangedL:(NSInteger) quantityL R:(NSInteger)quantityR;
#end
#interface FFFuelQuantityPickerVC : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
#property (weak, nonatomic) IBOutlet UIPickerView *thisPicker;
#property (nonatomic) NSString *fuelQuantityL;
#property (nonatomic) NSString *fuelQuantityR;
#property (nonatomic) id delegate;
- (IBAction)fullButtonPressed; //this is wired to the button
#end
//
// FFFuelQuantityPickerVC.m
#import "FFFuelQuantityPickerVC.h"
#define FUEL_MIN 0
#define FUEL_MAX 146
#define LEFT_COMPONENT 0
#define RIGHT_COMPONENT 1
#interface FFFuelQuantityPickerVC ()
#end
#implementation FFFuelQuantityPickerVC
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.fuelQuantityL){
self.fuelQuantityL = [NSString stringWithFormat:#"%i", FUEL_MAX];
}
if (!self.fuelQuantityR){
self.fuelQuantityR = [NSString stringWithFormat:#"%i", FUEL_MAX];
}
}
//set selected row to current values, if any
- (void) viewDidAppear:(BOOL)animated {
[self.thisPicker selectRow:FUEL_MAX - [self.fuelQuantityL intValue] inComponent:LEFT_COMPONENT animated:YES];
[self.thisPicker selectRow:FUEL_MAX - [self.fuelQuantityR intValue] inComponent:RIGHT_COMPONENT animated:YES];
}
//this method does not get called when the button is pressed (except when the VC is presented via storyboard popover segue)
- (IBAction)fullButtonPressed {
self.fuelQuantityL = [NSString stringWithFormat:#"%i", FUEL_MAX];
self.fuelQuantityR = [NSString stringWithFormat:#"%i", FUEL_MAX];
[self.thisPicker selectRow:0 inComponent:0 animated:YES];
[self.thisPicker selectRow:0 inComponent:1 animated:YES];
[self.delegate fuelQuantityChangedL:[self.fuelQuantityL integerValue]
R:[self.fuelQuantityR integerValue]];
}
#pragma mark - PickerViewDataSource delegate methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return (FUEL_MAX-FUEL_MIN + 1);
}
#pragma mark - PickerView delegate methods
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
float myWidth = self.view.frame.size.width;
if (component == 0) return myWidth / 3;
return (myWidth * 2 / 3);
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
//TODO
return 28;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [NSString stringWithFormat:#"%d", (FUEL_MAX)-row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == LEFT_COMPONENT){
self.fuelQuantityL = [NSString stringWithFormat:#"%i", FUEL_MAX - row];
} else {
self.fuelQuantityR = [NSString stringWithFormat:#"%i", FUEL_MAX - row];
}
[self.delegate fuelQuantityChangedL:[self.fuelQuantityL integerValue]
R:[self.fuelQuantityR integerValue]];
}
#end
Here is my code. I m using this code for display the UIPopoverController when I click the cell and pick the value from UIPickerView to display the value in UILabel in my UITableviewCell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
if (indexPath.row == 1)
{
//PopoverView controller.........................................................................
UIViewController *popoverViewController = [[UIViewController alloc] init];
//PopoverView....................................................................................
UIView *popoverView = [[UIView alloc]init];
[popoverView setBackgroundColor:[UIColor blackColor]];
//Navigation bar and Barbutton items..............................................................
UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
navBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doDone)];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(doCancel)];
UINavigationItem *navItem = [[UINavigationItem alloc]init];
navItem.rightBarButtonItem = btnDone;
navItem.leftBarButtonItem = btnCancel;
navBar.items = [[NSArray alloc]initWithObjects:navItem, nil];
[popoverView addSubview:navBar];
//UIPickerView.....................................................................................
UIPickerView *sortPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 320, 180)];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(320, 230);
sortPickerView.delegate = self;
sortPickerView.dataSource = self;
sortPickerView.showsSelectionIndicator = YES;
[popoverView addSubview:sortPickerView];
NSUserDefaults *ud1 = [NSUserDefaults standardUserDefaults];
if ([ud1 objectForKey:#"languages"])
{
NSUInteger row1 = [languages indexOfObject:[ud1 objectForKey:#"languages"]];
if (row1)
{
[sortPickerView selectRow:row1 inComponent:0 animated:YES];
}
}
else
{
[sortPickerView selectRow:0 inComponent:0 animated:YES];
}
CGRect popFrame = lbl_language.frame;
popFrame.origin.y = popFrame.origin.y + popFrame.size.height + 70;
popFrame.origin.x = popFrame.origin.x + 100;
popoverViewController.view = popoverView;
self.SortPopover = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.SortPopover presentPopoverFromRect:popFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
}
For reasons that are unclear to me, the UI Button was not receiving the push - it never highlighted. It was not overlaid by another view that I could see (the only other view was the picker). Anyway, I added a UIToolbar and changed the button to a bar button item and it works fine.

Resources