-[UIThreePartButton文本]:无法识别的选择器 [英] -[UIThreePartButton text]: unrecognized selector

查看:53
本文介绍了-[UIThreePartButton文本]:无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了一段时间了,无法弄清.

I've been racking myself on this one for a while, and can't figure it out.

这是一个为页面添加书签的操作表(适用于类似Safari的应用),当我点击确定"时,-[UIThreePartButton文本]:实例XXXX上无法识别的选择器

It is an action sheet that bookmarks a page (for a safari like app) When I hit Okay, i get the -[UIThreePartButton text]: unrecognized selector at instance XXXX

我似乎无法逐步解决我的痛苦所在.

I can't seem to step through where my pain is.

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) 
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Bookmarks" message:@"Please enter the site name: \n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        alert.center=CGPointMake(160, 50);
        alert.tag=1;

        UITextField *siteName=[[UITextField alloc]initWithFrame:CGRectMake(20, 70, 245, 30)];
        siteName.backgroundColor=[UIColor clearColor];
        siteName.borderStyle=UITextBorderStyleRoundedRect;
        siteName.autocorrectionType=UITextAutocorrectionTypeNo;
        siteName.delegate=self;
        siteName.clearButtonMode=UITextFieldViewModeWhileEditing;
        siteName.text=[[mainDelegate.sitesArray objectAtIndex:tag] objectForKey:@"webSite"];
        siteName.returnKeyType=UIReturnKeyDone;
        siteName.tag=2;
        [alert addSubview:siteName];

        [alert show];
    } 
}

我意识到它死在它下面的警报视图方法中:

I realized that it dies on the alert view method below it:

if(alertView.tag==1)
    if (buttonIndex==1)
    {
        if(((UITextField*)[alertView.subviews objectAtIndex:4]).text==@""||((UITextField*)[alertView.subviews objectAtIndex:4]).text==nil)
        {

通常在 if((((UITextField *)[alertView.subviews objectAtIndex:4])

该操作表上我有4个按钮/操作,其余的操作都很好...

I have 4 buttons/action on that action sheet, and the rest are good to go...

推荐答案

请勿尝试手动遍历Apple否则未记录的UI元素的视图层次结构.进行 [alertView.subviews objectAtIndex:4] 之类的操作很危险,因为您最终不得不对UIAlertView的内部结构进行盲目假设.

Do not try to manually traverse the view hierarchy of a UI element that Apple does not otherwise document. Doing something like [alertView.subviews objectAtIndex:4] is dangerous, because you end up having to make a blind assumption of the internal structure of a UIAlertView.

在这种情况下,您遇到的是错误的元素(UIThreePartButton),并且使您的应用程序崩溃并发生异常.即使您确实知道当前文本字段的正确位置索引,也无法保证Apple不会更改视图编号或在将来的OS版本中排序,从而突然导致应用程序的所有安装崩溃.

In this case, you're hitting the wrong element (a UIThreePartButton) and crashing your application with an exception. Even if you do get the index correct on where your text field is right now, there's no guarantee that Apple won't change the view number or ordering in a future OS release, suddenly causing all installations of your application to crash.

为防止这种情况,请保留对UITextField的引用,这样就无需在UIAlertView上探查视图层次结构.

To prevent this, hold on to a reference to your UITextField so you don't need to probe the view hierarchy on a UIAlertView.

更好的是,不要将警报视图用于文本输入,而应使用自定义模式视图.警报确实应该保留给偶发的错误显示或其他重要更新.从

Even better, don't use an alert view for text input, but a custom modal view. Alerts really should be reserved for infrequent error displays or other critical updates. From the iOS Human Interface Guidelines:

警报的频率不高出现可以帮助用户接受严重地.确保最小化您的应用显示的警报数量以及确保每个人都提供关键信息和有用的选择.

The infrequency with which alerts appear helps users take them seriously. Be sure to minimize the number of alerts your app displays and ensure that each one offers critical information and useful choices.

仅供参考,您还会在上述代码中泄漏您的 siteName UITextField,并可能泄漏您的UIAlertView.

As an FYI, you're also leaking your siteName UITextField in the above code, and potentially your UIAlertView.

这篇关于-[UIThreePartButton文本]:无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆