2010-12-10 86 views
0

我一直在对这一个进行一段时间的自我评估,并且无法弄清楚。- [UIThreePartButton text]:无法识别的选择器

它是某个网页加入书签(对于类似的应用程序的Safari)当我打好吧,我得到的 动作片 - [UIThreePartButton文]:无法识别选择在实例XXXX

我似乎

不能走过我的痛苦之地。

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

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

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

Spefically上if(((UITextField*)[alertView.subviews objectAtIndex:4])

我有4个钮在动作片/动作,其余的是很好去...

+0

当您在异常上放置断点并打开断点时,应用程序停止在哪一行上? – 2010-12-10 15:29:45

+0

F(alertView.tag == 1) \t \t如果(buttonIndex == 1) \t \t { \t \t \t如果(((的UITextField *)[alertView.subviews objectAtIndex:4]。)文本== @” “||((UITextField *)[alertView.subviews objectAtIndex:4])。文字==无) \t \t \t { – Jason 2010-12-10 15:43:59

+0

让我更新? – Jason 2010-12-10 15:44:19

回答

0

如果你有4个按钮每个动作你的objectAtIndex不是3而不是4?看起来你正在遇到越界异常。

具体来说objectAtIndex:4如果自上一个有效索引为3以来,数组中有4个项目,将会生成一个异常。

+0

是的,我一直在想...但错误似乎在: – Jason 2010-12-10 15:54:57

+0

alertView.subviews objectAtIndex:4 所以,我认为4不是问关于butten索引,而是查看索引。我可能会不知何故 – Jason 2010-12-10 15:55:44

+0

错误的索引在acutal视图上,而不是按钮。 – Jason 2010-12-10 16:03:10

2

不要尝试手动遍历Apple不另外记录的UI元素的视图层次结构。做类似[alertView.subviews objectAtIndex:4]的事情是很危险的,因为你最终不得不对UIAlertView的内部结构进行盲目的假设。

在这种情况下,您遇到了错误的元素(一个UIThreePartButton)并且崩溃了您的应用程序并出现异常。即使您确实在文本字段的正确位置获得了索引,也不能保证Apple在未来的操作系统版本中不会更改视图编号或排序,这会突然导致应用程序的所有安装崩溃。

为了防止出现这种情况,请保留对UITextField的引用,以便不需要在UIAlertView上探测视图层次结构。

更好的是,不要使用警报视图来输入文字,而是使用自定义模态视图。警报确实应该保留用于不频繁的错误显示或其他关键更新。从iOS Human Interface Guidelines

很少发生与哪些警报 出现可以帮助用户把他们 严重。请务必尽量减少您的应用显示的 警报数量和 确保每个警报提供关键的 信息和有用的选择。

作为一个供参考,你也泄漏你的siteName UITextField在上面的代码,并可能你的UIAlertView。