i need to add button dynamically in UIActionSheet in iphone.Please help me.
Just alloc and init a new UIActionSheet instance and add the buttons one after another using –addButtonWithTitle:. This method returns you the index at which the button has been added to. You can then set the Index of the destructive button via -setDestructiveButtonIndex.
Here is an example that adds one button and adds another one if the boolean value useDestructiveButton is YES (and directly sets it as destructive button, making it red):
UIActionSheet *sheet = [[UIActionSheet alloc] init];
[sheet addButtonWithTitle:#"Button 1"];
if (useDestructiveButton) {
[sheet setDestructiveButtonIndex:[sheet addButtonWithTitle:#"Button 2"]];
}
Don't forget to call the appropriate show method.
UIActionSheet * as=[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel",#"Cancel") destructiveButtonTitle:nil otherButtonTitles:nil];
[as setTag:0];
[as addButtonWithTitle:NSLocalizedString(#"First Button",#"First Button")];
[as addButtonWithTitle:NSLocalizedString(#"Second Button",#"Second Button")];
[as showInView:someController.view];
[as autorelease];
Also remember that your parent controller has to conform to the UIActionSheetDelegate protocol.
Related
In my iPhone app, I have an actionsheet which has 4 buttons.
Now to perform actions on click of these buttons, I have implemented ActionSheet Delegate.
The action sheet delegate method does not get called on click of the buttons. The same code works when integrated to another project.
I have declared all the method names properly.
Below is the screenshot which shows the delegate method
What could be wrong?
Make sure you have set your UIActionSheet delegate to self:
actionSheet.delegate = self;
and also make sure you're implementing <UIActionSheetDelegate> in the header file.
If you use the code to call the action sheet like below
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"YOUR TITLE" delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"YOUR MESSAGE" otherButtonTitles:#"Cancel", nil];
[actionSheet showInView:self.view];
[actionSheet release];
Then there won't be an issue calling the delegate method,
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
Cheers
I have a UIView with a date picker that I'd like to display in an action sheet. I'm using the following code:
-(IBAction) button_click:(id)sender{
//UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"the title" delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Destructive" otherButtonTitles:#"other", nil];
UIActionSheet *sheet = [[UIActionSheet alloc] init];
ActionSheetController *actionSheet = [[ActionSheetController alloc] initWithNibName:#"ActionSheetView" bundle:nil];
[sheet addSubview:actionSheet.view];
[sheet showInView:self.view];
}
What I get is a little bit of the top part of the new view coming up from the bottom and that's it. If I comment the two middle lines of code and uncomment the top part to display a regular action sheet, it works fine. Any ideas what I might be doing wrong?
use UIActionSheetDelegate and after that you can with this delegate add for example a image to button of alertsheet
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{
UIImageView *sample = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:#"Someimage.png"]];
[actionSheet addSubView:sample];
}
I do not believe you can specify Custom Views in a UIActionSheet. My understanding is that you can only display a title and 1 or more buttons. Below is the description of UIActionSheet in the Apple documentation:
Use the UIActionSheet class to present the user with a set of alternatives for how to proceed with a given task. You can also use action sheets to prompt the user to confirm a potentially dangerous action. The action sheet contains an optional title and one or more buttons, each of which corresponds to an action to take.
UIActionSheet Class Reference
I've discovered the behavoir occurs because I haven't used the UIActionSheet constructor properly. The buttons and title should be included:
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
You might want to check out an UIActionSheet alternative I made which lets you display custom content views without any dirty hacks: https://github.com/JonasGessner/JGActionSheet It has some other advantages over UIActionSheet as well!
In my tabbar app, i bring up a UIActionsheet from an action, called from a button in a navigation controller title bar.
The UIActionsheet functions as per normal, except for the bottom half of the below button 'cancel', which strangely doesnt respond to touch in the iPhone Simulator. The bottom half of the cancel button is where the UITabBar lies behind, and therefore is probably the problem.
Any ideas?
alt text http://img12.imageshack.us/img12/2166/screenshot20100119at236.png
Solution
My solution was from the first answer. Here is my working example code
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:message delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"OK" otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
[actionSheet release];
This looks like an UIActionSheet to me...
Anyway, you should show the action sheet as a subview of the application window, not of the current view.
UIActionSheet *actionSheet = [[UIActionSheet alloc] init...];
// ...
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
I've two questions for you, which i can't solve:
1) How do I create a menu like this -->
(source: momolog.com)
.
2) How can I add multiple images like the iApp named HotBook do?
.
Thanks in advance for your help.
Sean
Old post, but this may help others who come across the page...
For #1, that's a UIActionSheet. You can instantiate it with this:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Share" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"On Facebook", #"On Twitter", nil];
[actionSheet showInView:self.view];
[actionSheet release];
Note that your UIViewController must conform to the UIActionSheetDelegate protocol.
I am implementing datepicker with Action Sheet with the following code :
-(void)popupActionSheetWithDatePicker
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Done",nil];
UIDatePicker *picker = [[UIDatePicker alloc] init];
self.datePicker = picker;
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[sheet addSubview:self.datePicker];
[sheet showInView:self.view];
[sheet setBounds:CGRectMake(0,0, 320,500)];
CGRect pickerRect = self.datePicker.bounds;
pickerRect.origin.y = -80;
self.datePicker.bounds = pickerRect;
[sheet bringSubviewToFront:self.datePicker];
[picker release];
[sheet release]; }
Now, The Picker pops up & working perfactly Except below the Selection Indicator.
I mean On & Above the Selection indicator I am able to choose date, month & year, but Below that Selection indicator I can't select anything, It's become like disabled.
Can't find the reason or solution.
If Possible Please send any working Datepicker with UIActionsheet code.
Thanks in advance.
I have had a very similar issue with parts of a view not working when used with a UIActionSheet. My problem was that the bounds of the UIActionSheet were larger than the view I had added it to.
My solutions was to change where i inserted the sheet
[sheet showInView:self.view];
You may need to add it higher up for example
[sheet showInView:self.view.superview];
Hope this at least sets you on the right track
I had this problem also , i supose you have an UIToolbar behind the picker. Just do
[self.navigationController setToolbarHidden:YES];
while in picker , then
[self.navigationController setToolbarHidden:NO];
hope this help