Dismiss alert view in iOS by tapping outside or cancel button - ios

I had a very strange problem, I want to dismiss the UIAlertView when the user either taps on cancel button or anywhere else. I had read almost all the post and couldn't find an answer that solved my problem do any of you have any idea how to do this? Please guide me through the steps, I will be very thankful for this.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:label.text
message:label1.text
delegate:self
cancelButtonTitle:#"ཕྱིར་འཐེན།"
otherButtonTitles:#"ཉེ་ཆར།",#"དགའ་མོས།",#"ཉེ་ཆར་གཙང་བཟོ།",#"དགའ་མོས་གཙང་བཟོ།",nil];
[alert show];

Try this code
// Add gesture
UITapGestureRecognizer *tapOnView = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissAlert)];
[alert addGestureRecognizer:tapOnView];
Then you can call a function that dismiss your alert

Call touchesEnded method. It'll help you dismiss alertView when user tap anywhere else on the screen
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.yourAlert dismissWithClickedButtonIndex:0 animated:YES];
}
and remember to create yourAlert as strong reference

Related

iOS keyboard dismiss and then immediately show

I have UIViewController with UITextView. This TextView always firstResponder.
- (void)viewDidLoad
{
[super viewDidLoad];
[self createBottomBar];
_textView.delegate = self;
[_textView becomeFirstResponder];
}
When user tap "video" button my app show UIAlertView with textfield
- (IBAction)showVideoPicker:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_(#"Link to movie")
message:_(#"Insert a link to YouTube clip or a Coub")
delegate:self
cancelButtonTitle:_(#"Cancel")
otherButtonTitles:_(#"Attach"), nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
When user tap "Attach" button alert dismiss and keyboard dismiss and then immediately show. I would like another scenario: keyboard do not dismiss, because text view should always be a first responder. Just UIAlertView dismiss wihout keyboard dismiss/show animation.
For more understanding see gif here (do not have enough reputation to post images). Sorry for my English.
Have you tried setting the first responder on the Attach button tap via UIAlertView delegate? It's the only way here:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) [_textView becomeFirstResponder];
}
The keyboard might or might not jump a little, but that's all we have.

How to have a UIBarButton execute a custom function?

I'm hoping someone can help me figure this out...I'm a beginner Xcode / Objective-C programmer. I'm working on a app that is a continuation of last semester.
1: So I created a button and I need it to execute this custom function:
- (void)cancelTapped {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
[alert setTag:1];
[alert show];
}
How/where do I put this function? Is it in the button properties? Or would I write this in a custom class/controller and link it to it?
2: How do I get it to listen for the alert to return on: - alertView:didDismissWithButtonIndex:
3: From there, how would I would write the logic to hide the page and pop the view?
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 1 && buttonIndex == 1) {
// Delete data and return to lobby
[self.navigationController popViewControllerAnimated:YES];
}
}
You will need a custom UIViewController to house the logic for your button and alert view interactions. I am assuming you know how to do that.
Once done, assuming you have a reference to your button property in your view controller, you can programatically add a target to your button and pass the selector cancelTapped as a parameter:
[myButton addTarget:self action:#selector(cancelTapped) forControlEvents:UIControlEventTouchUpInside];
Alternatively you could control-drag from the button in your Storyboard to the header file of your custom UIViewController, and define an IBAction. That will create an empty cancelTapped method in your implementation which you could then add your logic in.
As for listening on UIAlertView messages, you will need to make your custom UIViewController a delegate of the UIAlertView by passing "self" as the delegate in the following statement:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"My Message" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
Your CustomViewController should also be declared as a UIAlertViewDelegate.
CustomViewController.h
#interface CustomViewController : UIViewController<UIAlertViewDelegate>
#end
Hope this helps!
By the using VIewWillDisappear method to detect the press of The back button of NavigationItem:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// Navigation button was pressed. Do some stuff
[self cancelTapped];
}
[super viewWillDisappear:animated];
}
- (void)cancelTapped {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
[alert setTag:1];
[alert show];
}
For More info && also Custom UIBArButtonItem Check Here

Objective-C - Where to prompt in UIAlertView on view controller dismissal

When I close a view controller, I want to prompt the user for input. This input is necessary before the dismiss should actually take place.
I tried placing the following code in my view controller:
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Hello!" message:#"Please enter your name:" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
alertTextField.placeholder = #"Enter your name";
[alert show];
[super dismissViewControllerAnimated:flag completion:completion];
}
However, since the UIAlertView does not block and wait, the super call gets executed.
I was debating on removing the super call from this function, and moving it to the alertView:clickedButtonAtIndex: function. I guess I would then need to store the flag and completion variables that are passed into the dismissViewControllerAnimated in the first place, so that I would have them available in the alertView:clickedButtonAtIndex: function.
Is there a better approach, so that I do not have to store these parameter values for the super call?
To me, I dont see why you could not put the call to dismiss in alertView:clickedButtonAtIndex. You could just set the flag parameter to YES or NO, and the completion block parameter to nil.
Of course, this does require prompting the user with UIAlertView before they have actually attempted to dismiss the view controller.
You should intercept the close request before dismissViewControllerAnimated: is called and use that to prevent the call from being made and to show the alert instead. Then, in the alert delegate method call dismissViewControllerAnimated: on self. Don't try cutting out super and calling it later, it will get messy and be a pain to maintain.
You could try making a new method, ala:
-(void) dismissButtonPressed
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Hello!" message:#"Please enter your name:" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
alertTextField.placeholder = #"Enter your name";
[alert show];
}
Then call that method when your dismiss button is pressed (done in different ways depending upon whether you are creating your views programmatically or in Interface Builder). Then, only when the UIAlertView is dismissed
(i.e. in -(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex) you dismiss the view controller.
Create the UIAlertView on the button you press to dismiss now and in the delegate methods of the UIAlertView you deal with the input you need , when you get what you want , simply dismiss the viewcontroller.

How to view another AlertView after dismiss?

can i display another UIAlertview after dismissed by timer? i use this method UIActivityindicatorview into UIAlertView with NSTimer to dismiss it self "please wait, Saving..." dismissed, then i want to view another UIAlertView say "Saved Successfully!" (Done). how to achieve this? thank you.
here i was use same this method but this not what i'm need. i just need view another UIAlertview after the first uialertview dismissed by timer.
please tell me if need more clear i can post my code here thanks.
- (IBAction)showAlert:(id)sender {
UIAlertView* alert_view = [[UIAlertView alloc]
initWithTitle: #"Save" message:#"please wait, saving..." delegate: self
cancelButtonTitle:#"done" otherButtonTitles:nil];
[alert_view show];
[alert_view release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0) {
UIAlertView* alert_view = [[UIAlertView alloc]
initWithTitle: #"Success" message:#"Saved Successfully!" delegate: self
cancelButtonTitle: #"Done" otherButtonTitles:nil];
[alertView dismissWithClickedButtonIndex:0 animated:TRUE];
}
else{
[alertView dismissWithClickedButtonIndex:1 animated:TRUE];
}
}
A timer is not the way to go about this. You are faking it instead of implementing it properly. You need to use protocols and delegates if you have multiple classes involved, but ultimately, you need to follow this flow:
1) Show a "Please wait...Saving" alert
2) Do the saving
3) When saving has completed, dismiss first alert, and show second.
Depending on HOW your are saving WHAT, this may need some extra logic to implement, but without more specifics, it's hard to help much more.

iOS Dev: Segue by pressing a button in an Alert?

I've been searching for a few hours and haven't found any answers, so I would really appreciate your help!
I am designing an app and need to segue to a different view controller when a button in an alert is pressed. I already have the alert set up, but just need the code to segue to a new view.
Try this. create alertview first.
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Open New controller?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alertView show];
[alertView release];
Implement the AlertView Delegate Method
#pragma mark AlertView Delegate
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
Viewcontroller *vc = [[UIViewcontroller alloc] initWithNib:#"Viewcontroller"];
[self presentModalViewController:vc];
[vc release];
}
}
Implement the UIAlertViewDelegate and add the method alertView:clickedButtonAtIndex:. Once the right button is clicked, call the method to segue into the new view.
Implement UIAlertViewDelegate and then you get a callback with which button was pressed when the alert was dismissed. Simply call performSegueWithIdentifier in there.
More details at the UIAlertViewDelegate protocol reference

Resources