在UIActionSheet或UIAlertView(iPhone / iPad)之后锁定视图

时间:2012-01-13 21:42:15

标签: iphone uiview focus uialertview uiactionsheet

我遇到了一些问题:UIAlertViewUIActionSheet窃取焦点而不放手。

详细说明,我有一个用户按下按钮的设置。然后,它会显示带有UITextField的UIAlertView。当用户按下“确定”按钮时,显示UIActionSheet(从UIAlertView委托方法调用),确认用户的某些内容。然而,当他们按下其中一个按钮时UIActionSheet消失,但焦点不会返回到主视图。

我一直在玩,无论我做什么,我总是以我的主UIView被这样的层覆盖(显然你可以通过图层看到我的视图)。我已经尝试删除视图的所有子视图,但它没有任何成功。

enter image description here

有人可以帮帮我吗?

以下是源代码:

// This method displays the UIAlertView which contains a UITextView linked up to a
// UIPickerView.
- (IBAction)buttonPressed:(id)sender
{
    UIAlertView *deleteAlert = [[UIAlertView alloc] initWithTitle:@"Delete Something" 
                                                          message:@"Select something to delete:" 
                                                         delegate:self 
                                                cancelButtonTitle:@"Cancel" 
                                                otherButtonTitles:@"Ok", nil];
    deleteAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
    deleteAlert.tag = kDeleteAlert;
    deleteTextField = [deleteAlert textFieldAtIndex:0];
    [pickerView reloadAllComponents];
    deleteTextField.inputView = pickerView;
    [deleteAlert show];
}


// The UIAlertView delegate method is then used to display a UIActionSheet to confirm
// whether the user wants to proceed.
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIActionSheet *confirmDelete = [[UIActionSheet alloc] initWithTitle:@"Delete blah?" 
                                                               delegate:self 
                                                      cancelButtonTitle:@"No" 
                                                 destructiveButtonTitle:@"Yes" 
                                                      otherButtonTitles:nil];
    confirmDelete.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [confirmDelete showInView:self.parentViewController.tabBarController.view];
}

// Then the focus should be returned to the original view (i.e. where the first button
// was pressed to launch the UIAlertView. However the focus is still locked and the
// view appears slightly dimmed around the edges.
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if (buttonIndex == actionSheet.destructiveButtonIndex) 
    {
        // Do some stuff ...
    }
}

2 个答案:

答案 0 :(得分:2)

我认为您的问题是调用actionSheet时deleteAlert警报视图未被解除的结果。

我听起来像警报视图仍然有焦点但处于未知状态,因为它没有被解雇,但是你已经用按钮按下了它。

据推测,您希望在显示操作表时关闭警报视图?然后当行动表被解雇时,您会回到主视图?所以,你想要的顺序:

1)显示提醒视图

2)如果按下警报视图上的按钮,它会使用- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

通知其代理人

3)关闭alertView - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated并显示操作表。

3)当使用操作表时,它会调用它的委托方法- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex并在该方法中,忽略它,或者你需要做的任何其他事情。

您可以将所有这些委托方法放在原始的调用viewcontroller中,使其符合<UIAlertViewDelegate, UIActionSheetDelegate>

您需要在创建时将原始viewcontroller设置为警报视图和操作表的委托。我希望这是有道理的。

更多信息http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html

答案 1 :(得分:0)

我想说这可能是b / c你没有在UITextField内的UIAlertView上辞职第一响应者。在显示UIActionSheet之前确保

[myTextField resignFirstResponder];