Opening a new view controller after alert - ios

enter code hereI am having trouble with going back to the previous view controller when alert is presented.
What I am trying to do is have the user enter in data, then an alert appear saying it was successful, then return to the previous view controller.
I currently have no code doing so and am seeking assistance with what I should put in.
- (IBAction)saveLabel:(id)sender
{
NSArray *data = [[NSUserDefaults standardUserDefaults] objectForKey:#"DATA"];
NSMutableArray *currentDataArray;
if (data == nil)
{
currentDataArray = [[NSMutableArray alloc]init];
}
else
{
currentDataArray = [[NSMutableArray alloc]initWithArray:data];
}
[currentDataArray addObject:self.textField.text];
[[NSUserDefaults standardUserDefaults] setObject:currentDataArray forKey:#"DATA"];
}
- (IBAction)enterButtonPressed:(id)sender
{
NSLog(#"enterButtonPressed");
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
}

//If u r using dismissing
[self dismissViewControllerAnimated:YES completion:^{
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
}];
//if u r using navigation ,popViewController
[CATransaction begin];
[CATransaction setCompletionBlock:^{
// handle completion here
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
}];
[self.navigationController popViewControllerAnimated:YES];
[CATransaction commit];

Set Delegate self of your UIAlertView
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
Use Delegate Method of UIAlertviewController.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0){
// Do your Stuff Here....
[self.navigationController popViewControllerAnimated:TRUE];
}
}

Add the following UIAlertViewDelegate method to your implementation file:
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex {
// If you are presenting this view controller
[self dismissViewControllerAnimated:YES completion:nil];
// If you are pushing this view controller
[self.navigationController popViewControllerAnimated:YES];
}
Also remember to set your UIAlertView delegate to your view controller, change to the following code:
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];

UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
enterAlert.tag=100;
[enterAlert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 100) {
if (buttonIndex == 0) {
// Do something when ok pressed
// If you are presenting this view controller
[self dismissViewControllerAnimated:YES completion:nil];
// If you are pushing this view controller
[self.navigationController popViewControllerAnimated:YES];
} else {
// Do something for other alertviewButton}
else{// Do something with responses from other alertViews by giving tags
}

Related

UiAlertView should change ViewController

What is wrong with following code? I will change the viewController, when the text in the alertView is not empty. The problem is, it will change the UiViewController every time, even it is empty.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
if ([[alertView textFieldAtIndex:0].text isEqual:#""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error warning" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"ChangeView"];
[self presentViewController:vc animated:YES completion:nil];
}
}
}
You should do this check instead:
if (![[alertView textFieldAtIndex:0].text.length) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error warning" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
} else ...

how to delay a popup page?

I just built an Activity indicator view, and a popup page in Xcode. How can I get a 3 second delay in Activity indicator view, then switch to popup page?
here is my Viewcontroller.m
- (IBAction)Connect:(UIButton *)sender forEvent:(UIEvent *)event
{
[self performSelector:#selector(delay2) withObject:Nil afterDelay:6.0];
[self performSelector:#selector(delay1) withObject:ConnectAct afterDelay:0.0];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"LP01;" message:#"No Connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Help", nil];
[alert show];
}
- (IBAction)ConnectLP02:(UIButton *)sender
{
[self performSelector:#selector(delay2) withObject:Nil afterDelay:6.0];
[self performSelector:#selector(delay1) withObject:ConnectAct afterDelay:0.0];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"LP02;" message:#"No Connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Help", nil];
[alert show];
}
- (void)delay1 {
ConnectAct.alpha = 1.0;
}
- (void)delay2 {
}
- (IBAction)ConnectLP02:(UIButton *)sender
{
[self performSelector:#selector(delay2) withObject:Nil afterDelay:6.0];
[self performSelector:#selector(delay1) withObject:ConnectAct afterDelay:0.0];
}
- (void)delay1 {
ConnectAct.alpha = 1.0;
}
- (void)delay2 {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"LP02;" message:#"No Connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Help", nil];
[alert show];
}

Using UIAlertView for a GetReady title

I am trying to use UIAlertView to pull up a get ready title then pull it off the screen to let the player know the game is starting. How do I do this using UIAlertView?
Try this Methods...
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"Get Ready!" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];;
[alertView show];
[self performSelector:#selector(dismissReadyAlert:) withObject:alertView afterDelay:3.0];
-(void)dismissReadyAlert:(UIAlertView *)alertView
{
[alertView dismissWithClickedButtonIndex:0 animated:NO];
alertView.title = #"Your game is started!";
[alertView show];
[self performSelector:#selector(dismissStartAlert:) withObject:alertView afterDelay:2.0];
}
-(void)dismissStartAlert:(UIAlertView *)alertView
{
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
Thanks!
You need to keep a reference to the alert. Then just call this method:
[alert dismissWithClickedButtonIndex:0 animated:YES];
Declare UIAlertView Object in header file.
UIAlertView *alertReady;
Then Display alertview whenever you like to display and call one method to dismiss alertview
alertReady = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Get Ready" delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
[alertReady show];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(hideAlertView) userInfo:nil repeats:NO];
-(void)hideAlertView
{
[alertReady dismissWithClickedButtonIndex:0 animated:YES];
}

Alertview with integer

Hello to everyone i would like to place my thinking about an alertview. I am thinking to create an Uialertview which will prompt the user to input two integers. Then i would like to retrieve these two integers and put the one on a timer and the second in an sql statement. So if anyone can help on how to implement that i would apreciate it. Thank you all.
Here is my code until now
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0] text]);
[alertView show];
You need to use delegate methods of UIAlterView.
First of all make "self a delegate" instead of nil.
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
Than add this function to your class:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
NSLog(#"%#", field.text);
}
To be 100% correct add also in your header class information that you implement this protocol .
// Try this
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertView textFieldAtIndex:0].delegate = self;
[alertView show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0]text]);
}

UIAlertView keeps re-appearing after dismissWithClickedButtonIndex included in performSelector: withObject: afterDelay:

I have a button which I want to implement with password before triggering a segue if the password is correct. it all looks fine up to the moment when you type in wrong password and I have implemented another alertView to tell the user the password is wrong. When the alert view pops out and dismisses after some delay, it keeps re-appearing and disappearing and nothing else can be done on the screen!
How to stop the re appearing?
Below is my part of the code that deals with this:
- (IBAction)editLeagues:(id)sender {
[self presentAlertViewForPassword];
}
-(void)presentAlertViewForPassword
{
_passwordAlert = [[UIAlertView alloc]initWithTitle:#"Password"
message:#"Enter Password to edit Leagues"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[_passwordAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
_passwordField = [_passwordAlert textFieldAtIndex:0];
_passwordField.delegate = self;
_passwordField.autocapitalizationType = UITextAutocapitalizationTypeWords;
_passwordField.tag = textFieldPassword;
[_passwordAlert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSString *password = [NSString stringWithFormat:#"55555"];
if ( ![_passwordField.text isEqual:password]) {
_wrongPassword = [[UIAlertView alloc] initWithTitle:#"Wrong Password"
message:#"You are not authorised to use this feature!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[_wrongPassword show];
[self performSelector:#selector(allertViewDelayedDissmiss:) withObject:nil afterDelay:2];
}
else
{
[self performSegueWithIdentifier:#"addLeague" sender:[alertView buttonTitleAtIndex:0]];
}
}
-(void) allertViewDelayedDissmiss:(UIAlertView *)alertView
{
[_wrongPassword dismissWithClickedButtonIndex:-1 animated:YES];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 4 )
{
return YES;
}
else
{
return NO;
}
}
[_wrongPassword dismissWithClickedButtonIndex:-1 animated:YES]; will call the delegate method alertView:didDismissWithButtonIndex:
You have two options:
don't set a delegate on the wrong password alert
check for the correct alert in alertView:didDismissWithButtonIndex: e.g.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alert == _passwordAlert) {
NSString *password = [NSString stringWithFormat:#"55555"];
// and so on
}
}
Issue is causing because when you dismiss the wrong password alert it'll also call the didDismissWithButtonIndex delegate method.
Solution 1
Set the delegate of wrong password alert to nil.
wrongPassword = [[UIAlertView alloc] initWithTitle:#"Wrong Password"
message:#"You are not authorised to use this feature!"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
Solution 2
Add a tag to your alertView. And change your methods like:
-(void)presentAlertViewForPassword
{
_passwordAlert = [[UIAlertView alloc]initWithTitle:#"Password"
message:#"Enter Password to edit Leagues"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[_passwordAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
passwordAlert.tag = 7;
_passwordField = [_passwordAlert textFieldAtIndex:0];
_passwordField.delegate = self;
_passwordField.autocapitalizationType = UITextAutocapitalizationTypeWords;
_passwordField.tag = textFieldPassword;
[_passwordAlert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 7)
{
NSString *password = [NSString stringWithFormat:#"55555"];
if ( ![_passwordField.text isEqual:password])
{
_wrongPassword = [[UIAlertView alloc] initWithTitle:#"Wrong Password"
message:#"You are not authorised to use this feature!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[_wrongPassword show];
[self performSelector:#selector(allertViewDelayedDissmiss:) withObject:nil afterDelay:2];
}
else
{
[self performSegueWithIdentifier:#"addLeague" sender:[alertView buttonTitleAtIndex:0]];
}
}
}

Resources