Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 扩展UIViewController以显示UIAlertController而无需子类化_Ios_Swift_Swift5 - Fatal编程技术网

Ios 扩展UIViewController以显示UIAlertController而无需子类化

Ios 扩展UIViewController以显示UIAlertController而无需子类化,ios,swift,swift5,Ios,Swift,Swift5,我希望重用代码,以便在我在几个my view控制器中删除tableview中的某些行时显示警报: func confirmDeleteitem:字符串{ let alert = UIAlertController(title: "Delete Planet", message: "Are you sure you want to permanently delete \(item)?", preferredStyle: .actionSheet)

我希望重用代码,以便在我在几个my view控制器中删除tableview中的某些行时显示警报:

func confirmDeleteitem:字符串{

    let alert = UIAlertController(title: "Delete Planet", message: "Are you sure you want to permanently delete \(item)?", preferredStyle: .actionSheet)
     
    let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: handleDeleteItem)
     
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: cancelDeleteItem)
    
    alert.addAction(deleteAction)
    alert.addAction(cancelAction)
    
    self.present(alert, animated: true, completion: nil)
}

func handleDeleteItem(alertAction: UIAlertAction!) -> Void {

    if let indexPath = deletePlanetIndexPath {
        presenter?.removePlanet(atIndex: indexPath, completion: { (result) in
            
            switch result {
            case .success(_):
                self.tableView.deleteRows(at: [indexPath], with: .fade)
                break
            case let .failure(error):
                print(error)
                break
            }
        })
        
        deletePlanetIndexPath = nil
    }
}


func cancelDeleteItem(alertAction: UIAlertAction!) {
    deletePlanetIndexPath = nil 
}
只有一个部分每次都会不同:

presenter?.removePlanet(atIndex: indexPath, completion: { (result) in
                
                switch result {
                case .success(_):
                    self.tableView.deleteRows(at: [indexPath], with: .fade)
                    break
                case let .failure(error):
                    print(error)
                    break
                }
            })
正如您所见,我可以简单地进行子类化并声明一些闭包变量,这些变量将在每次调用deleteAction时触发


这是一种非常简单的方法,但我对子类化并不是特别感兴趣。也许有一些关于完整扩展、基于协议的东西或任何其他建议的帮助。

您可以编写一个类扩展来查看控制器:

extension UIViewController {
    func createAlert(handleDeleteItem: @escaping () -> Void, cancelDeleteItem: @escaping  () -> Void) {
        let alert = UIAlertController(title: "Delete Planet", message: "Are you sure you want to permanently delete \(item)?", preferredStyle: .actionSheet)
     
        let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: handleDeleteItem)
     
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: cancelDeleteItem)
    
        alert.addAction(deleteAction)
        alert.addAction(cancelAction)
    
        self.present(alert, animated: true, completion: nil)
    }
}

然后为每个不同的视图控制器传入适当的删除和取消函数。

您可以为视图控制器编写一个类扩展:

extension UIViewController {
    func createAlert(handleDeleteItem: @escaping () -> Void, cancelDeleteItem: @escaping  () -> Void) {
        let alert = UIAlertController(title: "Delete Planet", message: "Are you sure you want to permanently delete \(item)?", preferredStyle: .actionSheet)
     
        let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: handleDeleteItem)
     
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: cancelDeleteItem)
    
        alert.addAction(deleteAction)
        alert.addAction(cancelAction)
    
        self.present(alert, animated: true, completion: nil)
    }
}

然后为每个不同的视图控制器传入适当的删除和取消功能。

UIAlertController的扩展可以工作:

class MyVC: UIViewController {
    private lazy var alertControllerA: UIAlertController = {
         return UIAlertController.customAlertController(/* params */)
    }()
}

private extension UIAlertController {
        static func customAlertController(_ item: String, handleDeleteItem: @escaping () -> Void, cancelDeleteItem: @escaping  () -> Void) -> UIAlertController {
            let alertController = UIAlertController(title: "Delete Planet", message: "Are you sure you want to permanently delete \(item)?", preferredStyle: .actionSheet)
         
        let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: handleDeleteItem)
     
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: cancelDeleteItem)
    
        alert.addAction(deleteAction)
        alert.addAction(cancelAction)
    
        return alertController
    }
}

UIAlertController的扩展可以工作:

class MyVC: UIViewController {
    private lazy var alertControllerA: UIAlertController = {
         return UIAlertController.customAlertController(/* params */)
    }()
}

private extension UIAlertController {
        static func customAlertController(_ item: String, handleDeleteItem: @escaping () -> Void, cancelDeleteItem: @escaping  () -> Void) -> UIAlertController {
            let alertController = UIAlertController(title: "Delete Planet", message: "Are you sure you want to permanently delete \(item)?", preferredStyle: .actionSheet)
         
        let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: handleDeleteItem)
     
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: cancelDeleteItem)
    
        alert.addAction(deleteAction)
        alert.addAction(cancelAction)
    
        return alertController
    }
}

感谢已经发布答案的人,非常感谢。我还提出了另一个解决方案。因此,如果我们使用Tob示例,我们将扩展所有UIViewController的功能,如果我们使用h和.h示例,我们将装饰我们的特定视图控制器,但这会迫使我们编写额外的惰性代码,这没什么大不了的,我主要喜欢这个示例这一示例不会使任何视图控制器能够使用UIAlerController扩展,但我将遵循基于协议的扩展解决方案:

导入UIKit

protocol UIAlertControllerManageable {
    func createAlert(with text: String, handleDeleteItem: @escaping (UIAlertAction) -> Void, cancelDeleteItem: @escaping  (UIAlertAction) -> Void)
}

extension UIAlertControllerManageable where Self: UIViewController {
   
    func createAlert(with text: String, handleDeleteItem: @escaping (UIAlertAction) -> Void, cancelDeleteItem: @escaping  (UIAlertAction) -> Void) {
        
        let alert = UIAlertController(title: "Delete", message: "Are you sure you want to permanently delete \(text)?", preferredStyle: .actionSheet)
         
        let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: handleDeleteItem)
         
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: cancelDeleteItem)
        
        alert.addAction(deleteAction)
        alert.addAction(cancelAction)
        
        self.present(alert, animated: true, completion: nil)
    }
}
用法:

class HomeViewController: UIViewController, UIAlertControllerManageable {
        
        func confirmDelete(deleteText: String) {
            
            createAlert(with: deleteText) { (action) in
                
                if let indexPath = self.deleteItemIndexPath {
                    self.presenter?.removeItem(atIndex: indexPath, completion: { (result) in
                        
                        switch result {
                        case .success(_):
                            self.tableView.deleteRows(at: [indexPath], with: .fade)
                            break
                        case let .failure(error):
                            print(error)
                            break
                        }
                    })
                    
                    self.deleteItemIndexPath = nil
                }
                
            } cancelDeleteItem: { (action) in
                self.deleteItemIndexPath = nil
            }
        }
    
    }

让我知道你的想法,谢谢!

感谢那些已经发布答案的人,非常感谢。我还提出了另一个解决方案。因此,如果我们使用Tob示例,我们将扩展所有UIViewController的功能,如果我们使用h和.h示例,我们将装饰特定的视图控制器,但这会迫使我们使用I编写额外的惰性代码这没什么大不了的,我最喜欢这个示例,它没有使任何视图控制器能够使用UIAlerController扩展,但我将遵循一个基于协议的扩展解决方案:

导入UIKit

protocol UIAlertControllerManageable {
    func createAlert(with text: String, handleDeleteItem: @escaping (UIAlertAction) -> Void, cancelDeleteItem: @escaping  (UIAlertAction) -> Void)
}

extension UIAlertControllerManageable where Self: UIViewController {
   
    func createAlert(with text: String, handleDeleteItem: @escaping (UIAlertAction) -> Void, cancelDeleteItem: @escaping  (UIAlertAction) -> Void) {
        
        let alert = UIAlertController(title: "Delete", message: "Are you sure you want to permanently delete \(text)?", preferredStyle: .actionSheet)
         
        let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: handleDeleteItem)
         
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: cancelDeleteItem)
        
        alert.addAction(deleteAction)
        alert.addAction(cancelAction)
        
        self.present(alert, animated: true, completion: nil)
    }
}
用法:

class HomeViewController: UIViewController, UIAlertControllerManageable {
        
        func confirmDelete(deleteText: String) {
            
            createAlert(with: deleteText) { (action) in
                
                if let indexPath = self.deleteItemIndexPath {
                    self.presenter?.removeItem(atIndex: indexPath, completion: { (result) in
                        
                        switch result {
                        case .success(_):
                            self.tableView.deleteRows(at: [indexPath], with: .fade)
                            break
                        case let .failure(error):
                            print(error)
                            break
                        }
                    })
                    
                    self.deleteItemIndexPath = nil
                }
                
            } cancelDeleteItem: { (action) in
                self.deleteItemIndexPath = nil
            }
        }
    
    }

让我知道你的想法,谢谢!

我高估了你的答案谢谢!我刚刚提出了另一个解决方案。我发布了答案我高估了你的答案谢谢!我刚刚提出了另一个解决方案。我发布了答案我也高估了你的答案谢谢!我提出了另一个解决方案。答案是postedI我也高估了你的答案谢谢!我想出了另一个解决方案。答案已发布