My UIPickerView has 3 components and there are three labels underneath. When an answer is chosen for 1 component of the picker I'd like the first label to show that components answer. Then the second and third component be shown on the 2nd and 3rd label. However it is currently not working as I thought, any suggestions?
I'm thinking my issue lies in the "-(void)pickerView:(UIPickerView *)pickerView didSelectRow:" section
#import "ViewController.h"
#interface ViewController2 : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
#property (weak, nonatomic) IBOutlet UIPickerView *picker1;
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#property (weak, nonatomic) IBOutlet UILabel *label3;
#end
and:
import "ViewController2.h"
#interface ViewController2 ()
{
NSArray*othercolourArray;
NSArray*otherseasonArray;
NSArray*otherotherArray;
}
#end
#implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
othercolourArray =[[NSArray alloc]initWithObjects:#"Health Prof", #"Housekeep", #"Visitors", #"Other",nil];
otherseasonArray =[[NSArray alloc]initWithObjects:#"Midwife", #"Nurse", #"Doctor",#"Other", nil];
otherotherArray =[[NSArray alloc]initWithObjects:#"Known Midwife", #"New Midwife", #"Doctor",#"Other", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (component) {
case 0:
return othercolourArray.count;
break;
case 1:
return otherseasonArray.count;
break;
case 2:
return otherotherArray.count;
break;
default:
break;
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// This method provides the data for a specific row in a specific component.
switch (component) {
case 0:
return [othercolourArray objectAtIndex:row];
break;
case 1:
return [otherseasonArray objectAtIndex:row];
break;
case 2:
return [otherotherArray objectAtIndex:row];
break;
default:
break;
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
_label1.text = [othercolourArray objectAtIndex: row];
_label2.text = [otherseasonArray objectAtIndex: row ];
_label3.text = [otherotherArray objectAtIndex: row ];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Your implementation of pickerView:didSelectRow:inComponent needs a bit more logic added I think. At the moment it looks like it'll switch the text for all labels depending on the last component-row combination. From reading your description, it looks like you need to set the label based on the row selected in a specific component. Maybe the below is what you're after.
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (component) {
case 0:
_label1.text = [othercolourArray objectAtIndex: row];
break;
case 1:
_label2.text = [otherseasonArray objectAtIndex: row];
break;
case 2:
_label3.text = [otherotherArray objectAtIndex: row];
break;
default:
break;
}
}
I found the problem, just needed to replace:
_label1.text = [othercolourArray objectAtIndex: row];
with
_label1.text = [othercolourArray objectAtIndex: [pickerView selectedRowInComponent:0]];
and so on with the other components, thanks anyways !
Related
So I've followed everything this post Default Picker Value iphone says, but I can't seem to get my picker to work.
Here's my .m snippet:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.arrPercent = #[#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24",#"25",#"26",#"27",#"28",#"29",#"30",#"31",#"32",#"33",#"34",#"35",#"36",#"37",#"38",#"39",#"40",#"41",#"42",#"43",#"44",#"45",#"46",#"47",#"48",#"49",#"50"];
self.arrPeople = #[#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"];
self.percent = [NSNumber numberWithInt:15];
self.people = [NSNumber numberWithInt:5];
self.strSubTotal = #"";
self.myPicker.dataSource = self;
self.myPicker.delegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//[self.myPicker selectRow:self.percent.integerValue inComponent:0 animated:YES];
[self.myPicker selectRow:5 inComponent:0 animated:YES];
[self.myPicker selectRow:5 inComponent:1 animated:YES];
[self.myPicker reloadAllComponents]; // tried commenting out this line with no luck
}
Here's my .h snippet:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface ViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>
#property (nonatomic,strong) NSArray *arrPercent;
#property (nonatomic,strong) NSArray *arrPeople;
#property (strong, nonatomic) IBOutlet UIPickerView *myPicker;
#property (strong, nonatomic) NSString *strSubTotal;
#property (strong, nonatomic) NSNumber *percent;
#property (strong, nonatomic) NSNumber *people;
#end
Here's the delegate stuff, I think...
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if(component== 0)
{
return [self.arrPercent count];
}
else
{
return [self.arrPeople count];
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
return [NSString stringWithFormat:#"%#%#", [self.arrPercent objectAtIndex:row], #"%"];
}
else
{
return [self.arrPeople objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == 0)
{
self.percent = [self.arrPercent objectAtIndex:row];
}
else
{
self.people = [self.arrPeople objectAtIndex:row];
}
[self updateSubTotal:-3];
}
When I start the emulator, the picker doesn't animate to the position I tell it to. Am I doing anything wrong? Please help!
As it turns out, the error was that for some reason, the connection between the picker in the UI was disconnected with the code. I had to hold the Control key while dragging the picker from the UI to the .h file.
I saw this because I noticed a little "empty button icon" next to the method declaration in the .h file. Once Xcode understood that the picker is controlled by the piece of code, everything works as expected.
Thank you so much!
I'm trying to bring up a picker view instead of a keyboard to put in a textfield, but my picker view doesn't show.
#import "SettingsViewController.h"
#import "FindClasses.h"
#interface SettingsViewController ()
#property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
#property (weak, nonatomic) IBOutlet UITextField *classTextField;
#property (strong, nonatomic) UIPickerView *picker;
#property (nonatomic, strong) FindClasses *finder;
#property (nonatomic, strong) NSArray *itemsArray;
#end
#implementation SettingsViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *itemsArray = [self.finder findClassesInTimetable];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)presentPicker {
CGRect frame = CGRectMake(0, 40, 0, 0);
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:frame];
picker.delegate = self;
picker.dataSource = self;
// TODO: animate this on screen
[self.view addSubview:picker];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self presentPicker];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.itemsArray count]; //where items is your array of items
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row inComponent:(NSInteger)component {
return [self.itemsArray objectAtIndex:row];
}
Can someone tell me what I'm doing wrong? I took the example from another Stackoverflow post:
This is how I use UIPickerViews, with sample code:
UIPickerView* genderPicker = [[UIPickerView alloc] init];
genderPicker.delegate = self;
genderPicker.dataSource = self;
After initializing your UITextField (registrationGenderField in my case):
registrationGenderField.inputView = genderPicker;
And then the delegates and dataSources:
- (NSString *) pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
switch (row) {
case 0:
return #"Gender";
break;
case 1:
return #"Male";
break;
case 2:
return #"Female";
break;
default:
return #"Gender";
break;
}
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (row) {
case 0:
registrationGenderField.text = #"";
break;
case 1:
registrationGenderField.text = #"Male";
break;
case 2:
registrationGenderField.text = #"Female";
break;
default:
registrationGenderField.text = #"";
break;
}
}
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 3;
}
Hope it helps you
- (void)presentPicker {
UIPickerView *picker = [[UIPickerView alloc] init];
picker.delegate = self;
picker.dataSource = self;
// TODO: animate this on screen
classTextField .delegate = self;
classTextField.inputView = picker;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[textField becomeFirstResponder];
return YES;
}
you just have to alloc init your pickerview;
and set its delegate to self and also add uipickerviewdelegate and datasource in .h file
than in textfieldbegin editing add [textField setInputView:picker];
also implement textfieldshouldreturn
I am relatively new to Xcode and I need some help. So, my view has 2 buttons (it has more buttons, but for simplicity's sake, let's say 2) which are all connected to one IBAction. When pressing each button a Picker appears loaded with an array depending on which button gets pressed. I would like for the title of the button to match what the user selected in the PickerView.
I know I have to modify some code in the pickerView:didSelectRow method but I don't know how to do it. The way it works now is that no matter which button I click (I should note the correct array loads up, so that part works correctly), button with tag "0" gets modified with the content of the selected Picker row. So I need the button at the SELECTED tag to be modified.
My header file is:
#interface XYZSecondViewController : UIViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) IBOutlet UIPickerView *myPicker;
#property (strong, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)myButton:(id)sender;
#end
My implementation file:
#import "XYZSecondViewController.h"
#interface XYZSecondViewController ()
#property (strong,nonatomic) NSArray *myArray1;
#property (strong,nonatomic) NSArray *myArray2;
#property (strong,nonatomic) NSArray *chosenArray;
#end
#implementation XYZSecondViewController
#synthesize myButton, myPicker;
- (IBAction)myButton:(id)sender
{
switch ([sender tag])
{
case 0:
self.chosenArray = self.myArray1;
break;
case 1:
self.chosenArray = self.myArray2;
break;
}
[self.myPicker reloadAllComponents];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [chosenArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
[myButton setTitle:[chosenArray objectAtIndex:row] forState:UIControlStateNormal];
}
You could track the tag for the button currently tapped.
Something like this:
#property (nonatomic) NSInteger tagLastTappedButton;
...
- (IBAction)myButton:(id)sender
{
tagLastTappedButton = sender.tag;
...
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
UIButton *buttonTapped = (UIButton *)[self.view viewWithTag:tagLastTappedButton];
[buttonTapped setTitle:[chosenArray objectAtIndex:row] forState:UIControlStateNormal];
}
Take one integer in you header:
int flag;
Modify you switch case:
switch ([sender tag])
{
case 0:
self.chosenArray = self.myArray1;
flag=0;
break;
case 1:
self.chosenArray = self.myArray2;
flag=1;
break;
}
and in your didSelectRow make an IF statement like:
if (flag==0) {
[myButton1 setTitle:[chosenArray objectAtIndex:row] forState:UIControlStateNormal];
}else{
[myButton2 setTitle:[chosenArray objectAtIndex:row] forState:UIControlStateNormal];
}
Hopefully this is an easy one.
Here's the deal: I've got a button item, "Select", that opens a picker. When the user selects something from the picker, the title of "Select" button changes to the selection and the picker animates out of view. Now I've created a second "Search" button that will query the Google Places API, but I need it to NOT query ITS title as its doing now, but the title of the OTHER button. Make sense?
This is the relevant code. I'm positive it's a simple tweak, but I'm new to Objective-C, so I'm still wrapping my head around how things operate.
ViewController.m
#import "RendezvousViewController.h"
#interface RendezvousViewController ()
#end
#implementation RendezvousViewController
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.userSelectArray count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.userSelectArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(#"Selected Row %ld", (long)row);
switch(row)
{
case 0:
self.userSelection.title = #"Breakfast";
break;
case 1:
self.userSelection.title = #"Brunch";
break;
case 2:
self.userSelection.title = #"Lunch";
break;
case 3:
self.userSelection.title = #"Dinner";
break;
case 4:
self.userSelection.title = #"Bar";
break;
case 5:
self.userSelection.title = #"Late Night Snack";
break;
}
}
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.userSelectArray = [[NSArray alloc] initWithObjects:#"Breakfast",#"Brunch",#"Lunch",#"Dinner",#"Bar",#"Late Night Snack" , nil];
//Make this controller the delegate for the map view.
self.mapView.delegate = self;
// Ensure that you can view your own location in the map view.
[self.mapView setShowsUserLocation:YES];
//Instantiate a location object.
locationManager = [[CLLocationManager alloc] init];
//Make this controller the delegate for the location manager.
[locationManager setDelegate:self];
//Set some parameters for the location object.
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
firstLaunch=YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)toolbarButtonPress:(id)sender {
UIBarButtonItem *button = (UIBarButtonItem *)sender;
NSString *buttonTitle = [button.title lowercaseString];
[self queryGooglePlaces:buttonTitle];
//Use the above title text to build the URL query and get the data from Google.
}
ViewController.m
#interface RendezvousViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate, UIPickerViewDataSource,UIPickerViewDelegate>
{
CLLocationManager *locationManager;
CLLocationCoordinate2D currentCentre;
int currenDist;
BOOL firstLaunch;
}
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#property (strong, nonatomic) IBOutlet UIBarButtonItem *userSelection;
#property (strong, nonatomic) IBOutlet UIBarButtonItem *searchButton;
#property (strong, nonatomic) IBOutlet UIPickerView *userPicker;
#property (strong, nonatomic) NSArray *userSelectArray;
#end
As always, thanks in advance.
If you make the button a property on your view, you could just access it directly (instead of using sender).
But why not get it directly from the picker instead of indirectly from the button title?
EDIT:
So based on your code, you want to add another button
#implementation RendezvousViewController{
UIButton *selectButton;
}
Initalize it
-(void)viewDidLoad {
selectButton = [[UIButton alloc] init];
...
}
Then set the title in the picker
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(#"Selected Row %ld", (long)row);
switch(row)
{
case 0:
self.selectButton.title = #"Breakfast";
break;
...
}
}
Then grab it when your other button is selected:
- (IBAction)toolbarButtonPress:(id)sender {
NSString *buttonTitle = [self.selectButton.title lowercaseString];
[self queryGooglePlaces:buttonTitle];
//Use the above title text to build the URL query and get the data from Google.
}
#implementation RendezvousViewController{
UIButton *selectButton;
NSString *selection;
}
I would suggest however, to have another property to just store the value that was selected instead of relying on your UI, like the button title. It would look like this:
Initalize it
-(void)viewDidLoad {
selectButton = [[UIButton alloc] init];
selection = #"";
...
}
Then set the title in the picker
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(#"Selected Row %ld", (long)row);
switch(row)
{
case 0:
selection = #"Breakfast";
break;
...
}
self.selectButton.title = selection;
}
Then grab it when your other button is selected:
- (IBAction)toolbarButtonPress:(id)sender {
[self queryGooglePlaces:selection];
//Use the above title text to build the URL query and get the data from Google.
}
if([buttonTitle isEqualToString:"Select"])
return; //no searching
else{
[self queryGooglePlaces:buttonTitle];
}
I have a UIPickerView that is being automatically loaded in loaded when a user touches the UITextField, at the end of selection the user can close the UIPicker by touching the "close" that I have added in the inputAccessoryView = toolbar;. My problem is that I need to set the value that is chosen in the UIPicker to be the value of self.textOutlet.text... I attempt to do so here but with no luck :
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [tempList objectAtIndex:row];
selectedText = _textOutlet.text;
Can someone please give me some ideas on how to fix this problem? Here is my header code:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) NSString* selectedText; //the UITextField text
#property NSArray * tempList;
#end
And here is my implementation code:
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *myLabel;
#property (weak, nonatomic) IBOutlet UIPickerView *thePicker;
#property (weak, nonatomic) IBOutlet UITextField *textOutlet;
#end
#implementation T3ViewController
#synthesize tempList, selectedText;
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [tempList count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [tempList objectAtIndex:row];
selectedText = _textOutlet.text;
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
CGFloat chosenValue;
switch (row) {
case 0:
chosenValue = 0.0000233;
break;
case 1:
chosenValue = 0.0001273;
break;
case 2:
chosenValue = 0.0012333;
break;
case 3:
chosenValue = 0.0032204;
break;
case 4:
chosenValue = 0.0234179;
break;
case 5:
chosenValue = 0.0002369;
break;
case 6:
chosenValue = 0.0004442;
break;
default:
chosenValue = 0;
break;
}
NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.minimumFractionDigits=7;
self.myLabel.text = [formatter stringFromNumber:#(chosenValue)];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
tempList = [[NSArray alloc] initWithObjects:#"3222° F",#"22150° F",#"1260° F",#"12170° F",#"83210° F",#"84415° F",#"10120° F", nil];
// hides the picker upon initiation of the view
self.thePicker.hidden = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)doneClicked:(id) sender
{
[_textOutlet resignFirstResponder]; //hides the pickerView
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.textOutlet) {
self.textOutlet.inputView = _thePicker;
self.thePicker.hidden = NO;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[toolbar 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:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
self.textOutlet.inputAccessoryView = toolbar;
}
}
#end
In your first snippet of code, you are setting selectedText after the return statement. This line of code will never get executed.
You need to return AFTER setting the selectedText
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
selectedText = _textOutlet.text;
return [tempList objectAtIndex:row];