Swift:防止ABPeoplePickerNavigationController关闭

7
我希望找到一种方法,如果用户在ABPeoplePickerNavigationController中按下“取消”按钮(我认为无法删除),则视图控制器要么不关闭,要么将自动重新打开。例如,给定以下内容:
var picker = ABPeoplePickerNavigationController()
picker.peoplePickerDelegate = self
self.presentViewController(picker, animated: true, completion: nil)

我希望能够像这样做某事:

if (self.presentedViewController != picker && !userContinuedPastPicker) {
//where userContinuedPastPicker is a boolean set to false 
//in a delegate method called when the user clicks on an a contact 
//(meaning the user didn't press the cancel button but instead clicked on a contact)

    //create and present a UIAlertAction informing the user they must select a contact

    //present picker again
    self.presentViewController(picker, animated: true, completion: nil) 
}

这种方法行不通,因为if语句不会等待用户按下取消按钮或联系人。

1个回答

4
我不确定是否有方法可以移除取消按钮或防止其起作用,但你可以响应func peoplePickerNavigationControllerDidCancel(_ peoplePicker: ABPeoplePickerNavigationController!)委托来处理取消按钮被按下的情况。
我建议,与其立即重新打开选择器,你应该打开一个警报告诉用户他们需要选择某人,然后再从那里重新打开。如果他们取消并立即重新打开,可能会感觉出现了问题。 参考 编辑:
显示警报或选择器可能需要延迟足够长的时间以使前一个选择器关闭。 dispatch_after

这似乎几乎就是我需要的!然而,当我尝试呈现另一个 ABPPNCUIAlertController 实例(正如您所建议的那样),我会收到一个错误,提示:警告:尝试在未在窗口层次结构中的 <App.FirstViewController: 0x7a06dd30> 上呈现 <UIAlertController: 0x7a0cfc50>! - Randoms
我的猜测是您在人员选择器正在从屏幕上动画移除时呈现警报。尝试将警报的presentViewController包装在dispatch_after中。https://dev59.com/gGAg5IYBdhLWcg3wDHQ3 - esthepiking

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接