I'm using the custom MZFormSheetController to use the custom "alert view". I have a button when I press it, it opens the MZFormSheetController and allows me to select a specific title for this button from UIPickerView. When I select the title from the picker, it is send to the viewController that contains the button by setNeedsDisplay
I want to change the title of the button with the selected title from the pickerView. I wrote the code but the title of the button doesn't updated..
In the button's viewController, The action of the button:
-(IBAction)openSelectCountries:(id)sender{
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"countriesPickerViewController"];
MZFormSheetController *formSheet = [[MZFormSheetController alloc] initWithViewController:vc];
formSheet.transitionStyle = MZFormSheetTransitionStyleSlideFromTop;
formSheet.shadowRadius = 2.0;
formSheet.shadowOpacity = 0.3;
formSheet.shouldDismissOnBackgroundViewTap = YES;
formSheet.shouldCenterVerticallyWhenKeyboardAppears = YES;
[formSheet presentAnimated:YES completionHandler:^(UIViewController *presentedFSViewController) {
}];
}
in viewDidLoad:
// selectedCountryIndex2 is a static NSString
if (selectedCountryIndex != 0) {
selectedCountryIndex2 = selectedCountryIndex;
}
NSLog(#"selectedCountryIndex2: %d", selectedCountryIndex2);
if (selectedCountryIndex2 == 0) {
NSLog(#"no country selected");
}else{
NSLog(#"the selected country: %#", [countryArray objectAtIndex:selectedCountryIndex2]);
[countrySelect setTitle:[countryArray objectAtIndex:selectedCountryIndex2] forState: UIControlStateNormal];
}
in CountriesPickerViewController.m:
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [countryArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [countryArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"in pickerView: %#", [countryArray objectAtIndex:row]);
ContactUsViewController *contactUsViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"contactUsViewController"];
contactUsViewController.selectedCountry = [countryArray objectAtIndex:row];
contactUsViewController.selectedCountryIndex = row;
[contactUsViewController.view setNeedsDisplay];
// to close the custom dialog
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"countriesPickerViewController"];
MZFormSheetController *formSheet = [[MZFormSheetController alloc] initWithViewController:vc];
[formSheet dismissFormSheetControllerAnimated:YES completionHandler:nil];
}
The expected thing is that the countrySelect button change its title when selectedCountryIndex2 is not equal 0 but this never happen although that the title is passed successfully and I got it by NSLog.
Related
I am new to IOS i want to create multiple textfield with only one picker progamatically. Suppose i need five textfield means if i clicked first textfield picker view can load the first array then i go to second textfield means picker view can load second array automatically and goes on upto last field and array.please give me idea and suggestion based on my problem.
Follow this steps to achieve your goal.
Download below generic classes from given link:
LabeledPickerView.h
LabeledPickerView.m
Copy this class into your project and import "LabeledPickerView.h" into your ViewController.h file. Also, Add UIPickerViewDataSource, UIPickerDelegate and UITextFieldDelegate.
Now, initialized Picker with following method:-
-(LabeledPickerView *)GetPickerViewWithTag:(int)Tag {
LabeledPickerView *pickerView = [[LabeledPickerView alloc] init];
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.tag = Tag;
pickerView.backgroundColor = [UIColor whiteColor];
pickerView.showsSelectionIndicator = YES;
return pickerView;
}
This method will return PickerView's properties.
Now, We'll add data into this using UIPickerView Delegate and Data Source Methods as follow:-
//Based on the text fields tags, you can populate the data in PickerView.
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1; //Returns components in PickerView. Change with switch - case statement if you want more components in any of the text fields.
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
int tag = (int)((UIPickerView *)pickerView).tag;
switch (tag) {
case 10: {
if (YourArray.count > 0) {
return [YourArray count];
}
else {
return 0;
}
break;
}
default:
break;
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
int tag = (int)((UIPickerView *)pickerView).tag;
switch (tag) {
case 10: {
if ([YourArray count] > 0) {
return [YourArray objectAtIndex:row];
}
else {
return #"";
}
break;
}
default:
break;
}
return #"";
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
int tag = (int)((UIPickerView *)pickerView).tag;
switch (tag) {
case 10: {
if ([YourArray count] > 0) {
YourTextField.text = [YourArray objectAtIndex:row];
}
else {
txtCity.text = #"";
}
break;
}
default:
break;
}
}
Now, Open Picker When You Start Editing as follow:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == firstTextField) {
LabeledPickerView *picker = [self GetPickerViewWithTag:10]; //Change tag as text field changes..
textField.inputView = picker;
[picker selectRow:0 inComponent:0 animated:YES];
[self pickerView:picker didSelectRow:0 inComponent:0];
}
}
In this method add as many text fields you have. Do't forget to assign different tags to each text field.
By Using above code, You can use single picker view with multiple text fields.
Happy Coding..!!
May be this will help you,
i done the same code in my project and it working. here i give you code for three textfields.
NSMutableArray *pickerTitleData, *pickerCityData, *pickerStateData;
#property (strong, nonatomic) IBOutlet UItextfield *txtTitle;
#property (strong, nonatomic) IBOutlet UItextfield *txtTitle;
#property (strong, nonatomic) IBOutlet UItextfield *txtTitle;
UIPickerView *TitleSelect,*CitySelect, *StateSelect;
for address type
pickerTitleData =[[NSMutableArray alloc]initWithObjects:#"Home",#"Office",#"Other", nil];
TitleSelect = [[UIPickerView alloc]init];
TitleSelect.dataSource = self;
TitleSelect.delegate = self;
TitleSelect.showsSelectionIndicator = YES;
txtTitle.inputView = TitleSelect;
// same you have three other array
#pragma mark - Picker View Data source for city
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
#try
{
if (pickerView == TitleSelect)
{
return pickerTitleData.count;
}
else if (pickerView == CitySelect)
{
return pickerCityData.count;
}
else if (pickerView == StateSelect)
{
return pickerStateData.count;
}
return 1;
}
#catch (NSException *exception)
{
NSLog(#"exception--%#",exception.description);
}
}
#pragma mark- Picker View Delegate for city
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
#try
{
if (pickerView == TitleSelect)
{
[txtTitle setText:[pickerTitleData objectAtIndex:row]];
}
else if (pickerView == CitySelect)
{
[txtTownOrCity setText:[pickerCityData objectAtIndex:row]];
}
else if (pickerView == StateSelect)
{
[txtState setText:[pickerStateData objectAtIndex:row]];
}
}
#catch (NSException *exception)
{
NSLog(#"%#",exception.description);
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
#try
{
if (pickerView == TitleSelect)
{
return pickerTitleData[row];
}
else if (pickerView == CitySelect)
{
return pickerCityData[row];
}
else if (pickerView == StateSelect)
{
return pickerStateData[row];
}
return #"";
}
#catch (NSException *exception)
{
NSLog(#"%#",exception.description);
}
}
I am using a uipicker app where I have two columns of selected data, using this code I can select the values fromm the picker (when it only has one row)
- (void)viewDidLoad
{
[super viewDidLoad];
_pickerData = #[#"item1", #"item2", #"Item 4", #"Item 5", #"Item 6"];
self.picker.dataSource = self;
self.picker.delegate = self;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
// return 2;
}
// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerData.count;
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _pickerData[row];
// return _pickerData[row][component];
}
- (IBAction)selOptions:(id)sender {
NSInteger row = [self.picker selectedRowInComponent:0];
NSString *selected = [_pickerData objectAtIndex:row];
NSLog(selected);
}
#end
With this code when I click my button I have the value of whatwas selected and then I can go on to push my segue or whatever I like.
However, I would like mypicker to have two rows, but with the code posted below...I cannot I get an error when I click the button.
- (void)viewDidLoad
{
[super viewDidLoad];
_pickerData = #[ #[#"item1", #"A"],
#[#"item2", #"B"],
#[#"item3", #"C"],
#[#"item4", #"D"],
#[#"item5",#"E"],
#[#"item6",#"F"]
];
// Connect data
self.picker.dataSource = self;
self.picker.delegate = self;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
// return 1;
return 2;
}
// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerData.count;
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _pickerData[row][component];
}
- (IBAction)selOptions:(id)sender {
NSInteger row = [self.picker selectedRowInComponent:0];
NSString *selected = [_pickerData objectAtIndex:row];
NSLog(selected);
NSInteger row2=[self.picker selectedRowInComponent:1];
NSString *selectedb=[_pickerData objectAtIndex:row2];
NSLog(selectedb);
}
#end
The data is displayed fine but, I do not know how to allow for values from both of my rows to be selected upon clicking my button.
Any help on this would be appreciated.
Thank you
I have a UIPickerView filled with an array of tree types. I also have an empty UIImageView below that I would like to change upon the selection of the picker view. I got a label to change with the selection but cannot get images to change. Here is what I have so far
EDIT
EDIT : Now working! Correct Code below
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dataArray = [[NSArray alloc]initWithObjects:#"Birch", #"Elm", #"Maple", #"Oak", #"White Pine",#"Willow", nil];
myPicker.delegate = self;
imageArray = [[NSArray alloc]initWithObjects:#"birch_river.png",#"elm_american.png",#"maple_bigtooth.png", #"oak_bur.png", #"white_pine.png", #"oak_willow.png", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [dataArray count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
myLabel.text = [dataArray objectAtIndex:row];
myImage.image = [UIImage imageNamed:[imageArray objectAtIndex:row]];
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component
{
return [dataArray objectAtIndex:row];
}
#end
What you can do is, have an array filled with the image names (Ex: IMAGES_ARRAY with image names).
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
myLabel.text = [dataArray objectAtIndex:row];
YOUR_IMG_VIEW = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[IMAGES_ARRAY ObjectAtIndex.row]]];
}
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>
I have a profile form as part of a registration process for an iOS app.
I would like to use a 'drop down' menu for items such as gender, title, DOB etc. The data for each will be static - I will use the UIPickerView to implement - but my question is - do I need to create arrays and data delegates to populate each individual picker or is there a simpler way to apply static data?
Can you do it without a delegate? No.
Can you do it without an array? Yes, but you shouldn't.
Here is an example without an array (from http://cocoamatic.blogspot.com/2010/08/create-uipickerview-programmatically.html).
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 3;
return numRows;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title = [#"" stringByAppendingFormat:#"Row %d",row];
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
However, you really should just use an array it makes it so much easier to read an maintain. If you want to add an additional value, you just add it to your array. Instead of updating in multiple spots, just add another value to your array. Also, much easier to understand.
#implementation PickerViewController
.
.
- (void)viewDidLoad {
[super viewDidLoad];
_myArray = #[#"Row 1", #"Row 2", #"Row 3",];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 3;
return _myArray.count;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title = myArray[row];
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
If you had multiple pickers, you would just use multiple arrays and check each PickerView to see which one you had since the PickerView is passed into each of the functions listed.
#implementation PickerViewController
.
.
- (void)viewDidLoad {
[super viewDidLoad];
_myArray1 = #[#"Row 1", #"Row 2", #"Row 3",];
_myArray2 = #[#"Row 1-2", #"Row 2-2", #"Row 3-2", #"Row 4-2"];
UIPickerView pickerView1 = [[UIPickerView alloc] init];
UIPickerView pickerView2 = [[UIPickerView alloc] init];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
if (pickerView == pickerView1)
{
// First Picker
}
else if (pickerView == pickerView2)
{
// First Picker
}
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == pickerView1)
{
// First Picker
return _myArray1.count;
}
else if (pickerView == pickerView2)
{
// Second Picker
return _myArray2.count;
}
// A third picker passed in somehow
return 0;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (pickerView == pickerView1)
{
// First Picker
return 1;
}
else if (pickerView == pickerView2)
{
// Second Picker
return 1;
}
// A third picker passed in somehow
return 0;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
if (pickerView == pickerView1)
{
// First Picker
title = myArray1[row];
}
else if (pickerView == pickerView2)
{
// Second Picker
rtitle = myArray2[row];
}
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (pickerView == pickerView1)
{
// First Picker
return 300;
}
else if (pickerView == pickerView2)
{
// Second Picker
return 400;
}
// A third picker passed in somehow
return 0;
}