在OrderFormViewController
我有tableView
而tableViewCell我有一个cameraButton
,在点按时会显示提醒。
现在当OrderFormViewController
出现(未推送)时,它会加载tableView
并且它的单元格(行数是1硬编码的)。< / p>
我在IBAction
的{{1}}下面有以下代码:
cameraButton
我搜索了这个,所以这就是为什么我尝试@IBAction func cameraButtonPressed(_ sender: Any) {
//DispatchQueue.main.async {
let alert = UIAlertController(title: "Choose Image From", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
{
self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera
self.imagePicker.allowsEditing = true
UIApplication.shared.keyWindow?.rootViewController?.present(self.imagePicker, animated: true, completion: nil)
}
else
{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.imagePicker.allowsEditing = false
UIApplication.shared.keyWindow?.rootViewController?.present(self.imagePicker, animated: true, completion: nil)
}))
//alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
//}
}
并尝试从UIApplication.shared.keyWindow?.rootViewController
运行它,但有些DispatchQueue.main.async
无效。
更新如果我按下视图控制器,那么它可以工作。但我需要一个解决方案来呈现视图控制器。
答案 0 :(得分:1)
我强烈建议重构整个解决方案以使用代理人 - 委托从UITableViewCell
向OrderFormViewController
提交提醒的操作,只需使用
self.present(alert, animated: true, completion: nil)
因为使用UIApplication.shared.keyWindow?.rootViewController
非常脆弱。
所以只是为你勾勒出来,我相信最好的方法就是:
class OrderFormViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
// set the delegate
cell.delegate = self
return cell
}
}
extension OrderFormViewController: CustomCellDelegate {
func customCellDelegateButtonPressed(_ customCell: CustomCell) {
let alert = UIAlertController(title: "Choose Image From", message: nil, preferredStyle: .alert)
// configure the alert
// now the presentation of the alert is delegated here to OrderFormViewController, so it can simply use self to present
self.present(alert, animated: true, completion: nil)
}
}
protocol CustomCellDelegate: class {
func customCellDelegateButtonPressed(_ customCell: CustomCell /*, potentially more parameters if needed */)
}
class CustomCell: UITableViewCell {
// rest omitted for brevity
weak var delegate: CustomCellDelegate?
@IBAction func cameraButtonPressed(_ sender: Any) {
delegate?.customCellDelegateButtonPressed(self)
}
}