Need help calling a method after pressing a button from a UIActionSheet - iphone

I have a UIActionSheet that I am able to successfully call from a UIBarButtonItem in my app. However, when I click on a button that is on the UIActionSheet, the method that I am trying to get it to call does not appear to be called, and the UIActionSheet goes away. My relevant code looks like this:
In my MapViewController.h file:
#interface MapViewController : UIViewController<MKMapViewDelegate, UIActionSheetDelegate>{
In my MapViewController.m file:
-(IBAction)showActionSheet {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Share your favourite restaurant with your friends" delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"EMail",#"Facebook",#"Twitter",nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0)
NSLog(#"You chose email!");
if(buttonIndex == 1)
NSLog(#"You chose facebook!");
if(buttonIndex == 2)
NSLog(#"You chose twitter!");
}
Can anyone see where I am going wrong? My personal hunch is that it may have something to do with how I am implementing the UIActionSheetDelegate interface.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Share your favourite restaurant with your friends" delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"EMail",#"Facebook",#"Twitter",nil];
should be
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Share your favourite restaurant with your friends" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"EMail",#"Facebook",#"Twitter",nil];
And also make sure your .h file implements the UIActionSheetDelegate protocol

You are passing nil as the delegate, but the delegate is your view controller, so pass self:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Share your favourite restaurant with your friends" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"EMail",#"Facebook",#"Twitter",nil];

May be you forget to set the delegate , Add below in your code
actionSheet.delegate = self ;
From the Documentation ...
The receiver’s delegate or nil if it doesn’t have a delegate.
#property(nonatomic, assign) id<UIActionSheetDelegate> delegate

Related

Show UIAlertView after UIActionSheet button pressed

After the user clicks a button I want an action sheet to come up asking for them to choose between two options. Once they have selected an option, I want an AlertView to come up telling them they will leave the application and have them choose to cancel to operation or continue to the other application.
Code is as follows:
.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface mapView : UIViewController <MKMapViewDelegate, UIActionSheetDelegate, UIAlertViewDelegate>
#property (nonatomic, weak)IBOutlet MKMapView *mapView;
#property (nonatomic, weak)IBOutlet UIBarButtonItem *getDirections;
- (IBAction)selectDestination:(id)sender;
- (void)checkLeave;
#end
.m
- (IBAction)selectDestination:(id)sender
{
UIActionSheet *selectDestinationAS = [[UIActionSheet alloc] initWithTitle:#"Select Destination: " delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Destination 1", #"Destination 2", nil];
[selectDestinationAS showInView:self.view];
}
- (void)checkLeave
{
UIAlertView *checkLeaveAlert = [[UIAlertView alloc] initWithTitle:#"Leave CDSI?" message:#"This will open the Maps application to continue directions. Are you sure you want to continue?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Open", nil];
[checkLeaveAlert show];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self checkLeave];
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString: #"Destination 1"]) {
NSURL *BOHMDirections = [NSURL URLWithString:#"http://maps.apple.com/?daddr=Destination1&saddr=Current+Location"];
[[UIApplication sharedApplication] openURL:BOHMDirections];
} else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString: #"Destination 2"]) {
NSURL *BOPDirections = [NSURL URLWithString:#"http://maps.apple.com/?daddr=Destination2&saddr=Current+Location"];
[[UIApplication sharedApplication] openURL:BOPDirections];
}
}
The ActionSheet shows up, when an option is selected the Maps app opens (as desired) but the AlertView shows up only after you reenter the original app. How do I get it to show up before I leave the app?
Navigate to the external app on UIAlertView delegate.
To pass the selected item index at UIActionSheet, pass the index as parameter in checkLeave method and set as tag to the UIAlertView
By this way, the UI execution will be, on ActionSheet Clicked, the alertview ask confirmation with user. Once the user confirms, the navigation will be performed based on the action sheet selection. To hold, the action sheet selection, we are passing that data as tag.
If you need, you can add a private property to hold the item data clicked and access it in UIAlertViewDelegate.
- (void)checkLeave :(NSInteger)index
{
UIAlertView *checkLeaveAlert = [[UIAlertView alloc] initWithTitle:#"Leave CDSI?" message:#"This will open the Maps application to continue directions. Are you sure you want to continue?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Open", nil];
[checkLeaveAlert setTag:index];
[checkLeaveAlert show];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self checkLeave : buttonIndex];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1){
//Destination 1 clicked
}
}
Similar to the action sheet, UIAlertView has a delegate protocol that you should implement/comply with. Especially the method alertView:didDismissWithButtonIndex: is of interest.
Call the Maps app there and not in clickedButtonAtIndex

UIActionSheet, how to dismiss it with a single click?

UIActionSheet, how to dismiss it with a single click?
I have to click a button 2 times so it gets dismissed, what should I do to make it dismissble after the first click?! Here is my code:
-(void)buttonHeld:(id)sender
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:#"Delete Contact?!" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Delete" otherButtonTitles: nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];
[popupQuery release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(#"Delete button clicked Button Clicked");
else if (buttonIndex == 1) {
NSLog(#"Cancel Button Clicked");
}
}
Based on the name of your method "buttonHeld", my assumption is that you are trying to launch the UIActionSheet from a UILongPressGestureRecognizer. You will need to use the code listed below if you want it to work as expected. The problem stems from UILongPressGestureRecognizer being a 'continuous gesture' which fires multiple times and progresses through several different states. You'll need to monitor for the correct state and then load your UIActionSheet. I had the exact same problem and this is what solved it for me.
- (IBAction) buttonHeld:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
UIActionSheet *popupQuery = [[UIActionSheet alloc]
initWithTitle:#"Delete Contact?!"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Delete"
otherButtonTitles:nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];
}
}
This is how i dismiss the actionsheet...
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
Hope this helps
It took me a few taps to get it to cancel as well. The problem ended up being which view I attached it to.
[action showInView:self.view];
self.view was only referenced in IB. I changed it to a different view and it worked.
No need to declare in this delegate when you alloc the actionsheet you have make cancel Button that by default dismiss the ActionSheet some thing like this
[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:btnTitle,#"Send for a date range",nil];
from any other event you can use this method
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
In your code there is a syntax error you are putting else block in if block.
If u're developing for the iphone it's always safe to use :
[popupQuery showFromBarButtonItem:sender animated:YES]
or showFromTabBar: or showFromToolBar etc. instead of showinView.

clickedButtonAtIndex in appdelegate is not called

I am calling UIAlert with 2 buttons "Cancel" and "OK" in MyapplicationAppDelegate.m file , the alert is called but on tap of "Cancel" or "OK" button
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
method is not called.
I have added UIAlertViewDelegate in the MyapplicationAppDelegate.h file as below
#import UIKit/UIKit.h
#interface MyapplicationAppDelegate: NSObject UIApplicationDelegate,UIAlertViewDelegate
{
..
}
I want to know what else is required.
I am not sure whether its your typo while posting the question but you should call the delegate within <.. , .. >
#interface MyapplicationAppDelegate: NSObject <UIApplicationDelegate,UIAlertViewDelegate> { .. }
Here is sample code for UIAlertView
UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:#""message:#"Your message goes here" delegate:self cancelButtonTitle:#"OK"otherButtonTitles:nil];
[myAlertView show];
[myAlertView release];
I am not sure myAlertView.delgate = self is the right method but i do set delegate with initWithTitle
For me,
#define appDelegate ((AppDelegate*)[UIApplication sharedApplication].delegate)
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
message:#"Say Yes or NO?" delegate:appDelegate
cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
alertView.tag = 1;
[alertView show];
[alertView release];
do the trick!
Now its going into, -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; in AppDelegate file without adding,
#interface MyapplicationAppDelegate: NSObject <UIApplicationDelegate,UIAlertViewDelegate> { .. }

How to make UIBarButtonItem give menu-options?

You know how if you click and hold on a link in Safari (for iPhone obviously) it gives you a list of options like "open in new window", "Open". "Copy" etc?
How do you call this and is it possible to get a UIBarButtonItem to do this (but every time it is clicked not just when held down)?
This is how you show this menu:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel",
destructiveButtonTitle:nil
otherButtonTitles:#"Button 1", #"Button 2", #"Button 3", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:[self view]];
[actionSheet release];
In order to connect it to UIBarButtonItem just point the bar button item to a selector that will include the code above.
Don't forget to implement - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex method of UIActionSheetDelegate.

UIActionsheet Locks iphone screen

i am new to iphone...i have coded in viewdidload
-(void)viewDidload
{
actSheet = [[UIActionSheet alloc] initWithTitle:#"How"
delegate:self
cancelButtonTitle:#"ok"
destructiveButtonTitle:nil
otherButtonTitles:nil];
}
when one button is pressed,
i coded like
[actSheet showInView:self.view];
but, the screen is full like locked,i could not do nothing....anyhelpplease?
At minimum you must implement the UIActionSheetDelegate method – actionSheet:clickedButtonAtIndex: in the object you have set as delegate (self). The sheet is automatically dismissed after the method is invoked.
Where is your
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
?