Hi My UI Picker no longer works on iOS7, it pops up but it is blank. Only happened on iOS7.
//picker
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
animationsLabel.text = [optionsArray objectAtIndex:[picker selectedRowInComponent:0]];
if (pickerView==animationsPicker) {
animationsLabel.text = [optionsArray objectAtIndex:[animationsPicker selectedRowInComponent:0]];
}
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [optionsArray count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [optionsArray objectAtIndex:row];
NSLog(#"title options array %lu",(unsigned long)optionsArray.count);
}
And the action:
-(IBAction)animationActionSheet:(id)sender {
UIActionSheet *selectPopup = [[UIActionSheet alloc] initWithTitle:#"\n\n\n\n\n\n\n\n"
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[selectPopup showInView:self.view];
optionsArray = [[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"6",#"5",#"7",#"8",#"9", nil];
// Create picker
animationsPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 160, 320, 100)];
animationsPicker.showsSelectionIndicator = YES;
[self.view addSubview:animationsPicker];
animationsPicker.delegate = self;
animationsPicker.dataSource = self;
[selectPopup addSubview:animationsPicker];
// Animate picker opening
[UIView beginAnimations:nil context:nil];
[selectPopup setBounds:CGRectMake(0, 0, 320, 485)];
[UIView commitAnimations];
}
This is what I have tried so far. There was never a problem displaying the picker in the an Action sheet before, but now it is blank. Im wondering did anyone else come across this problem, or can you see any errors? I printed everything to do with the picker.
I was told that putting pickers in Action Sheets was non-standard. I never had any problems doing this in iOS 6, but in iOS 7, I got errors. I switched to UIView instead of action sheets for my pickers and it works fine. I hope this helps.
Adding subviews is not recommended for the action sheet, per documentation.
"Subclassing Notes
UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:."
Apple is reinforcing their usage for Action Sheets....and they upped the ante for this to work in iOS 7, which means you have to code around it.
Check out this project I made. It's practically the same thing as having a picker inside an action sheet https://github.com/DiscoverPioneer/PickerSlider
Related
Wish you happy new year to all iOS developers.I want to display Blood group in UITextfield.
So I should took UIPickerview and append the Blood group list.My requirement is if user hit the UITextfiled the UIPickerview will be displayed and user select the blood group and it will reflect the blood group item in UITextfield.I am trying to do this functionality. But I have got some problem to display blood group value in UITextfield.The problem is for example if I click the A-ve blood group this will be displayed as it is in UITextfield.But if i click the A+ve group it will display like A ve instead of A+ve in UITextfield. This is my problem.Any body please help me. Thanks in advance.
The given below is my code.
//Bloodgroupviewcontroller.m
arrChooseServices=[[NSMutableArray alloc]initWithObjects:#"A+ve",#"A-ve",#"B+ve",#"B-ve",#"AB+ve",#"AB-ve",#"O+ve",#"O-ve", nil];
bloodGroup=[[UITextField alloc]initWithFrame:CGRectMake(150, 200, 120, 20)];
bloodGroup.borderStyle=UITextBorderStyleRoundedRect;
bloodGroup.backgroundColor=[UIColor clearColor];
bloodGroup.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
[bloodGroup setUserInteractionEnabled:NO];
bloodGroup.font=[UIFont systemFontOfSize:14.0];
bloodGroup.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
bloodGroup.textAlignment=NSTextAlignmentCenter;
bloodGroup.delegate=self;
bloodGroup.inputView=self.chooseServicePicker;
bloodGroup.inputAccessoryView=self.accessoryView;
[self.view addSubview:bloodGroup];
-(UIPickerView *)chooseServicePicker
{
if(chooseServicePicker==nil)
{
chooseServicePicker=[[UIPickerView alloc]init];
chooseServicePicker.delegate=self;
chooseServicePicker.dataSource=self;
chooseServicePicker.showsSelectionIndicator=YES;
}
return chooseServicePicker;
}
-(UIToolbar *)accessoryView
{
if ( accessoryView == nil )
{
accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(onBloodGroupSelection)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
accessoryView.items=[NSArray arrayWithObjects:doneButton,cancelButton, nil];
}
return accessoryView;
}
- (void)onBloodGroupSelection
{
NSInteger row = [self.chooseServicePicker selectedRowInComponent:0];
bloodGroup.text=[arrChooseServices objectAtIndex:row];
[bloodGroup resignFirstResponder];
}
About the + not displaying, I tested your code and it is working fine for me. I had to change the [bloodGroup setUserInteractionEnabled:NO]; to [bloodGroup setUserInteractionEnabled:YES]; to make the UITextField clickable. Still, try adding
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component{
bloodGroup.text = [arrChooseServices objectAtIndex:row] ;
}
to your code to make the UITextField change text when the UIPickerView is tapped.
Try this,
Add this method and other delegate method to ur class
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component
{
yourtextfield.text = [yourdataarray objectAtIndex:row] ;
}
Take a string globally as "selectedString"
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
selectedString = yourdataarray[row];
}
-(void)onBloodGroupSelection
{
bloodGroup.text= selectedString;
[self.view endEditing:YES];
}
I have a UIPickerview inside a ActionSheet dateSheet that gets the demographic of the user. Here is my code.
UIPickerView *picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0,125,320,216)];
picker.tag = 1;
picker.delegate=self;
picker.showsSelectionIndicator=YES;
[dateSheet addSubview:picker];
[dateSheet showInView:self.view];
[dateSheet setFrame:CGRectMake(0,220,320,312)];
I changed the code from this:
UIPickerView *picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0,95,320,216)];
picker.tag = 1;
picker.delegate=self;
picker.showsSelectionIndicator=YES;
[dateSheet addSubview:picker];
[dateSheet showInView:self.view];
[dateSheet setFrame:CGRectMake(0, 168, 320, 312)];
But either way now on the iPad simulator it looks like this
Here is where I set my pickerviews data
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [pickerdata objectAtIndex:row];
}
I know lots of people have asked same question, and I've tried lots of them, I don't know what part that I miss, I still can get it to work.
I have a 9*9 table that I want to display and change, because of the size of Iphone Screen, I'm thinking to show one column a time.
What I want is to press the top left button and a UIPickerView will popup allows me to choose which column to display, then reload the UITextFields.
Can anyone give me a detailed answer? I'm still relatively new in this(couple months).
Thank you in advance.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
Above is the code I found, it pops up alright, I have problem adding array to pickerview, and can anyone tell me how should dismissActionSheet method be?
Thank you.
Ok, so I think I understand what you are asking, correct me if this isn't what you need help with...but to add an array (of presumably NSStrings) to the picker view you need to use the - (NSString *)pickerView:titleForRow:forComponent: delegate method. Here is a quick examle:
//Say you have an array of strings you want to present in the pickerview like this
NSArray *arrayOfStrings = [NSArray arrayWithObjects:#"One", #"Two", #"Three", #"Four", nil];
//First we need to say how many rows there will be in the pickerview
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayOfStrings count];
}
//Do the same for components (columns) in pickerview
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
//Here we set the actual title/string on the pickerview
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component==0){
//Grab the nth string from array
return [arrayOfStrings objectAtIndex:row];
}
}
//This is called if a user taps a row
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(component==0)
NSLog(#"The User Selected the row titled %#", [arrayOfStrings objectAtIndex:row]);
}
Similairly with actionsheet, you need to add the delegate method for handling when a user clicks a button on the actionsheet
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex==0){
NSLog(#"First Button Clicked");
} else if( buttonIndex == 1){
NSLog(#"Second Button Clicked");
}
}
I started learning iOS programming about two months ago and so far I love it, but I'm kinda struggling a bit with UIPicker(s) and UIDatePickers in the app that I'm making, and I was hoping that someone might point me in the right direction. The app is supposed to mimic already existing PHP web app, that is nothing more than a form with a bunch of dropdowns.
Lets say that i have GetAddressViewController that will have a bunch of input fields. For the sake of simplicity lets say that I will have only 3 input fields for: country, city and street.
The user should tap on "country" input field and the UIPicker shows up much like a keyboard would and after selecting the country, a web app will return a JSON array with all the cities of that country. The same process is repeated when user taps on "city" input field, the UIPicker pops up with a list of returned cities, user selects a street, the UIPicker slides out and web app returns an array of streets etc.
Lets say that in my In my GetAddressViewController.h file i have:
#import <UIKit/UIKit.h>
#interface GetAddressViewController : UIViewController
{
UITextField *countryField;
UITextField *cityField;
UITextField *streetField;
NSArray *countries;
NSArray *cities;
NSArray *streets;
}
#property(nonatomic, strong) IBOutlet UITextField *countryField;
#property(nonatomic, strong) IBOutlet UITextField *cityField;
#property(nonatomic, strong) IBOutlet UITextField *streetField;
#property(nonatomic, strong) IBOutlet NSArray *countries;
#property(nonatomic, strong) IBOutlet NSArray *cities;
#property(nonatomic, strong) IBOutlet NSArray *streets;
#end
In GetAddressViewController.m file i have synthesized properties and in the storyboard i only have 3 input fields that have been connected to appropriate outlets.
Is there some fundamental mistake in my existing code that i should be aware of?
Now, I feel that I have missed something while reading the tutorials regarding picker views, since most of the examples that I've found on StackOverflow don't make much sense to me.
Could someone point me to a basic similar example that could help me. Are UIPickers the way to go or is there a better approach?
I haven't felt this helpless for a while and any help would be greatly appreciated, thanks
EDIT:
Ok I'm making progress I hope that I can help someone else who has the same problem.
To make this work and to connect pickerview to input fileds inputView property you need to do this.
To your .h file you need to add two delegates: UIPickerViewDelegate and UIPickerViewDataSource
#interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>{ ...
Then in your .m file you'll need to have something like this. I have this code in my wiewDidLoad method.
UIPickerView *cityPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
cityPicker.delegate = self;
cityPicker.dataSource = self;
[cityPicker setShowsSelectionIndicator:YES];
cityField.inputView = cityPicker;
This simply means that cityPicker pickerview will appear when you tap on the cityField.After that you need to add the following pickerview delegate methods that will fill pickerview with correct data. The data in my case is an array that holds a list of cities.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return cities.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [cities objectAtIndex:row];
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
cityField.text = (NSString *)[cities objectAtIndex:row];
}
If you want to have "Done" that will hide the UIPicker you'll need this code:
UIToolbar* mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
cityField.inputAccessoryView = mypickerToolbar;
And add a new method pickerDoneClicked
-(void)pickerDoneClicked
{
NSLog(#"Done Clicked");
[cityField resignFirstResponder];
}
I hope that this may help someone.
Hi Please follow this whole solution of multiple pickerview or just use -(void)createActionsheet it may solve your problem
define in .h
UIActionSheet *actionSheet;
NSString *pickerType;
BOOL select
define in .m
-(IBAction)countryBtnPressed:(id)sender
{
[self createActionSheet];
pickerType = #"picker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
Txt.text = [array objectAtIndex:0];
[chPicker release];
}
-(IBAction)stateBtnPressed:(id)sender
{
[self createActionSheet];
pickerType = #"statepicker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
stateTxt.text = [stateArray objectAtIndex:0];
[chPicker release];
}
/// Create Action Sheet
- (void)createActionSheet {
if (actionSheet == nil) {
// setup actionsheet to contain the UIPicker
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}
#pragma mark UIPickerViewDelegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count;
if ([pickerType isEqualToString:#"picker"])
count = [array count];
else if([pickerType isEqualToString:#"picker"])
count = [statearray count];
return count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:#"picker"])
string = [array objectAtIndex:row];
if ([pickerType isEqualToString:#"statepicker"])
string = [statearray objectAtIndex:row];
return string;
}
// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
select = YES;
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:row];
}
if ([pickerType isEqualToString:#"statepicker"])
{
stateTxt.text = [statearray objectAtIndex:row];
}
}
- (void)pickerDone:(id)sender
{
if(select == NO)
{
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:0];
}
else if ([pickerType isEqualToString:#"statepicker"])
{
stateTxt.text = [statearray objectAtIndex:0];
}
}
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
}
UIPickers are OK to use, but in this particular case, where the list of entries can be a very long list and also the entries can some times may be more than a simple string, for example, if the country name is "Republic of Zimbabwe, Africa" or some thing like that UIPicker controllers cannot display the data in the best possible way. You can use table views and present them modally. Tableviews give you a much better way to do a selection if the list of entries are more than a hundred or so.
I've a UIPickerView and need to make it show inside a UIActionSheet in iPad (it works very straightforward in iPhone, not in iPad).
I've a button on the View when I click on I execute the following code:
- (IBAction) buttonPressed:(id)sender
{
NSLog(#"I am pressed");
UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(100,200,500,500)];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[self.view addSubview:picker];
}
// the PickerView delegate and datasource functions:
// 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 5;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return #"One";
}
But When change the above code to add the PickerView inside an actionSheet, It shows only the header of the ActionSheet but without the PickerView inside it!
The modified code as follows:
- (IBAction) buttonPressed:(id)sender
{
NSLog(#"I am pressed");
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:#"My PickerView"
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(100,200,500,500)];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[actionSheet addSubview:picker];
[actionSheet showFromRect:CGRectMake(0, 0, 320, 469) inView:self.view animated:NO];
}
I stuck, I gonna use a UIPopoverController with a UITableViewController inside instead! .. Thanks anyway!
Try to modify the frame of the picker. (100, 200) on (x, y) might not be a good position.
I think the line ([actionSheet addSubview:picker];) should be
[actionSheet addSubview:picker.view];
I was having the same issue but using UIWindow.