UIActionSheet在第二次点击按钮时解散

时间:2011-12-30 10:16:06

标签: ios cocoa-touch uiactionsheet

我有一个带有EDIT和DELETE按钮的actionSheet,两个都是其他按钮这是我为它编写的代码

-(void)method1
{

    action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil     destructiveButtonTitle:nil otherButtonTitles:@"Edit", @"Delete", nil];
     action.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    [action showInView:self.view];
    [action release]; 

}

我使用了删除方法为方法分配操作..

-(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        // do something
    }
    if(buttonIndex == 1)
    {
        // do something
    }
}

现在的问题是只需点击一个按钮就不会忽略动作表。请帮我解决一些问题。

2 个答案:

答案 0 :(得分:1)

您使用了错误的委托方法,对于按钮交互,您应该使用:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

而不是:

-(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex

答案 1 :(得分:1)

这似乎是iOS4.0中的一个错误。我在我的模拟器中遇到了这个问题。我将版本更改为4.3和5.0,似乎没问题。

修改:

似乎我的问题更具体地说是由委托方法启动两次的动作表&#34 ;-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField"

不确定为什么在这种情况下调用两次而不是其他调用(再次假设iOS 4.0错误已在以后的版本中得到修复)。我的解决方法是跟踪它是否已被调用,而不是第二次调用它。

修改2

我建议做类似的事情:

-(void)method1
{
    if(hasLaunchedActionSheet)
    {
        return;
    }

    hasLaunchedActionSheet = YES;
    ...

-(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    hasLaunchedActionSheet = NO;
    ...

对我来说,问题不在于Xcode,而在于iOS SDK本身调用我的事件两次。我不确定你是如何调用method1所以它可能是另一个不同事件的问题。

相关问题