Swift: 防止 ABPeoplePickerNavigationController 关闭

Swift: Prevent ABPeoplePickerNavigationController from Closing

我想找出一种方法,如果用户在 ABPeoplePickerNavigationController 中按下 "Cancel" 按钮(我认为无法删除),视图控制器要么不关闭,要么会自动重新打开。

例如,给定以下内容:

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 语句不会 "wait"。

我不确定是否有办法删除取消按钮或阻止它工作,但您可以响应 func peoplePickerNavigationControllerDidCancel(_ peoplePicker: ABPeoplePickerNavigationController!) 委托来处理按下取消按钮的情况。

我建议不要立即重新打开选择器,而是打开一个警告,告诉用户他们需要选择某人,然后从那里重新打开它。如果他们取消它可能会感觉坏了,它会立即重新打开。

Reference

编辑:
呈现警报或选择器可能需要延迟足够长的时间才能关闭前一个选择器。 dispatch_after