Having trouble dismissing 2 UIActionSheets - iphone

I have 2 UIBarButoon in a UIToolbar. Each has an IBAction to launch their own UIActionSheets.
Right now, if I present one, then click the other UIBarButton, the other ActionSheet gets presented as well, so I have two on the screen and they are overlapping. I want one to dismiss when the other is presented, etc.
I have this code now:
- (IBAction)showFirstActionSheet:(id)sender
{
if (!self.eRXActionSheet)
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"eRX Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"New eRX", #"eRX Refills", nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
self.eRXActionSheet = sheet;
[sheet release];
[self.eRXActionSheet showFromBarButtonItem:sender animated:YES];
return;
}
[self.eRXActionSheet dismissWithClickedButtonIndex:self.eRXActionSheet.cancelButtonIndex animated:YES];
self.eRXActionSheet = nil;
}
- (IBAction)showSecondActionSheet:(id)sender
{
if (!self.actionSheet)
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Action Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Help", #"Lock", #"Log Out", nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
self.actionSheet = sheet;
[sheet release];
[self.actionSheet showFromBarButtonItem:sender animated:YES];
return;
}
[self.actionSheet dismissWithClickedButtonIndex:self.actionSheet.cancelButtonIndex animated:YES];
self.actionSheet = nil;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet == self.eRXActionSheet)
{
if (buttonIndex == 0)
{
[self erxButtonClicked];
}
else if (buttonIndex == 1)
{
[self erxRefillButtonClicked];
}
}
else
{
if (buttonIndex == 0)
{
[self helpButtonClicked];
}
else if (buttonIndex == 1)
{
[self lockButtonClicked];
}
else if (buttonIndex == 2)
{
[self logOut];
}
}
}

In your code, before creating an action sheet, your are checking if the action sheet to be displayed is not nil. Logically, this will always evaluate to true.
Rather you should check if the other action sheet is not nil. If so, dismiss it and create the new one.
-(void)buttonForActionSheet1Clicked {
if (actionSheet2 != nil) {
// dismiss it
}
// show actionSheet1
}
-(void)buttonForActionSheet2Clicked {
if (actionSheet1 != nil) {
// dismiss it
}
// show actionSheet2
}
Alternatively, you can force the user to dismiss the present action sheet with the cancel button first by disabling the other button in the toolbar.

Call dismissWithClickedButtonIndex:animated: on the actionsheet you want dismissed.
E.g. in showFirstActionSheet add this line to dismiss the other:
if(self.actionSheet) [self.actionSheet dismissWithClickedButtonIndex:<indexofCancelButton> animated:YES];

Related

UIActionsheet not dismissing

I have 3 buttons in my UIActionsheet.
I want one cancel button at the bottom of it.
I also want that all of the buttons should be able to dismiss UIActionsheet.
Currently, none of them does the job for me.
Here is how I display it:
UIActionSheet *actionSheet = nil;
NSArray *otherButtons = nil;
otherButtons = [NSArray arrayWithObjects:#"Button1", #"Button2", #"Button3",
nil];
actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
for( NSString *btntitle in otherButtons)
{
[actionSheet addButtonWithTitle:btntitle];
}
[actionSheet addButtonWithTitle:#"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
In my delegate, I do this:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//some code
[actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
But it does not dismiss. I do not want to change my design and position of any buttons.
What should I do?
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
[actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
[self performSelector:#selector(OtherOperations) withObject:nil afterDelay:0.0];
}
-(void)OtherOperations{ //some code
}
Be careful when opening an action sheet from a lpgr. Only open it on "Began", not again on "Ended".
- (void) handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.tblYourVids];
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSIndexPath *indexPath = [self.aTable indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(#"long press on table view but not on a row");
else {
NSLog(#"long press on table view at row %d", indexPath.row);
self.selectedCell = [self.aTable cellForRowAtIndexPath:indexPath];
DLog(#"open action sheet only once");
[self showActionSheet];
}
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// eat this one
}
}
you have to add this to yours .h file.
<UIActionSheetDelegate>
Also add this line to yours .m file.
actionSheet.delegate = self;
Let me know, this is working or not. If works, accept it as right answer
just try to use the below code.it will automatically dismiss.
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Printer"
otherButtonTitles: nil];
[sheet showInView:self.view];
[sheet release];
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
let me know whether it is working or not
Happy Coding!!!!!
This is not the right way to cancel UIActionSheet firstly you have to disable to default UIActionSheet by javascipt
and go through this web
http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/

UIAlertView buttons don't work

I'm using an alert view to alert the user when the internet is not connected. I have two buttons in the alert view and they both don't seem to work. I already implemented the in the .h file. I used NSLog to check whether it responds when I click. It responds in the console, but does nothing when the button is pressed. Here's a snippet of my code.
- (IBAction)startButton:(id)sender
{
if (![self connectedToNetwork])
{
UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle: #"Network Error!" message: #"You are not connected to the internet" delegate: self cancelButtonTitle: #"OK" otherButtonTitles: #"Open Settings", nil];
[internetAlert show];
}
}
- (void)alertView: (UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"Ok clicked");
HomeViewController *blah = [self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self.view addSubview: blah.view];
}
else if (buttonIndex == 1)
{
NSLog(#"Settings clicked");
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:#"prefs:root=WIFI"]];
}
}
I used [self.contentView addSubview: blah.view] because I do not have a navigation controllers. Any thoughts on what I'm doing wrong
Here you are using wrong method for UIAlertView button clicked. you have to use another method of UIAlertView.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Please change to the following code and it will work.
- (IBAction)startButton:(id)sender
{
if (![self connectedToNetwork])
{
UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle: #"Network Error!" message: #"You are not connected to the internet" delegate: self cancelButtonTitle: #"OK" otherButtonTitles: #"Open Settings", nil];
[internetAlert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"Ok clicked");
HomeViewController *blah = [self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self.view addSubview: blah.view];
}
else if (buttonIndex == 1)
{
NSLog(#"Settings clicked");
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:#"prefs:root=WIFI"]];
}
}
Please let me know if you still have any questions.

Dismiss UIActionSheet from AppDelegate

In my view, I have an actionsheet that may be presented from a UIBarButton (iPad). If user goes to homescreen and back, app shows a lock screen wehre user must enter password for security. But if the popover wasn't dismised before going into background, it is still showing on top of lockscreen. The UIActionSheet is a property of that VC, not the App Delegate.
The method in my delegate is:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"STATUS - Application did become active");
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
[_notActiveTimer invalidate];
_notActiveTimer = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:#"_application_background" object:self userInfo:NULL];
LockScreen *vc = [[[LockScreen alloc] initWithNibName:#"LockScreen" bundle:nil] autorelease];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
vc.showFullUsernamePasswordLogin = TRUE;
[self.splitView presentModalViewController:vc animated: YES];
}
Code:
- (IBAction)showeRXActionSheet:(id)sender
{
if (self.actionSheet != nil) {
[self.actionSheet dismissWithClickedButtonIndex:0 animated:NO];
}
if (!self.eRXActionSheet)
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"eRX Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"New eRX", #"eRX Refills", nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
self.eRXActionSheet = sheet;
[sheet release];
[self.eRXActionSheet showFromBarButtonItem:sender animated:YES];
return;
}
[self.eRXActionSheet dismissWithClickedButtonIndex:self.eRXActionSheet.cancelButtonIndex animated:YES];
self.eRXActionSheet = nil;
}
- (IBAction)actionButtonClicked:(id)sender
{
if (self.eRXActionSheet != nil) {
[self.eRXActionSheet dismissWithClickedButtonIndex:0 animated:NO];
}
if (!self.actionSheet)
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Action Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Help", #"Lock", #"Log Out", nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
self.actionSheet = sheet;
[sheet release];
[self.actionSheet showFromBarButtonItem:sender animated:YES];
return;
}
[self.actionSheet dismissWithClickedButtonIndex:self.actionSheet.cancelButtonIndex animated:YES];
self.actionSheet = nil;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet == self.eRXActionSheet)
{
if (buttonIndex == 0)
{
[self erxButtonClicked];
}
else if (buttonIndex == 1)
{
[self erxRefillButtonClicked];
}
}
else
{
if (buttonIndex == 0)
{
[self helpButtonClicked];
}
else if (buttonIndex == 1)
{
[self lockButtonClicked];
}
else if (buttonIndex == 2)
{
[self logOut];
}
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
self.eRXActionSheet = nil;
self.actionSheet = nil;
}
This should do what you want.
-(void)dismissSheet{
if (self.actionSheet){
[self.actionSheet dismissWithClickedButtonIndex:0 animated:NO];
}
}
-(void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dismissSheet) name:UIApplicationWillResignActiveNotification object:nil];
// Your other setup code
}
you could also use the UIApplicationDidEnterBackgroundNotification, what ever makes sense for you application flow.
remember to remove observer in dealloc.

UIAction Sheet Not Dismissing When Pressing UIBarButton

I am presenting a UIActionSheet from a UIBarButtonItem. I want the action sheet to dismiss when I click the bar button again, instead it is creating a new one each time layered on top of each other. Any ideas?
- (IBAction)actionButtonClicked:(id)sender
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:#"Action Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Help", #"Lock", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showFromBarButtonItem:sender animated:YES];
[popupQuery release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
[self erxButtonClicked];
}
else if (buttonIndex == 1)
{
[self erxRefillButtonClicked];
}
}
I would declare a #property for popupQuery on my class and use it to keep track of the action sheet.
- (IBAction)actionButtonClicked:(id)sender
{
// If self.popupQuery is not nil, it means the sheet is on screen and should be dismissed. The property is then set to nil so a new sheet can be created next time.
if (self.popupQuery)
{
[self.popupQuery dismissWithClickedButtonIndex:self.popupQuery.cancelButtonIndex animated:YES];
self.popupQuery = nil;
return;
}
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Action Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Help", #"Lock", nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
sheet.delegate = self;
self.popupQuery = sheet;
[sheet release];
[self.popupQuery showFromBarButtonItem:sender animated:YES];
}
// Implementing this method to be notified of when an action sheet dismisses by means other than tapping the UIBarButtonItem. We set the property to nil to prepare for lazy instantiation next time.
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
self.popupQuery = nil;
}

Creating two action sheets in one view

I created two action sheets in one view. There are two buttons, each will initiate one action sheet.
The problem: when i press on first choice in both action sheets the same action is triggered.
Here's my code:
-(IBAction) ChangeArrow:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Change Arrow"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Red"
otherButtonTitles:#"Blue",#"Black",nil];
[actionSheet showInView:self.view];
[actionSheet release];}
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex ==[actionSheet destructiveButtonIndex]) {
self.bar.image=[UIImage imageNamed:#"red"];
}
else if(buttonIndex == 1){
self.bar.image=[UIImage imageNamed:#"blue"];
}
else if(buttonIndex == 2){
self.bar.image=[UIImage imageNamed:#"dark"];}
}
//Second Action sheet:
-(IBAction) Background:(id)sender{
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:#"Change Background"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Sky"
otherButtonTitles:#"Thumbs",#"Smiley",nil];
[actionSheet2 showInView:self.view];
[actionSheet2 release];
}
- (void) actionSheet2: (UIActionSheet *)actionSheet2 didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex ==[actionSheet2 destructiveButtonIndex]) {
self.background.image=[UIImage imageNamed:#"sky"];
}
else if(buttonIndex == 1){
self.background.image=[UIImage imageNamed:#"thumbs"];
}
else if(buttonIndex == 2){
self.background.image=[UIImage imageNamed:#"smiley"];}
}
Set the tag property on each actionsheet to a different value. Then you can check sender.tag to see which action sheet called your method.
Ex.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Change Arrow"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Red"
otherButtonTitles:#"Blue",#"Black",nil];
actionSheet.tag = 1;
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:#"Change Arrow"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Red"
otherButtonTitles:#"Blue",#"Black",nil];
actionSheet2.tag = 2;
Then
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if(actionSheet.tag == 1) {
//do something
} else if(actionSheet.tag == 2) {
//do something else
}
}