UIActionSheet event not firing - ios

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!

Related

How to get grouped looking UIActionSheet buttons when dynamically adding them

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

Adding View Controller's view to UIAlertView (Objective c)

I have ViewController with UITableView. if i touch on any Cell, it shows information about this Cell in another view. It is working well. But when i add MyViewController's view to UIAlerView's view, if i touch on AlertView, it is giving runtime error.
this is my code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
MyViewController *VC = [[MyViewController alloc] init];
[alert setValue:VC.view forKey:#"accessoryView"];
[alert show];
any help please ...
my program shows like this:
An alert view is simply shown on the screen. It doesn't matter which view controller is currently visible, when the show method is called, the alert view is added as the top view.
The only thing that would matter in terms of view controllers is which one you want as the delegate view controller, which can be assigned to objects other than self.
Refactor your code to look like this:
MyViewController *VC = [[MyViewController alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"" delegate:VC cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
Notice how the VC you want to display is now the delegate to the alert?

How do you call this iOS 7 control?

I am looking to add to my iOS app a control that looks like this menu on the bottom:
Can anybody tell me what control that is? Does not look like Picker View.
That is a UIActionSheet. Official Documentation
It cannot be dragged in from the list of UI elements. It is presented programmatically very similar to a UIAlertView.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Title"
delegate:self cancelButtonTitle:#"Cancel Button"
destructiveButtonTitle:#"Destructive Button"
otherButtonTitles:#"Other Button 1", #"Other Button 2", nil];
[actionSheet show];

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:)

Error with action:#selector in app

I'm creating an app were users should be able to report posts. I want an actionsheet when the user press the report button.
This is my code:
[self.avatarImageView.profileButton addTarget:self action:#selector(didTapReportButtonAction:) forControlEvents:UIControlEventTouchUpInside];
and here is the didTapReportButtonAction code:
- (void)didTapReportButtonAction:(UIButton *)button {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Title" delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Destructive" otherButtonTitles:#"Other1", #"Other2", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet addButtonWithTitle:#"Add"];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
}
But when I try to press the report button now, nothing happens.
Can someone help me with this?
Can you try this:
[actionSheet showInView:self.view];
If avatarImageView is an UIImageView, its userinteraction property is false by default.
this means the event won't called.
make sure avatarImageView.userInteraction = YES

Resources