How to get grouped looking UIActionSheet buttons when dynamically adding them - ios

Normally, when I add buttons to UIActionSheet statically like so:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles: #"Yu", #"Gi", #"Oh", nil];
The appearance of the UIActionSheet will appear grouped like so, with the Cancel buttons a separate group:
However, as I would need to add my buttons dynamically depending on user actions, I found out that if I do so like this:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet addButtonWithTitle:#"Yu"];
[actionSheet addButtonWithTitle:#"Gi"];
[actionSheet addButtonWithTitle:#"Oh"]; //This is just an example, could add more or less
The resulting UIActionSheet looks like this:
How would I be able to add buttons dynamically but still have them appear grouped as one would when adding buttons statically?
Thanks!

You need to set the Cancel button just like the others:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet addButtonWithTitle:#"Yu"];
[actionSheet addButtonWithTitle:#"Gi"];
[actionSheet addButtonWithTitle:#"Oh"]; //This is just an example, could add more or less
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];

Related

UIActionSheet show black line while presenting on iOS 7

UIActionSheet' was displaying properly in my one of the application until iOS 7 release. But now when I open UIActionSheet in iOS 7, it shows black line` on top corner of action sheet. As you can see in below image
After spending ample amount of time to find its reason, Finally I found that if we display UIActionSheet title (set title property with some text) then those black line removed by iOS 7. like image
But As per requirement I don't want to display UIActionSheet title. So Is there any other approach to remove those black lines in corner? Or its an iOS 7 bug?
- (IBAction) showActionsheetWithoutTitle:(id)sender {
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Button1", #"Button2",#"Button3", nil];
[actionsheet showInView:self.view];}
Replace #"" with nil for initWithTitle, as it takes a small gap between the title and other buttons:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:#"Button1", #"Button2",#"Button3", nil];

Remove Padding from UIActionSheet Popover

I am creating a UIActionSheet to allow users to Take or Select a photo. Can someone tell me why it does not display the same as Apple's? This is for the iPad, so I have not declared a Cancel button.
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
actionSheet.delegate = self;
[actionSheet addButtonWithTitle:NSLocalizedString(#"choosePhotoPopoverButton", #"Choose Photo - Get a photo from the album.")];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
[actionSheet addButtonWithTitle:NSLocalizedString(#"takePhotoPopoverButton", #"Take Photo - Use camera to take photo.")];
}
[actionSheet showFromRect:self.imageView.frame inView:self animated:YES];
Apple's Contacts app. Tight margins. Line between rows.
My working app example. Extra padding at bottom. No line between rows.
Thanks for any insight.
Try this
UIActionSheet *actionSheet;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate: self cancelButtonTitle:#"" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo",#"Camera", nil];
}else{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate: self cancelButtonTitle:#"" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", nil];
}
[actionSheet showFromRect:self.imageView.frame inView:self animated:YES];
You still need to declare a cancel button. When running on the iPad it will simply ignore the cancel button, whereas on iPhone it will use it
I wanted to maintain the flexibility of using addButtonWithTitle, but unfortunately on the iPad, it doesn't show a line above the bottom button. This did the trick. The iPad automatically chops off the last button if there's a cancel button. If you're on the iPad, you don't need a Cancel button.
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
actionSheet.delegate = self;
[actionSheet addButtonWithTitle:NSLocalizedString(#"choosePhotoPopoverButton", #"Choose Photo - Get a photo from the album.")];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
[actionSheet addButtonWithTitle:NSLocalizedString(#"takePhotoPopoverButton", #"Take Photo - Use camera to take photo.")];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:(#"Cancel");
[actionSheet showFromRect:self.imageView.frame inView:self animated:YES];

IOS UIActionSheet Button behave as a CancelButton?

I'm creating an UIActionSheet:
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Choose region/city"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil, nil];
for (Cities *c in self.cities) {
[actionSheet addButtonWithTitle:c.name];
}
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
I'm just setting the cities names as the buttontitle. The Cities array length is 8.
The output is fine, but when I click on the first Button ( this one is called Auckland) it says it is a CancelButton.
The other buttons and the cancelbutton works fine, only the first one not. Both the Auckland button and the CancelButton got a buttonIndex of 0.
Can anyone tell me why this is? And how to fix it?
PS:
If i do cancelButtonTitle:nil, its working fine. But i would like the Cancel button:)

UIActionSheet event not firing

I have a UIActionSheet to provide user options on a table view controller. I have implemented this before without issue, but for some reason now, my event handler does not fire when a button is clicked. In my header file, I have implemented the proper delegate as follows:
#interface GaugeDetailTableViewController : UITableViewController <UIActionSheetDelegate>
In my implementation, I have the following code to support the action sheet:
-(void)loadPopup{
UIActionSheet *popup = [[UIActionSheet alloc]
initWithTitle:#"Options"
delegate:nil
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Add to Favorites", #"Gauge Detail Help", #"Main Menu", nil];
popup.actionSheetStyle = UIActionSheetStyleDefault;
[popup showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"%d\n", buttonIndex);
}
The action sheet appears properly on the view controller, but for some reason, the action sheet click event handler is never reached. Any suggestions? Thanks!
You need to set the delegate:
UIActionSheet *popup = [[UIActionSheet alloc]
initWithTitle:#"Options"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Add to Favorites", #"Gauge Detail Help", #"Main Menu", nil];
popup.actionSheetStyle = UIActionSheetStyleDefault;
It looks like you have forgotten to set the delegate. You need to implement UIActionSheetDelegate (which it looks like you have) and then set the delegate!

Odd artifact next to UIActionSheet in iOS7

My iOS7 app has an odd visual bug in a UIActionSheet. It is shown below. Any thoughts?
Code:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Done" destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.view];
Looks like an iOS7 bug to me in drawing of the button layer, can't really think of a solution but aside trying to work out the factors that have caused that to happen
Maybe try a different approach and see if you have some success?
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet addButtonWithTitle:#"Done"];
[actionSheet showInView:self.view];
Or maybe set it under otherButtonTitles?
It's a strange one but if you can find the cause it might be worth submitting the bug report to apple

Resources