Pregunta

I am making an app where if the webpage doesnt load, it gives an error and returns to the previous screen. However, in doing this, after all the code, get an undeclared identifier

#pragma mark - View lifecycle


- (void)viewDidLoad
{
    UIAlertView *cats = [[UIAlertView alloc] initWithTitle:@"**Read this first!**" 
                                                   message:@"Thank you for ..." 
                                                  delegate:nil 
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
    [cats show];
    [catscroll setScrollEnabled:YES];
    [catscroll setContentSize:CGSizeMake(320,4800)];
    [catscroll setPagingEnabled:NO];
    [catform loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.petfinder.com/s/showPage.do?siteId=76333&pageId=7133416&shelterId=MA84&navigateToPage=Adopt%20Pets"]]];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)doPop
{
    [cats dissmissWithClickedButtonIndex:-1 animated:YES];
    [self.navigationController popViewControllerAnimated:YES];
    UIAlertView *noconnectcatform = [[UIAlertView alloc] initWithTitle:@"Check your connection!" 
                                                               message:@"Cannot connect to FPP Servers.\nPlease check your Internet Connection\nYou may not proceed until you are connected via a cellular network." 
                                                              delegate:nil cancelButtonTitle:@"OK" 
                                                     otherButtonTitles:nil];
    [noconnectcatform show];
}

as you can see in this image. If the webpage doesnt load it activates doPop, which returns the view and displays a message. However, this is throwing an EXC_BAD_ACCESS because, as you can see, under the viewDidLoad method, there is another message that plays. The app is getting confused and crashing. I tried to fix this by dismissing the alert in the doPop method, however it is strangely giving me this error. I may be misunderstanding, but isn't the alertview defined where it says "UIAlertView *cats"? why does it say that it is not defined in the doPop method? Please help!

¿Fue útil?

Solución

The object cats is defined locally to viewDidLoad. The scope of the variable does not extend beyond the method, thus doPop has no idea what cats is.

Move cats to be defined in the .h file as a member/class variable.

This means you'll need to remove the UIAlertView * from within viewDidLoad and just reference cats (as is, you are defining another variable scoped to viewDidLoad).

You have a typo in the method called in doPop The method is dismissWithClickedButtonIndex:1 animated:YES]; You have dissmissWithClickedButtonIndex:1 animated:YES];

Also, you only need IBOutlet defined with the @property

Otros consejos

Okay, so if i were you i would simply show the error message on the view that failed to load, then use the default cancel action to pop this view (This is the most common practice)

Alternatively, if you really wanna show the message in the other view you have to make that view display the error message. there are several ways to do this but id still go with the first option. (I could tell you how if you really wanna take this approach)

Edit: If you just want that error to go away, add before "Implementation" in your .m file

@interface CatForm ()
{
    UIAlertView *cats;
}

@end

and change

UIAlertView *cats = [[UIAlertView alloc] initWithTitle:@"**Read this first!**" 
                                                                  message:@"Thank you for considering to adopt a cat from our shelter. PLEASE READ THIS FORM CAREFULLY.\n\nThis on-line form is intended to assist you in selecting a cat that is suitable for you, your family, and your lifestyle.\n\nThe Friends of the Plymouth Pound have established guidelines and policies that must be met in order for your pre-screening application to be approved.\n\nThe information you provide is essential to facilitate the application review process.\n\nIn order to be considered for adoption by the Friends of the Plymouth Pound, you must:\n\n\n*Be at least 18 years old.\n\n*Provide all the applicable information requested below.\n\n**Note, any data field marked with an asterisk character (*) must be filled in. Incomplete application forms will not be considered for review and will be denied automatically.**\n\n*Understand that this is a pre-screening form.\n\n\nThe Friends of the Plymouth Pound will contact you and approve you if, and only if, all requirements have been met.\n\nUpon review of your pre-screening application, you will be contacted by the Friends of the Plymouth Pound to notify you if your application has been accepted and if further information is needed. \n\n\n**Please note that we do contact all personal references and veterinarians, and we must speak with them personally. In addition, in some cases, a pre-adoption home visit may also be required.**\n\nIn the event that your pre-screening application has been not been approved, we will notify you of same.\n\n\nTHE BOARD OF DIRECTORS OF THE FRIENDS OF THE PLYMOUTH POUND RESERVES THE RIGHT TO DENY ANY PRE-APPLICATION BASED ON THE ORGANIZATION'S ESTABLISHED STANDARDS AND POLICIES, INCLUDING, BUT NOT LIMITED TO, INCOMPLETE INFORMATION, NON-DISCLOSURE OR OMISSION OF PERTINENT FACTS, AND NON-COMPLIANCE WITH ACCEPTED STANDARDS.\n\n\nUpon approval of your application and transfer of the animal, you will be charged a $150.00 adoption fee per cat.\n\n\nWe are an all-volunteer organization. Due to the high volume of applications, we ask that you be patient in waiting for a reply. Please do not submit duplicate applications or separate requests for response. Doing this will not accelerate the review process in any way." 
                                                                 delegate:self cancelButtonTitle:@"OK"
                                                        otherButtonTitles:nil];

for

cats = [[UIAlertView alloc] initWithTitle:@"**Read this first!**" 
                                                                  message:@"Thank you for considering to adopt a cat from our shelter. PLEASE READ THIS FORM CAREFULLY.\n\nThis on-line form is intended to assist you in selecting a cat that is suitable for you, your family, and your lifestyle.\n\nThe Friends of the Plymouth Pound have established guidelines and policies that must be met in order for your pre-screening application to be approved.\n\nThe information you provide is essential to facilitate the application review process.\n\nIn order to be considered for adoption by the Friends of the Plymouth Pound, you must:\n\n\n*Be at least 18 years old.\n\n*Provide all the applicable information requested below.\n\n**Note, any data field marked with an asterisk character (*) must be filled in. Incomplete application forms will not be considered for review and will be denied automatically.**\n\n*Understand that this is a pre-screening form.\n\n\nThe Friends of the Plymouth Pound will contact you and approve you if, and only if, all requirements have been met.\n\nUpon review of your pre-screening application, you will be contacted by the Friends of the Plymouth Pound to notify you if your application has been accepted and if further information is needed. \n\n\n**Please note that we do contact all personal references and veterinarians, and we must speak with them personally. In addition, in some cases, a pre-adoption home visit may also be required.**\n\nIn the event that your pre-screening application has been not been approved, we will notify you of same.\n\n\nTHE BOARD OF DIRECTORS OF THE FRIENDS OF THE PLYMOUTH POUND RESERVES THE RIGHT TO DENY ANY PRE-APPLICATION BASED ON THE ORGANIZATION'S ESTABLISHED STANDARDS AND POLICIES, INCLUDING, BUT NOT LIMITED TO, INCOMPLETE INFORMATION, NON-DISCLOSURE OR OMISSION OF PERTINENT FACTS, AND NON-COMPLIANCE WITH ACCEPTED STANDARDS.\n\n\nUpon approval of your application and transfer of the animal, you will be charged a $150.00 adoption fee per cat.\n\n\nWe are an all-volunteer organization. Due to the high volume of applications, we ask that you be patient in waiting for a reply. Please do not submit duplicate applications or separate requests for response. Doing this will not accelerate the review process in any way." 
                                                                 delegate:self cancelButtonTitle:@"OK"
                                                        otherButtonTitles:nil];

In the View Did Load

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top