How To Add Delay Before Presenting Second Alert View - ios

Im displaying two Alert Views one after another in succession with the following:
-IBAction
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"my message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert setDelegate:self];
[alert show];
}
- (void)didPresentAlertView:(UIAlertView *)alertView
{
[alertView setTitle:#"My new title"];
[alertView setMessage:#"My new message"];
}
The transition from the first Alert View to the second is so fast the user would not have time to read the fist message. Could someone advise how to add a delay between the Alerts. I Think I need to implement an NSTimer but implementing this is where I could use some advice.

I would suggest using dispatch_after, which can be inlined:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// code to be executed on the main queue after delay
});

Try this simple method:
- (void)alertView
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"Get Ready!" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alertView show];
[self performSelector:#selector(dismissStartAlert:) withObject:alertView afterDelay:5];
}
-(void)dismissStartAlert:(UIAlertView *)alertView
{
[alertView dismissWithClickedButtonIndex:0 animated:YES];
[alertView setTitle:#"My new title"];
[alertView setMessage:#"My new message"];
[alert show];
}

Related

Opening a new view controller after alert

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
}

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

is possible to remove previous alert view?

i have create two UIAlertView views in one method. Code like below
-(void) alert{
UIAlertView *alert_1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 1” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_1 show];
UIAlertView *alert_2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 2” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 show];
}
after call this method. iPhone app will appear popup for 2 times.
first appear is alert_1, disappear alert_1 and appear alert_2
after user press ok button in alert_2 thn appear alert_1
should be remove alert_1 when appear alert_2
is possible to remove previous alert view?
Send message - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated to alert1.
UIAlertView *alert_1 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 1” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_1 show];
[alert_1 dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *alert_2 = [[UIAlertView alloc] initWithTitle:#"Message" message:#“Alert 2” delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 show];
I am not clear about what your requirement is. But from what I understood you want alert_2 to popup first and when clicked on the "OK" button you want to dismiss that alert view and popup alert_1
- (void) alertview
{
alert_1 = [[UIAlertView alloc] initWithTitle:#"Alert 1" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert_1 setTag:1];
alert_1.delegate = self;
alert_2 = [[UIAlertView alloc] initWithTitle:#"Alert 2" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_2 setTag:2];
alert_2.delegate = self;
[alert_2 show];
}
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==2)
{
[alert_1 show];
}
}
Please note to declare your alert views in your .h file
Get UIAlertView by its tag or #Property and use this [myAlertView dismissWithClickedButtonIndex:-1 animated:YES];

UILongPressGestureRecognizer Show Alert On Textfield

I have a UILongPressGestureRecognizer added to a UITextField. When I press the UITextField it is show me alert but that is three alert show me.
That is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *gs = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(AlertServer:)];
gs.delegate = self;
[_companyidTxt addGestureRecognizer:gs];
[gs release];
}
-(void)AlertServer:(UILongPressGestureRecognizer *)gs
{
alertView = [[UIAlertView alloc] initWithTitle:#"Server" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Okay", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
myTextField = [alertView textFieldAtIndex:0];
myTextField.text=mainString;
[alertView show];
[alertView release];
[alertView retain];
}
Can anyone explain why this happens, and how it can be prevented?
Thanx in advance
Try this,
- (void)AlertServer:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
alertView = [[UIAlertView alloc] initWithTitle:#"Server" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Okay", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
myTextField = [alertView textFieldAtIndex:0];
myTextField.text=mainString;
[alertView show];
[alertView release];
}
else if (sender.state == UIGestureRecognizerStateBegan){
NSLog(#"UIGestureRecognizerStateBegan.");
//Do Whatever You want on Began of Gesture
}
}
Change your longPressGestureRecognizer.minimumPressDuration based on your observations (The time interval is in seconds. The default duration is is 0.5 seconds.) or use some flag to check if Alert is already shown.

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

Resources