Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

ios - Extending UIViewController(s) to show UIAlertController without subclassing

I would like to reuse code to show alert when I delete some row in tableview in a few my view controllers:

func confirmDelete(item: String) {

    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 
}

The only one part will be different everytime:

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
                }
            })

So as you can see I can simple do subclassing and declare some closure variable which will be triggered each time deleteAction invoked.

It's very simple way, but I am not super fun of subclassing. Maybe there is some help-full extension, protocol based thing or any other suggestions.

question from:https://stackoverflow.com/questions/66055761/extending-uiviewcontrollers-to-show-uialertcontroller-without-subclassing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could write a class extension to view controller:

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)
    }
}

And then pass in the appropriate delete and cancel functions for each different view controller.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...