I want to display UIPickerView with UIToolBar on the top of pickerView with done and cancel button.Attached is the screen shot for the same.I also have tab bar controller in bottom side of view and I want to display pickerView over bottom bar thats why I am using uipickerView inside of actionsheet.Now my issue is how to dismiss that pickerView on cancelButton click which is added in UIActionSheet?
Following is the function to display pickerView :
-(IBAction)setMile:(id)sender
{
menu = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,55,0,0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES; // note this is default to NO
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(setMileCancel:)];
[barItems addObject:cancelBtn];
[cancelBtn release];
cancelBtn=nil;
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
flexSpace=nil;
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(setMileDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
doneBtn=nil;
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
barItems=nil;
[menu addSubview:pickerToolbar];
[menu addSubview:pickerView];
[menu showInView:self.view];
//[menu showFromTabBar:[[self tabBarController] tabBar]]; this code is not working
[menu setBounds:CGRectMake(0,0,320, 545)];
[pickerView release];
[menu release];
pickerView=nil;
menu=nil;
}
I have following code on cancel button click
-(IBAction)setMileCancel:(id)sender
{
//[menu removeFromSuperview];
[menu dismissWithClickedButtonIndex:0 animated:YES];
}
// menu=nil;
Please comment this line, then it will work
Why are you using a UIActionSheet to accomplish this? Make the UIPickerView the inputView of a UITextField, and make the UIToolBar the inputAccessoryView of the UITextField. That way, you need only become/resignFirstResponder on the textfield to dismiss the UIPickerView and UIToolBar, without having any messy code! For more information, UITextField Reference. Hope that Helps!
I have resolved the issue.
Just remove following two lines from above code :
[menu release];
menu=nil;
Related
i add some viewcontroller to the tab bar controller and in viewcontroller i add 1 textfield so when i click on text field i call UIactionsheet. the action sheet contain 2 sub parts (1. UIpickerview 2. UItoolbar)... i face some issue like when i load UIactionsheet it works fine but i am not able select any row.. or not click on done button for dissmiss actionsheet. i put some code here..
in tabbarcontroller.m
-(void)LoadActionSheet
{
actionsheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
actionsheet.actionSheetStyle = UIActionSheetStyleDefault;
actionsheet.delegate = self;
}
in viewcontroller.m
-(void) Comboaction:(NSMutableArray *)DataArray
{
EditTabController *obj_tab = [[EditTabController alloc] init];
[obj_tab LoadActionSheet];
UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(100, obj_tab.actionsheet.frame.origin.y-15, 200, 44)];
Toolbar.barStyle = UIBarStyleBlack;
[Toolbar sizeToFit];
[Toolbar bringSubviewToFront:obj_tab.actionsheet];
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(DissmissPickerview)];
[barItems addObject:doneBtn];
[Toolbar setItems:barItems animated:YES];
[obj_tab.actionsheet setBounds:CGRectMake(0, obj_tab.tbar.frame.origin.y-100, 325,40)];
picker = [[UIPickerView alloc] init];
[picker setFrame:CGRectZero];
picker.tag = 101;
picker.delegate = self;
picker.dataSource = self;
picker.showsSelection
Indicator = YES;
[picker setShowsSelectionIndicator:YES];
[picker setBackgroundColor:[UIColor blackColor]];
// [picker setFrame:CGRectMake(0,Toolbar.frame.size.height+1, 320, 360)];
[picker bringSubviewToFront:obj_tab.actionsheet];
[obj_tab.actionsheet setDelegate:self];
[obj_tab.actionsheet addSubview:Toolbar];
[obj_tab.actionsheet addSubview:picker];
[obj_tab.actionsheet setBounds:CGRectMake(0, 0, 320, 50)];
[obj_tab.actionsheet showInView:[UIApplication sharedApplication].keyWindow];
}
and all pickerview delegate methods.. it works fine in simple viewcontroller. but not work properly in tabbarcontroller..
I am adding a button to an actionsheet. That's easy, but in my case there are more than 5 and Apple does not accept more 5 buttons on action sheet. So I add a scroll view to the actionsheet and add buttons to the scrollview. But this view has two fixed buttons that is cancel and one other with some title.
Is this the right way to do this?
if you have more than five buttons, its better to use a customized table view in normal view and give UIModalTransitionStyleCoverVertical so that it will come like an action sheet
Use following code....
- (IBAction)actionsheetbuttonpress:(id)sender {
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerData = [[UIPickerView alloc]initWithFrame:pickerFrame];
pickerData.showsSelectionIndicator = YES;
pickerData.dataSource = self;
pickerData.delegate = self;
[actionSheet addSubview:pickerData];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:#"DONE-A" style:UIBarButtonItemStyleBordered target:self action:#selector(DatePickerDoneClick)];
UIBarButtonItem *doneBtn1 = [[UIBarButtonItem alloc]initWithTitle:#"DONE-B" style:UIBarButtonItemStyleBordered target:self action:#selector(DatePickerDoneClick)];
UIBarButtonItem *doneBtn2 = [[UIBarButtonItem alloc]initWithTitle:#"DONE-C" style:UIBarButtonItemStyleBordered target:self action:#selector(DatePickerDoneClick)];
[barItems addObject:doneBtn];
[barItems addObject:doneBtn1];
[barItems addObject:doneBtn2];
//add more two button here...or you can add any no of buttons..
[pickerDateToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerData];
[actionSheet addSubview:pickerDateToolbar];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
[barItems release];
[actionSheet release];
[pickerData release];
}
Hope, this will help you....chill
I have a UIPickerView. I want a done button on top of the pickerview and i want to dismiss the picker view on click of the done button. Can u please help me on this?
add action sheet to your view and then add tool bar with done button
at top of action sheet and below add your your picker to action sheet
in done button click write below method to dismiss action sheet
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
actionSheet=[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.view];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
[cancelBtn release];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
[barItems release];
[pickerToolbar release];
UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 216);
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[actionSheet addSubview:picker];
[picker release];
-(void)done_clicked:(id)sender
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancel_clicked:(id)sender
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
You can use the toolbar for the same purpose. Take a look at this link. Here, the same solution is taken into consideration. Hope that may help you. Thanks.
every body took the action sheet and then added the bar button ,but simply added uibutton from the view and take method remove from the super view. show the bellow example , i am tried this code successful. i think this is very useful code with out using action sheet
-(void)parserDidEndDocument:(NSXMLParser *)parser { NSLog(#"the marray data is %#",marray); // [self showpicker1]; if (marray.count == 0) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#"NO DATA FOUND" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil]; [alert show]; }
[self showpicker1];
}
-(void)showpicker1 { //view1=[[UIView alloc] initWithFrame:CGRectMake(55, 59, 171, 80)]; //[self.view addSubview:view1]; pv = [[UIPickerView alloc] initWithFrame:CGRectMake(55,79,171,0)]; pv.delegate = self; pv.dataSource = self; pv.showsSelectionIndicator = YES; [self.view addSubview:pv]; done=[UIButton buttonWithType:UIButtonTypeRoundedRect]; [done setFrame:CGRectMake(55, 59, 171, 20)]; [done setTitle:#"DONE" forState:UIControlStateNormal]; [done addTarget:self action:#selector(dismiss) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:done]; } -(void)dismiss { [pv removeFromSuperview]; [done removeFromSuperview]; }
In my application in some view i have a picker at the bottom and tabBar i successfully hidden the tabBar but the problem is the frame allocated to TabBar is also hiding. so now the picker is displayed only half can any one help me out of this problem ?
use this method it works:
[actionSheetPicker showFromTabBar:self.tabBarController.tabBar];
I hope this helps you.
Don't hide tabbar if you hide it will so many create so many problems in this case
instead of hiding use this
[actionSheet showInView:self.tabBarController.tabBar];
You do not need to hide tabbar controller, you just need to add your action sheet in to tabbarcontroller
[actionSheet showInView:self.tabBarController.view];
Dont Hide tabbar and try bellow code
actionSheet=[[UIActionSheet alloc] init];
[actionSheet showInView:self.tabBarController.tabBar];
actionSheet.frame=CGRectMake(0, 100,480 ,232);
actionSheet.delegate=self;
picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 50,480 ,175)];
picker.delegate=self;
picker.showsSelectionIndicator=YES;
[actionSheet addSubview:picker];
UIToolbar *toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,480 ,50)];
toolbar.barStyle =UIBarStyleBlackTranslucent;
NSMutableArray *ButtonArray=[[NSMutableArray alloc ]init];
Save=[[UIBarButtonItem alloc ]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(save_pressed)];
UIBarButtonItem *titleName;
titleName=[[UIBarButtonItem alloc ] initWithTitle: #"Select Date" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *space=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancel=[[UIBarButtonItem alloc ]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel_pressed)];
[ButtonArray addObject:cancel];
[cancel release];
[ButtonArray addObject:space];
[ButtonArray addObject:titleName];
[titleName release];
[ButtonArray addObject:space];
[space release];
[ButtonArray addObject:Save];
[Save release];
[toolbar setItems:ButtonArray];
[ButtonArray release];
[actionSheet addSubview:toolbar];
[toolbar release];
[actionSheet release];
How do you make a UIPickerView act like the one with a webview wherein there is a drop down selection box and instead of dropping down like usual websites do, the iphone makes it into a UIPickerView with all the selections in. When you select one, a check becomes visible beside your selection and changes the value of the drop box. And how do you put the "Done" button on top of the UIPickerView to dismiss the UIPickerView?
I already know that [pickerview setHidden:YES] is the method to use to hide the pickerview. I just don't know how to include the "Done" button in the UIPickerView.
Regards,
Chris
This piece of code will slide out a picker view as keyboard and attached a done button on top of it. Basically, you want to set a inputAccessoryView with your input field.
You should call this method on a touch down event for your input field.
- (IBAction)showYourPicker:(id)sender {
// create a UIPicker view as a custom keyboard view
UIPickerView* pickerView = [[UIPickerView alloc] init];
[pickerView sizeToFit];
pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
self.yourPickerView = pickerView; //UIPickerView
yourTextField.inputView = pickerView;
// create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
// Prepare done button
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self
action:#selector(pickerDoneClicked:)] autorelease];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
// Plug the keyboardDoneButtonView into the text field...
yourTextField.inputAccessoryView = keyboardDoneButtonView;
[pickerView release];
[keyboardDoneButtonView release];
}
Finally, your Done button calls the "pickerDoneClicked" method, where you should add
[yourTextField resignFirstResponder]; which will hide the picker view.
The "Done" button is placed in UIToolBar.
Use the below method of UIToolBar for adding the "Done" buttons.
- (void)setItems:(NSArray *)items animated:(BOOL)animated {
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(DatePickerDoneClick)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
}