viewWillAppear UIAlertView not showing - ios

I have two different view controllers, one for a dashboard and one for registration. I do not want the user to be able to interact with anything on the dashboard until the user logs in through an alertview. So every time the user navigates back to the dashboard or presses cancel and they are not logged in, I want the login alert to popup.
This works perfectly in all cases, including when the user hits the back button on the navigation bar in the registration view, but does not work when the user clicks OK on the alert in the registration page.
the dashboard view contains this code:
#property(strong) UIAlertView * alert;
//...
-(void)viewWillAppear:(BOOL)animated
{
user_email = [[NSUserDefaults standardUserDefaults] stringForKey:#"email"];
if ( user_email==nil ){
[self auto_login];
} else //...
}
-(void)auto_login
{
alert = [[UIAlertView alloc] initWithTitle:#"Login" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Login",#"Forgot Password",#"Register",nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
{
self.debug.text = #"Cancel";
[self auto_login];
break;
}
//...
default:
{
self.debug.text = #"Register";
[self nav_register];
break;
}
}
}
-(void)nav_register
{
RegisterProfileController *rvc = [[RegisterProfileController alloc] init];
[self.navigationController pushViewController:rvc animated:YES];
}
The registration view controller contains this code:
-(void)catch_registration
{
NSString *response = [[NSString alloc] initWithData:self.httpdata encoding:NSASCIIStringEncoding];
if( [response isEqualToString:#"OK"] ){
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:#"Success" message:#"..." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
successAlert.alertViewStyle = UIAlertViewStyleDefault;
[successAlert show];
}
else //...
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([alertView.title isEqualToString:#"Success"])
[self.navigationController popViewControllerAnimated:TRUE];
}
After debugging, I know that after clickedButtonAtIndex runs in the registration view controller, [alert show] runs in the dashboard view controller, and clickedButtonAtIndex does NOT run in the dashboard view controller, but no alert shows up.
Why isn't the alert showing or how can I debug this further?

If clickedButtonAtIndex "does NOT run" as you said, then the delegate might not be set correctly. In addition, you should likely move the code from viewWillAppear: to viewDidAppear: because the view is not in the view hierarchy at that point. Your solution might be a combination of these two issues.

Related

Dismiss alert view when performing delegate method

hi there when i run my delegate method which is parsing json data the alert view appears to freeze whilst it is performing the method is there anyway to hide the alert view whilst the app is running the code I've tried
- (IBAction)btnAdd:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Add Source" message:#"Enter the web address of the json data" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert setTag:0];
[alert dismissWithClickedButtonIndex:-1 animated:YES];
[alert show];
}
this doesn't actually do anything.
any advice?
*UPDATE
in the delegate method i get the same result
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 0) {
if (buttonIndex == 1) {
NSString *textEnteredraw = [[alertView textFieldAtIndex:0] text];
[alertView dismissWithClickedButtonIndex:-1 animated:YES];
From method
- (IBAction)btnAdd:(id)sender
Remove
[alert dismissWithClickedButtonIndex:-1 animated:YES];
Because as #Dima said, you are dismissing the alertView before you are even showing it.
You call the code to hide the alert before you even show it. This method is meant to be called after the alert is shown.

UIAlertView crash issue on another view controller

I have 2 view controllers, ViewController 1(VC1) and 2(VC2). In VC2 I have a back and done button. On clicking back button it goes directly to VC1 and on done it makes an api call and when it gets a response it shows an alert view and clicking ok goes back to VC1. Now when I make a api call a loading bar shows up and disappears when I get response and shows the AlertView. But if during that fraction of second when the loading disappears and AlertView is going to be popped up if I click on back and the view changes to VC1, the alert appears on VC1 and results in a crash.
This is a rare case as no user will purposely try for it but I was wondering if that crash can be managed without disabling the back button. I think there can be other instance such cases like if we are making an asynchronous calls and if the user is allowed to use UI while waiting for response and if any error alert that was suppose to show on one ViewController shows up in another may result in crash since the delegate that alert is referring to is that of the previous view controller. So is there any way to handle this kind of crash efficiently?
//Alert View sample
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] ;
[alert setTag:701];
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([alertView tag] == 701)
if (buttonIndex == 0)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
The proper way to fix this problem is to use an instance variable to keep a reference to the alert view.
This instance variable should be set to nil in the alertView:didDismissWithButtonIndex: delegate method.
In the view controller's dealloc method, you call dismissWithClickedButtonIndex:animated: if the instance variable is still set.
Assume _alertView is the instance variable.
Create the alert:
_alertView = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] ;
[_alertView setTag:701];
[_alertView show];
Update your existing alertView:clickedButtonAtIndex: method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 701) {
_alertView.delegate = nil;
_alertView = nil;
if (buttonIndex == 0) {
[self.navigationController popViewControllerAnimated:YES];
}
}
}
Add:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
_alertView = nil;
}
Add:
- (void)dealloc {
if (_alertView) {
_alertView.delegate = nil;
[_alertView dismissWithClickedButtonIndex:_alertView.cancelButtonIndex animated:NO];
_alertView = nil;
}
}

activate segue after email sent

Once the user of my app has sent an email and has seen a UIAlert informing them their email has been sent successfully, id like them to then be taken back to a home screen via a segue. at the moment the user has to push a back button to achieve this.
I get the feeling it should be implemented in the mailComposeController method, but I've never programatically activated a segue.
my code is given below:
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
case MFMailComposeResultCancelled:
{
[self dismissViewControllerAnimated:YES completion:nil];
}
break;
case MFMailComposeResultSent:
{
[self dismissViewControllerAnimated:YES completion:nil];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Disclaimer", "")
message:NSLocalizedString(#"Thank you for Dobbing in a Hoon. You will shortly receive an email from Council. Please be aware that the Police are responsible for actioning your requests.", "")
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
break;
}
case MFMailComposeResultFailed:
{
[self dismissViewControllerAnimated:YES completion:nil];
UIAlertView *alert_failed = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Email Failure", "")
message:NSLocalizedString(#"Your Email Failed to send - Please try Again", "")
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert_failed show];
break;
}
default:
break;
}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex;
{
if (buttonIndex != alert.cancelButtonIndex) {
[self performSegueWithIdentifier:#"hoonToHome" sender:self];
}
}
If you need only to pop to your home screen and if the home screeen is nav controllers root view controller try
[self.navigationContoller popToRootViewControllerAnimated:YES];
Or if you have a sugue for that use
[self performSegueWithIdentifier:#"yourSegueID" sender:nil];
UPDATE
If you want the transition after alertview's ok click the set alert delegate to self and implemet delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
in this Method add the above code.
I you want immediate transition then place it before or after [alert show];
First you need to set the delegate of the UIAlertView to self and implement the UIAlertView delegate methods.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
Then to call your segue.
[self performSegueWithIdentifier:#"someIdentifierDefinedInStoryboard" sender:self];
Or if you're referring to the standard back action of a navigation controller
[self.navigationController popToRootViewControllerAnimated:YES];

EXC_BAD_ACCESS(code=1 address=0x2f9cd928) when running project on device

on the simulator everything worked fine but on my ipod touch 6.1.5 i am getting error when clicking on any button in the alert view.
-(void)optionsButtonPressed {
UIAlertView *optionsAlertView = [[UIAlertView alloc] initWithTitle:#"Options" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add Note",#"Recover Last Note",#"Recover All", nil];
[optionsAlertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Add Note"]) {
AddNoteViewController *addNoteViewController = [[AddNoteViewController alloc] initWithNibName:nil bundle:NULL];
// ...
}
// ...
}
i wont post the whole method because its long.... the problem is that when the alertview appears, and as soon as i press on any button it is crashing which was not happening in the simulator.
i am using xcode 4.6.3
here is the error: Thread 1: EXC_BAD_ACCESS(code=1 address=0x2f9cd928)
NOTE: in the first viewController(first to load), i have an alert view that is working just fine, and has a method to handle which button is pressed.

Trying use an AlertView to push to another view controller

I'm having trouble programming an AlertView to push to a new ViewController. I've followed several tutorials and still have not been able to get it function properly. Any help would be appreciated.
- (IBAction)web:(id)sender {
UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"You are now entering a website outside of the Company App: Any links or references to other Internet sites (hyperlinks) are provided by Company merely as a convenience to users of this App." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Proceed", nil];
[testAlert show];
[testAlert release];
}
-(void)web:(UIAlertView *)web clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
}
else if (buttonIndex == 1) {
JBWebViewController *jbweb = [[JBWebViewController alloc] initWithNibName:#"JBWebViewController" bundle:nil];
[self.navigationController pushViewController:jbweb animated:YES];
}
}
You should use UIAlertView delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
not
-(void)web:(UIAlertView *)web clickedButtonAtIndex:(NSInteger)buttonIndex
and check self.navigationController is not nil.

Resources