使用破坏性样式和UIAlertAction

时间:2018-10-17 20:46:24

标签: ios swift uialertcontroller

我是新手,我正在开发联系人应用程序。我希望能够删除当用户单击delete按钮时遇到的联系人,但是在实现它时遇到了麻烦。目前,我拥有一个让用户点击contact并拥有Show Details时可以看到cancel button的功能。但是对于我的删除,我希望能够摆脱我单击的任何联系人。我将代码粘贴到下面。

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedContact = contacts[indexPath.row] as? Contact
        let name = selectedContact!.contactName!
        let actionHandler = { (action:UIAlertAction!) -> Void in
            //            self.performSegue(withIdentifier: "EditContact", sender: tableView.cellForRow(at: indexPath))
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "ContactController")
                as? ContactsViewController
            controller?.currentContact = selectedContact
            self.navigationController?.pushViewController(controller!, animated: true)
        }

        let alertController = UIAlertController(title: "Contact selected",
                                                message: "Selected row: \(indexPath.row) (\(name))",
            preferredStyle: .alert)

        let actionDelete = UIAlertAction(title: "Delete" ,
                                   style: .destructive,
                                   handler:nil)

        let actionCancel = UIAlertAction(title: "Cancel",
                                         style: .cancel,
                                         handler: nil)
        let actionDetails = UIAlertAction(title: "Show Details",
                                          style: .default,
                                          handler: actionHandler)
        alertController.addAction(actionCancel)
        alertController.addAction(actionDetails)
        alertController.addAction(actionDelete)
        present(alertController, animated: true, completion: nil)
    }

1 个答案:

答案 0 :(得分:1)

您可以尝试所有有/无操作的操作,因为无需创建变量,然后添加它,就可以addAction直接使用它的处理程序

alert.addAction(UIAlertAction(title: "Delete", style:.destructive, handler: { (action) in 

      contacts.remove(at:indexPath.row) 
      tableView.deleteRows(at:[indexPath],with:.fade)

}))

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedContact = contacts[indexPath.row] as? Contact
    let name = selectedContact!.contactName! 
    let alertController = UIAlertController(title: "Contact selected",
                                            message: "Selected row: \(indexPath.row) (\(name))",
        preferredStyle: .alert)


     alertController.addAction(UIAlertAction(title: "Show Details", style:.default, handler: { (action) in 

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "ContactController")
            as? ContactsViewController
        controller?.currentContact = selectedContact
        self.navigationController?.pushViewController(controller!, animated: true)

    }))

    alertController.addAction(UIAlertAction(title: "Delete", style:.destructive, handler: { (action) in 

        contacts.remove(at:indexPath.row) 
        tableView.deleteRows(at:[indexPath],with:.fade)

    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style:.cancel, handler:nil))  

    present(alertController, animated: true, completion: nil)
}