我爱平铺

100天Swift学习挑战#7:UIAlertController

let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .actionSheet)
if let popoverController = optionMenu.popoverPresentationController {
    if let cell = tableView.cellForRow(at: indexPath) {
        popoverController.sourceView = cell
        popoverController.sourceRect = cell.bounds
    }
}

// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(cancelAction)

// Add Call action
let callActionHandler = { (action:UIAlertAction!) -> Void in
    let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
    alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    self.present(alertMessage, animated: true, completion: nil)
}

let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style: .default, handler: callActionHandler)
optionMenu.addAction(callAction)

let checkInAction = !self.restaurantIsVistied[indexPath.row] ? UIAlertAction(title: "Check in", style: .default, handler: { (action:UIAlertAction) -> Void in
    let cell = tableView.cellForRow(at: indexPath)
    cell?.accessoryType = .checkmark
    self.restaurantIsVistied[indexPath.row] = true
}) : UIAlertAction(title: "Undo Check in", style: .default, handler: { (action:UIAlertAction) -> Void in
    let cell = tableView.cellForRow(at: indexPath)
    cell?.accessoryType = .none
    self.restaurantIsVistied[indexPath.row] = false
})
optionMenu.addAction(checkInAction)

// Display the menu
present(optionMenu, animated: true, completion: nil)

文章原始链接:https://sijie.wang/posts/swift-challenge-7/

本站文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请保留原始链接