【问题标题】:Undeclared identifier that is declared?声明的未声明标识符?
【发布时间】:2012-06-20 01:29:29
【问题描述】:

我正在制作一个应用程序,如果网页没有加载,它会给出错误并返回到上一个屏幕。但是,在这样做之后,在所有代码之后,得到一个未声明的标识符

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

正如您在这张图片中看到的那样。如果网页没有加载,它会激活 doPop,它会返回视图并显示一条消息。但是,这会引发 EXC_BAD_ACCESS,因为如您所见,在 viewDidLoad 方法下,还有另一条消息在播放。该应用程序变得混乱和崩溃。我试图通过消除 doPop 方法中的警报来解决这个问题,但是奇怪的是它给了我这个错误。我可能会误解,但不是在它说“UIAlertView *cats”的地方定义了警报视图吗?为什么它说它没有在doPop方法中定义?请帮忙!

【问题讨论】:

  • 在堆栈溢出中使用此处的代码格式复制粘贴您的代码,否则我们检查起来太麻烦了。其实没关系,在这种情况下,您的图像质量足够高,可以阅读。
  • 好吧:不知道 * 的格式,但这里是 paste.org 链接:pastie.org/4117873
  • 尝试将错误消息的代理设置为 nil。
  • 没有修复错误。你的意思是改变 UIAlertView *cats........delegate:self 到 UIAlertView *cats........ ...代表:无,对吧?
  • 这里是 .h 以防万一:pastie.org/4117942

标签: iphone ios xcode exc-bad-access undeclared-identifier


【解决方案1】:

对象cats 在本地定义为viewDidLoad。变量的范围没有超出方法,因此doPop 不知道猫是什么。

移动 cats 以在 .h 文件中定义为成员/类变量。

这意味着您需要从 viewDidLoad 中删除 UIAlertView * 并仅引用猫(因为您正在定义另一个范围为 viewDidLoad 的变量)。

doPop中调用的方法有错别字,方法是dismissWithClickedButtonIndex:1 animated:YES];你有dissmissWithClickedButtonIndex:1 animated:YES];

另外,您只需要使用@property 定义的IBOutlet

【讨论】:

  • 这就是我真正开始的想法。谢谢你的澄清!我确实尝试在全局范围内定义它,但它给了我一个不同的错误。让我把它弄起来。
  • 我将下面 cmets 中的粘贴链接更新为我的新代码。我现在收到此错误:“UIAlertView”没有可见的@interface 声明扇区“dismissWithClickedButtonIndex:animated
  • 糟糕!让我看看你的更新,忽略以前的 cmets。
  • 这与实际问题无关,这是一种常见的做法,即在本地声明一个 uialertview 并仅显示它,因为只要它在屏幕上可见,它就会工作并使用它的委托。跨度>
  • 我的编辑包含问题,这是方法名称中的拼写错误,dismiss 不是拼写 dissmiss
【解决方案2】:

好的,如果我是你,我会简单地在加载失败的视图上显示错误消息,然后使用默认的取消操作来弹出这个视图(这是最常见的做法)

或者,如果您真的想在另一个视图中显示消息,则必须使该视图显示错误消息。有几种方法可以做到这一点,但 id 仍然使用第一个选项。 (如果你真的想采用这种方法,我可以告诉你怎么做)

编辑:如果您只是希望该错误消失,请在 .m 文件中的“实施”之前添加

@interface CatForm ()
{
    UIAlertView *cats;
}

@end

改变

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

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

在视图中加载

【讨论】:

  • 你没有抓住重点。他希望手动关闭 UIAlertView
  • 这是用户手中的应用程序的工作方式。如果用户没有互联网连接,应用程序将发送一条错误消息(无论哪个屏幕),但将用户引导到上一个屏幕(采用表单选择屏幕)
  • @obuseme 出于这个原因,您只是不反对答案,sam 的最终目标很明确,我在这里发布的解决方案是解决 exec 访问权限错误的第一个问题,他试图解决这个问题他的方式,但没有必要。
  • 我投了反对票,因为您的回答离题且与手头的实际问题无关。