is possible to remove previous alert view? - ios

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

Related

iOS UIAlertView being shown more than once

Hi I have used the following function to call an alert view with a specific message. The NSLogs are being printed only once but alert view is called more than once.
I am using UITabBarController for any additional info.
- (void)showUIAlertWithMessage:(NSString *)message {
NSLog(#"showUIAlertWithMessage called");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Something went wrong!😕😕"
message:message
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}

Order of executing methods

I have a problem about order of executing methods.
if (indexPath.row == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data will be downloaded"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
When I run this code snippet, I want to first show an alert view. However, it calls the getData method before showing alert view. Once the getData method is completed, alertView comes to the window.
How can I correct this?
The function is called asynchronously therefore appData gets called before the first alert view is visible. Change your code to this:
if (indexPath.row == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data will be downloaded"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
The below method will be called when the user presses OK on your first alert, but it means that while the first alert is visible, your code for appData would not start.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
Note: Remember to add UIAlertViewDelegate in your view controller
I don't have Xcode with me, but I'll take a stab at it anyway. Your problem is because the alert won't show until the main loop iterates. And it won't iterate until your getData method is executed since you're doing this on the main thread. So you need to fork.
Try wrapping your if() in something like this:
dispatch_async(dispatch_get_main_queue(),
^ {
if([app getData]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Data is downloaded."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
});
It's because the run loop must continue running so [UIAlertView show] is asynchronous and will not block the current thread. If the main thread was blocked then no user interface events could be delivered.
If you want something to occur after the first alert view is dismissed then do it within the alertView:clickedButtonAtIndex: delegate method.

UIAlert not showing

Fresh and green newbie here. I just started programming and already got stuck with UIAlert. I've searched on this site but there are so many postings and as I said I am kinda new to all this so I don't know what to really look for. This is what I have.
-(void)showError {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Please try again later. Have a nice day."
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
}
Need to do
[alert show];
to present
Code should be :
-(void)showError {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Please try again later. Have a nice day."
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alert show];
[alert release];
}
[alert show];
I always forget this too. Happy Coding.
-(void)showError
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Please try again later. Have a nice day."
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alert show];
}

Checking to see if textfields are filled out

In my Calorie Tracker App, I have two textfields. One is for the name of the food, and the other is for the amount of calories.To check and see if they are filled I wrote the following code:
if([foodString isEqual: #" "])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You have not entered the name of the meal!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else if([self.amountText isEqual: #" "])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You have not enteted the amount of calories!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else if([self.amountText isEqual:#" "] && [foodString isEqual:#" "])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You haven't entered anything!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
So for the one where the amountText textfield isn't filled out, but the nameOfFood is filled out, there is an alert. For the rest however, there is no alert. If anyone could provide the proper syntax for checking to see if my textfields are being filled out properly, it would be much appreciated.
Remember, my first textfield is a string value, and the other is an integer value.
Thanks in advance.

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