objective-c UIAlert button not working - iphone

I am a newbie at objective-c and I have an UIAlert that is inside a function like so:
- (void)loadJSON
{
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Announcement" message: #"No Connection" delegate: nil cancelButtonTitle:#"OK" otherButtonTitles:nil]; [alert show]; [alert release];
});
} else {
//some code
}
}
and when the user clicks the OK button this function should fire up to recall the loadJSON function? (atleast I think so)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self loadJSON];
}
My end goal here is if there is no internet connection, display UIAlert, user sees message, clicks okay, if still no internet connection, display alert. I disconnected from the internet and the alert message only appears once.
Is there something wrong with my code?

Not assigned delegate for alertView.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Announcement"
message: #"No Connection"
delegate: self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];

Related

Number keypad jumping after UIAlertview

I have a login screen that shows an alert when the user gets the password wrong on a number of occasions. On clicking one of the buttons, another alert view is presented to confirm, if I press the cancel button on this second alert view, the number keypad bounces from the bottom of the screen back up to its original position. No code is being executed during this second alert response. Can someone help?
if (loginCount < 5) {
// Display alert to user
UIAlertView *loginError = [[UIAlertView alloc] initWithTitle:#"Login Failure" message:#"You have entered an incorrect passcode. Please try again." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[loginError show];
} else {
// Display alert to user, including option to reset the app as they have 5 or more login failures
UIAlertView *loginError = [[UIAlertView alloc] initWithTitle: #"Login Failure" message: #"You have entered an incorrect passcode on 5 or more occasions. Please try again or reset the app." delegate: self cancelButtonTitle: #"Try Again" otherButtonTitles: #"Reset App", nil]
[loginError show];
}
// Clear password fields
[self clearPasswordFields];
[passcode1 becomeFirstResponder];
// Increment the login count
loginCount++;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// If the user has chosen to reset the app, alert with a confirmation first before resetting
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"Reset App"]) {
// Create alert to give the user the choice to confirm reset
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Confirm Reset" message:#"Are you sure you wish to Reset?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
} else if ([title isEqualToString:#"Yes"] && buttonIndex == 1) {
[Utilities resetApp];
[self dismissViewControllerAnimated:NO completion:nil];
}
}
This is how i will do it.
in the header file:
UIAlertView *loginError;
in the implementation file:
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if (loginCount < 5) {
// Display alert to user
loginError = [[UIAlertView alloc] initWithTitle:#"Login Failure" message:#"You have entered an incorrect passcode. Please try again." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[loginError show];
} else {
// Display alert to user, including option to reset the app as they have 5 or more login failures
loginError = [[UIAlertView alloc] initWithTitle: #"Login Failure" message: #"You have entered an incorrect passcode on 5 or more occasions. Please try again or reset the app." delegate: self cancelButtonTitle: #"Try Again" otherButtonTitles: #"Reset App", nil]
[loginError show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// If the user has chosen to reset the app, alert with a confirmation first before resetting
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"Reset App"]) {
[self showSecondAlert];
} else if ([title isEqualToString:#"Yes"] && buttonIndex == 1) {
[Utilities resetApp];
[self dismissViewControllerAnimated:NO completion:nil];
}else{
[self clearPasswordFields];
[passcode1 becomeFirstResponder];
}
}
-(void)showSecondAlert
{
//make sure you dismiss the old alertview first
[loginError dismissWithClickedButtonIndex:0 animated:NO];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Confirm Reset" message:#"Are you sure you wish to Reset?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
}
Hope this helps...

how to clear the text field after alert is generated and we press "OK"

I have a login screen and when userid or password is incorrect, an alert is generated. Now when the user presses "OK" it returns back to login screen but doesn't clear the text field and still has the previous input.
How to remove it?
my code for alert msg is:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login Failed"
message:#"Login id or password is incorrect"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
Thanks.
You should set delegate to self
Your alert will be :
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login Failed" message:#"Login id or password is incorrect" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[message show];
and use :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
{
[yourTextField setText:#""];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//cancel clicked ...do your action
}
}
add UIAlertViewDelegate in .h file and add this into .m file
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login Failed"
message:#"Login id or password is incorrect"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==alertView.cancelButtonIndex)
{
[yourTextField setText:#""];
}
}
try this one in your login screen go to viewWillDisappear and make you textfield to nil, it may solve your problem

Is it possible to show alert in `clickedButtonAtIndex` method of alertview

In delegate method ofUIalerview I am trying to show another alert after finishing the process on clicking button index. Is it possible to do so? Also I want to call a method on button click of alert. How can I do that?
I am trying in this way. Is is correct?
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchasedone show];
[purchasedone release];
}
}
You want to use tags for your UIAlertViews
Assigning tags
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Do something first" message:#"This is the first UIAlertView" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[firstAlert setTag: 0];
[firstAlert show];
[firstAlert release];
Handling those tags
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (alertView.tag)
{
case 0: /* firstAlert */
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchaseDone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchaseDone setTag: 1];
[purchaseDone show];
[purchaseDone release];
}
}
break;
case 1: /* purchaseDone */
{
/* purchaseDone uialertview was triggered, handle it here. */
}
break;
}
}
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"a" message:#"b" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"ca",nil];
[a show];
[a release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1)
{
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
purchasedone.tag = 55;
[purchasedone show];
[purchasedone release];
}
if([alertView tag] == 55 && buttonIndex == 0)
{
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"New Alert" message:#"New Alert Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[purchasedone show];
[purchasedone release];
}
}
write UIAlertViewDelegate in .h file.
put the following line on top of your .m file
`#define PREVIOUS_ALERT 101`
`#define NEW_ALERT 102`
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//you need to make a condition here for checking the alert else it will go into infinite loom of showing alert after alert
//you can do it by setting tag to alert something like this
if(alertView.tag == PREVIOUS_ALERT) //sot the previous alert tag to this where you created it
{
if(buttonIndex == 1)
{
inappPurchaseViewController = [[InAppPurchaseViewController alloc] init];
[inappPurchaseViewController Upgrade:nil];
[inappPurchaseViewController release];
UIAlertView *purchasedone = [[UIAlertView alloc] initWithTitle:#"Enable Ads" message:#"You can Enable Ads from Settings Option" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
purchasedone.tag = NEW_ALERT;
[purchasedone show];
[purchasedone release];
}
}
else
{
//code for your second alert
}
}

How should I properly format this code?

I've a small issue here. I am using an if statement with UIAlertView and I have two situations, both result in UIAlertViews. However, in one situation, I want to dismiss just the UIAlertView, the other, I want the UIAlertView to be dismissed and view to return to root view.
This code describes is:
if([serverOutput isEqualToString:#"login.true"]){
[Alert dismissWithClickedButtonIndex:0 animated:YES];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
UIAlertView *success = [[UIAlertView alloc] initWithTitle:#"Success" message:#"The transaction was a success!"
delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[success show];
[success release];
} else {
UIAlertView *failure = [[UIAlertView alloc] initWithTitle:#"Failure" message:#"The transaction failed. Contact sales operator!"
delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[failure show];
[failure release];
}
}
-(void)alertView: (UIAlertView *)success clickedButtonAtIndex: (NSInteger)buttonIndex{
switch(buttonIndex) {
case 0: {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
}
So, in both cases, they follow the above action, but obviously, that's not what I want. Any ideas on what I do here?
You will have to differentiate between the 2 uialertview in your clickedButtonAtIndex: method.
Use the tag property to differentiate.
When you create the alerview assign a tag id to them:
UIAlertView *success = [[UIAlertView alloc] initWithTitle:#"Success" message:#"The transaction was a success!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
success.tag = 1;
[success show];
Similarly,
failure.tag = 2;
Then you switch on the tag ids
switch(alertView.tag){
case 1: //dismiss alertview
case 2: //dismiss alertview and return to root view
}
You could paste your code in Eclipse, and press ctrl+i.

UIAlertView Question

I am trying to act upon whichever button is pressed on an alert. I have the following code and the first alert pop's up but it never gets to the second one.
I have set it up so that the UIAlertViewProtocol is defined in the header too.
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex != [actionSheet cancelButtonIndex])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Something actually happened" message:#"Something was done" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"test",nil];
[alert show];
}
}
-(void)alert:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex ==0)
{
NSLog(#"tetetete");
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[a show];
[a release];
[alert release];
}
}
The simplest explanation is that the delegate is not set properly. Set the debugger to
if(buttonIndex ==0) to make sure the delegate method is being called. Alternatively, the button index might not be zero so the second alert is never created. The debugger can check that as well.
You should move the line...
[alert release];
... to the first method.
I've never tried to daisy chain alerts like this. It's theoretically possible that since alerts are modal and attached to the window and not the top view, that you can't add a second alert until the first one has been completely removed from the window. If the window merely releases the alert it might persist in a property of the window if the originating object has not yet released it. Having the view retained until after the second view is shown might cause a collision of some sort in the window object.
I have modified your code check it
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex != [actionSheet cancelButtonIndex])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Something actually happened" message:#"Something was done" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"test",nil];
[alert show];
[alert release];
}
}
-(void)alert:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex ==0)
{
NSLog(#"tetetete");
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"test" message:#"test" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[a show];
[a release];
[a release];
}
}