Dismiss UIActionSheet from AppDelegate - iphone

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.

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.

Having trouble dismissing 2 UIActionSheets

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

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;
}

auto dismiss alert view and switch view (Xcode)

i'm working on an app, but i have some problems with auto dismiss alert view and switch to another views.
Here is my code:
This is file "Progress.h":
#import <Foundation/Foundation.h>
#import "Progress_ToolViewController.h"
#interface Progress : UIAlertView {
UIProgressView *myPro;
//UILabel *myPercent;
UILabel *myCount;
NSTimer *timer;
int count;
BOOL userDismissed;
}
#property (nonatomic, retain)UIProgressView *myPro;
//#property (nonatomic, retain)UILabel *myPercent;
#property (nonatomic, retain)UILabel *myCount;
-(void)start;
-(void)moreprogress;
-(void)makecount;
-(id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
#end
Here is file "Progress.m":
#import "Progress.h"
#import "Progress_ToolViewController.h"
#implementation Progress
#synthesize myPro;
#synthesize myCount;
//#synthesize myPercent;
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{
if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
{
UIProgressView *thePro = [[UIProgressView alloc] initWithFrame:CGRectMake(12.0, 60.0, 220.0, 20.0)];
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(240.0, 55.0, 60.0, 20.0)];
[thePro setBackgroundColor:[UIColor clearColor]];
[theLabel setBackgroundColor:[UIColor clearColor]];
[theLabel setTextColor:[UIColor whiteColor]];
theLabel.text = 0;
[self addSubview:thePro];
[self addSubview:theLabel];
self.myPro = thePro;
self.myCount = theLabel;
myCount.text = #"0";
[self start];
[thePro release];
[theLabel release];
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 65.0);
[self setTransform:translate];
}
return self;
}
-(void)start{
myPro.progress = 0.0;
//myPro.text = #"%";
timer = [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:#selector(moreprogress) userInfo:nil repeats:YES];
timer = [NSTimer scheduledTimerWithTimeInterval:0.20 target:self selector:#selector(makecount) userInfo:nil repeats:YES];
}
-(void)moreprogress{
myPro.progress = myPro.progress + 0.02;
if(myPro.progress == 1.0){
[timer invalidate];
[self dismissAlert];
}
}
-(void)makecount{
count = count + 4;
myCount.text = [[NSString alloc] initWithFormat:#"%i", count];
if(count == 100){
[timer invalidate];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 1) {
if (buttonIndex == 1) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
else if (buttonIndex == 2) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Not sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
}
else if ([alertView tag] == 2) {
userDismissed = YES;
}
}
-(void)dismissAlert{
if(userDismissed) return;
[self release];
}
#end
This is file "Progress_ToolViewController .h":
#import <UIKit/UIKit.h>
#class Progress;
#interface Progress_ToolViewController : UIViewController <UIAlertViewDelegate>{
BOOL userDismissed;
Progress *prog;
}
- (void)showAlert;
#end
This is file "Progress_ToolViewController.m":
#import "Progress_ToolViewController.h"
#import "Progress.h"
#implementation Progress_ToolViewController
- (void)showAlert
{
prog = [[Progress alloc] initWithTitle:#"Your Value:" message:#" " delegate:self cancelButtonTitle:nil okButtonTitle:nil];
prog.tag = 2;
userDismissed = NO;
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(dismissAlert) userInfo:nil repeats:NO];
[prog show];
}
-(void)dismissAlert{
if(userDismissed) return;
[prog dismissWithClickedButtonIndex:0 animated:YES];
[prog release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)alertView:(Progress *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 1) {
if (buttonIndex == 1) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
else if (buttonIndex == 2) {
UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:#"Sub-Alert" message:#"You've chosen \"Not sure\"." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[subAlert show];
[subAlert release];
}
}
else if ([alertView tag] == 2) {
userDismissed = YES;
}
}
- (void)dealloc {
[super dealloc];
}
#end
When i run this app, it's also dismiss but it turns off the program. How can i solve it?
-(void)showAlert
{
prog = [[UIAlertView alloc]initWithTitle:#"Auto alert" message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(dismissAlert) userInfo:nil repeats:NO];
[prog show];
}
-(void)dismissAlert
{
[prog dismissWithClickedButtonIndex:0 animated:YES];
TrackingSetting *_obj = [[TrackingSetting alloc]init];
[self presentViewController:_obj animated:YES completion:nil];
}
Try this sample code it is working fine i have tested and after diss miss of alert you can create another view object and and show over it no issue.
But in your case i think "prog" is a view .so you have to remove that view from superview means it depends on how you are showing that view if you are adding that view using add sub view you have to remove from super view .if you are using present modal view then you have to dismiss that view .