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
Related
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];
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:)
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!
I've noticed in my app that my cancel button is hard to tap, it seem like the hit area is not in the center.
How do I fix this ?
Heres my code...
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:dg
cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles: #"Help Pages", #"Tutorial",
#"Feedback / Questions ", #"Facebook Group", #"About", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet setTag:CommonUIActionSheetHelp];
[actionSheet showInView:vw];
[actionSheet release];
I think your problem lies in the [actionSheet showInView:vw];, perhaps you are using tabBarController/toolBar in you application(at bottom), this error occurs at that time.
you should use either showFromToolbar or showFromTabBar as per your design. If your design is different then please mention it.(if no tabBar/toolBar there).
Thanks
agree whit Ravin answer and you can try
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
it can help also
I have similar problem in iOS 8, using UIAlertController with style UIAlertControllerStyleActionSheet.
The problem is that the first tap on the action sheet is ignored.
Turned out it got to do with my text field's keyboard interfering with the touches.
The fix is to dismiss the keyboard first, then present the action sheet.
[textfield resignFirstResponder];
[self presentViewController:alert animated:YES completion:nil];
I have a method called -showMoreTools: which is: - (IBAction) showMoreTools:(id)sender {
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"Close" otherButtonTitles:#"Add Bookmark", #"Add to Home Screen", #"Print", #"Share", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleDefault;
popupQuery.dismiss
[popupQuery showFromBarButtonItem:moreTools animated:YES];
[popupQuery release];
}
When an user taps a UIBarButtonItem it displays that UIActionSheet, but then, if the user wants to close the UIActionSheet without taping the Close button, (taping the UIBarButtonItem, then it displays the UIActionSheet over the first UIActionSheet.
It's possible to implement somehow taping another time the UIBarButtonItem to close the UIActionSheet?
Thank you so much – I'm a newbie in iOS Programming!
In order to dismiss it when you click on the button twice, you need to keep track of the currently displaying ActionSheet. We do this in our iPad app and it works great.
In your class that has the showMoreTools, in the header put:
#interface YourClassHere : NSObject <UIActionSheetDelegate> {
UIActionSheet* actionSheet_; // add this line
}
In the class file, change it to:
-(IBAction) showMoreTools:(id)sender {
// currently displaying actionsheet?
if (actionSheet_) {
[actionSheet_ dismissWithClickedButtonIndex:-1 animated:YES];
actionSheet_ = nil;
return;
}
actionSheet_ = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"Close" otherButtonTitles:#"Add Bookmark", #"Add to Home Screen", #"Print", #"Share", nil];
actionSheet_.actionSheetStyle = UIActionSheetStyleDefault;
[popupQuery showFromBarButtonItem:moreTools animated:YES];
[actionSheet_ release]; // yes, release it. we don't retain it and don't need to
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
// just set to nil
actionSheet_ = nil;
}
I found another solution to this. The problem is that when using showFromBarButtonItem, the toolbar view is automatically added to the popover's list of passthrough views. You can modify (and clear) the passthrough views when using a UIPopoverController directly, but not when it's presented as part of a UIActionSheet.
Anyway, by using showFromRect, there is no toolbar that the popover can automatically add to its passthrough views. So if you know the (approximate) rectangle where your button bar is, you can use something like:
CGRect buttonRect = CGRectIntersection(toolbar.frame, CGRectMake(0, 0, 60, self.frame.size.height));
[popupQuery showFromRect:buttonRect inView:self animated:YES];
In the above example, my button is on the left hand side of the toolbar.
try by setting the flag(YES/NO)
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
use
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
My method is similar to christophercotton's.
In my showActionSheet I check if the actionsheet is visible rather than instantiated:
- (IBAction)showActionSheet:(id)sender
{
if ([self.fullActionSheet isVisible]) {
[self.fullActionSheet dismissWithClickedButtonIndex:-1 animated:NO];
_fullActionSheet = nil;
return;
}
//actionsheet code
}