add buttons on actionsheet. and buttons should be more than 5 - iphone

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

Related

action sheet hiding the button showing it in scroll view

I have a UIButton on a scroll view. On clicking that button action sheet appears. But the button gets hide behind the sheet. I want button to be scrolled up so that it remains visible above action sheet.
Please help
- (IBAction)foodInstructionOrPrescriptionInformationButtonClicked:(UIButton *)sender
{
tagForPicker = sender.tag;
[self presentPicker:sender];
}
- (void)presentPicker:(UIButton*)element
{
actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
picker.tag = tagForPicker;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
UIToolbar *tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
tools.barStyle=UIBarStyleDefault;
[actionSheet addSubview:tools];
UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActionPickerDoneClicked)];
UIBarButtonItem *CancelButton=[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActionPickerCancelClicked)];
UIBarButtonItem *flexSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *array = [[NSArray alloc]initWithObjects:CancelButton,flexSpace,flexSpace,doneButton,nil];
[tools setItems:array];
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 400)];
}
else
{
[actionSheet showFromRect:CGRectMake(0, 480, 320, 215) inView:self.view animated:YES];
[actionSheet setBounds:CGRectMake(0, 0, 320, 511)];
}
}
- (void)viewDidLayoutSubviews
{
[backgroundScrollView setContentSize:CGSizeMake(320,1340)];
}

display pickerview along with action sheet

I have a textfield. once I tap on that I want a UIPickerView to pop out and select an item from the UIPickerView and after once I am done with the selection I want to confirm the selection by tapping on an UIActionSheet which has confirm button on it.So my requirement is to have a UIActionSheet and beneath that a picker view. I am just a beginner in iOS development and I am still not familiar with the technical terminologies. So please bear with my informal way of asking questions.
Here is my block of code:
creating UIPickerView and UIActionSheet
UIActionSheet *confirmRoomSelectionAS =[[UIActionSheet alloc]initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Done",nil];
UIPickerView *roomTypePicker=[[UIPickerView alloc]init];
[confirmRoomSelectionAS addSubview:roomTypePicker];
[confirmRoomSelectionAS showInView:self.view];
[confirmRoomSelectionAS setBounds:CGRectMake(0, 0, 320, 600)];
[roomTypePicker setFrame:CGRectMake(0, 170, 320, 216)];
[roomTypePicker setDataSource:self];
[roomTypePicker setDelegate: self];
roomTypePicker.showsSelectionIndicator = YES;
Try like this,
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet showInView:[self.view window]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 390)];
[actionSheet addSubview:[self createToolbar:#"Test"]];
UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 162);
picker.showsSelectionIndicator = YES;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
- (UIView *)createToolbar:(NSString *)titleString {
UIToolbar *inputAccessoryView = [[UIToolbar alloc] init];
inputAccessoryView.barStyle = UIBarStyleBlackOpaque;
inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[inputAccessoryView sizeToFit];
CGRect frame = inputAccessoryView.frame;
frame.size.height = 44.0f;
inputAccessoryView.frame = frame;
UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancelBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel:)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 , 11.0f, 100, 21.0f)];
[titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:14]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:titleString];
[titleLabel setTextAlignment:UITextAlignmentCenter];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
NSMutableArray *array = [NSMutableArray arrayWithObjects:cancelBtn,flexibleSpaceLeft,title,flexibleSpaceLeft, doneBtn, nil];
[inputAccessoryView setItems:array];
[doneBtn release];
[title release];
[titleLabel release];
[flexibleSpaceLeft release];
[cancelBtn release];
return [inputAccessoryView autorelease];
}
- (void)done:(id)sender {
}
- (void)cancel:(id)sender {
}
It will be like,
3 things I thought you should modify:
1) add your cancel button manually, to later better check the cancel button pressing event
UIActionSheet * confirmRoomSelectionAS = [[UIActionSheet alloc]
initWithTitle:#""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[confirmRoomSelectionAS addButtonWithTitle:#"Done"];
confirmRoomSelectionAS.cancelButtonIndex = [confirmRoomSelectionAS addButtonWithTitle: #"Cancel"];
[confirmRoomSelectionAS showInView:self.view];
2) implement your actionsheet delegate method
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.cancelButtonIndex) {
// cancel button pressed;
return;
}
}
3) You should declare *UIPickerView picker in your .h to later check of its selected value in the previous delegate method (also including the Done check) by adding sthing like this:
NSInteger row;
row = [picker selectedRowInComponent:0];

issue of hide uipickerview in iphone

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;

UIpickerview not animating from UItabbarcontroller

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..

How to add a button on top of a PickerView and How to dismiss the PickerView on click of that button?

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]; }