Number keypad jumping after UIAlertview - iphone

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...

Related

objective-c UIAlert button not working

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

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

Detecting which UIAlertView was clicked

i need to pop up alert when my application loaded... I called it didfinished launching..
after clicking ok button need to show another alert message i use clickedButtonAtIndex...
Now when I clicked the ok button its calling again and again.. the alertview..
I need to call only once... what to do?
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
viewControllersList = [[NSMutableArray alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Alow this app to use your GPS location"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
NSLog(#"NO");
}
else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
}
#thanks in advance.
set delegate:nil in second alertView
I mean
if (buttonIndex==0) { NSLog(#"NO"); } else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:nil cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
Define each UIAlertView and in the delegate look for which Alert to respond to:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alert1) {
if (buttonIndex==0) {
NSLog(#"NO");
} else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages." delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
} else {
/* the second alertview using the same buttonIndex */
}
}
You can also add a tag to your AlertView and te check the tag later on the clickedButtonAtIndex method
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:#"" message:#"All local datawill be erased. Erase local data?" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Erase", #"Cancel",nil];
alert.tag =123; // added the tag so we can prevent other message boxes ok button to mix up
[alert show];
then
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex ==0 && alertView.tag==123){
// Do what ever you wish
}
}
If what you want is to receive the location, just request it and the system will automatically show the message for you. Follow this example:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:tabBarController.view];
// Create a location manager instance to determine if location services are enabled. This manager instance will be
// immediately released afterwards.
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:#"Location Services Disabled" message:#"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
[servicesDisabledAlert release];
}
[manager release];
}
http://developer.apple.com/library/ios/#samplecode/LocateMe/Listings/Classes_AppDelegate_m.html%23//apple_ref/doc/uid/DTS40007801-Classes_AppDelegate_m-DontLinkElementID_4
Same for push notifications.
though u got many implementation solutions already ...but i think better practice would be to assign a tag to each of your AlertView and before detecting the button tapped check the tag of invoking AlertView.Hope this helps.
#Gerard:Though both location manager and push notification service raise system generated messages ,these can be checked off by users not to be shown again.So better HIG complaint method is application generating message whenever push or location manager are required.

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.