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

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.

Related

Dismiss alert view when performing delegate method

hi there when i run my delegate method which is parsing json data the alert view appears to freeze whilst it is performing the method is there anyway to hide the alert view whilst the app is running the code I've tried
- (IBAction)btnAdd:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Add Source" message:#"Enter the web address of the json data" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert setTag:0];
[alert dismissWithClickedButtonIndex:-1 animated:YES];
[alert show];
}
this doesn't actually do anything.
any advice?
*UPDATE
in the delegate method i get the same result
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 0) {
if (buttonIndex == 1) {
NSString *textEnteredraw = [[alertView textFieldAtIndex:0] text];
[alertView dismissWithClickedButtonIndex:-1 animated:YES];
From method
- (IBAction)btnAdd:(id)sender
Remove
[alert dismissWithClickedButtonIndex:-1 animated:YES];
Because as #Dima said, you are dismissing the alertView before you are even showing it.
You call the code to hide the alert before you even show it. This method is meant to be called after the alert is shown.

How do i stop/cancel the segue when UIAlertView triggered

I got a button that leads to a tableViewController.
I created UIAlertView that gets triggered when the resultArray is null, which also means there is nothing to show on tableView but when user clicks ok, nothing changes and it goes to empty tableView.
How can I stop going into the other view controller?
this is my code
if (refilteredArray.count==0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No result"
message:#"Please enter a different combination."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
The segue is not triggered with code, its a button triggered IBAction
- (IBAction)motorButton:(id)sender {
...
}
you can use following method
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
in the method body you can verify if your array is empty or not and if it is return NO. To be more specyfic you should check your segue identifier (remember to add it to your segue in IB first)
You need to go for if.. else block
if (refilteredArray.count==0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No result"
message:#"Please enter a different combination."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}else{
[self performSegueWithIdentifier:#"yourSegue" sender:sender];
}
For more info, check this link about segue

Show Next Screen Only After User Closes Alert

I'm working on a losing screen and I'd like to let users view their game before showing the stats screen. I've decided to use an alert to show users the current screen. After they hit OK, they should be taken to the stats screen.
The problem is, the stat screen pops up at the same time as the alert. How can I make sure the stats screen opens after the user closes the alert?
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
You need to use delegate method of UIAlertView and also need to set delegate of UIAlertView = self,
Following is delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
check buttonIndex in this method it's give you NSInteger and manage click of button.
For more information read this UIAlertView Delegates.
You need to set the alert view delegate. In this example self will conform to the UIAlertViewDelegate protocol.
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.delegate = self;
[alert show];
}
You can then listen to a delegate method for when the alert view is dismissed :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
For this situation need to use delegate method,
For simple Example Refer Here: http://www.idev101.com/code/User_Interface/UIAlertView.html

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

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.

Resources