Swift:如何从UITableViewCell,Action表中做segue

时间:2018-02-18 06:51:44

标签: swift tableview

如何从UITableViewCell创建一个segue? 我有一个操作表和一个“修改配置文件”选项,我想通过传递一个User类型的Object来制作另一个viewController。 这是我的代码:

    let actionsheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)

    actionsheet.addAction(UIAlertAction(title: "Programmer un RDV", style: UIAlertActionStyle.default, handler: { (action) -> Void in
        self.gotoCalendar()//Ouvrir la calendar
    }))
    actionsheet.addAction(UIAlertAction(title: "Liste des RDV", style: UIAlertActionStyle.default, handler: { (action) -> Void in
    }))
    actionsheet.addAction(UIAlertAction(title: "Factures", style: UIAlertActionStyle.default, handler: { (action) -> Void in
    }))
    actionsheet.addAction(UIAlertAction(title: "Envoyer un Whatsapp", style: UIAlertActionStyle.default, handler: { (action) -> Void in
    }))
    actionsheet.addAction(UIAlertAction(title: "Envoyer un SMS", style: UIAlertActionStyle.default, handler: { (action) -> Void in
    }))
    actionsheet.addAction(UIAlertAction(title: "Modifier", style: UIAlertActionStyle.default, handler: { (action) -> Void in
        //Ir i want to do segue to an other viewController, and pass data object
    }))
    actionsheet.addAction(UIAlertAction(title: "Supprimer", style: UIAlertActionStyle.default, handler: { (action) -> Void in
        self.deleteUser(); //Suppression du customer
    }))

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
    cancelAction.setValue(UIColor.red, forKey: "titleTextColor")

    actionsheet.addAction(cancelAction)
    myVC?.present(actionsheet, animated: true, completion: nil)

2 个答案:

答案 0 :(得分:1)

您可以在profileViewController中声明变量,例如profileDate,然后在选择动作表选项后,将数据传递给变量。

actionsheet.addAction(UIAlertAction(title: "Modifier", style: UIAlertActionStyle.default, handler: { (action) -> Void in
    // push new view controller in stack 
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("profileViewController") as profileViewController
    vc.profileDate = profileDate
    self.navigationController?.pushViewController(vc, animated: true)

}))

actionsheet.addAction(UIAlertAction(title: "Modifier", style: UIAlertActionStyle.default, handler: { (action) -> Void in
    // Move to next view controller without stack
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "profileViewController") as! ProfileViewController
    vc.profileDate = profileDate
    self.present(vc, animated: true, completion: nil)
}))

或使用performSegue

actionsheet.addAction(UIAlertAction(title: "Modifier", style: UIAlertActionStyle.default, handler: { (action) -> Void in
        self.performSegue(withIdentifier: "profileSegue", sender: self)

}))

下一步:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "profileSegue" {
        if let destinationVC = segue.destinationViewController as? ProfileViewController {
                destinationVC.profileDate  = "Example"
        }
    }
}

答案 1 :(得分:0)

UITableViewCell:

步骤创建协议: 在课程NewTableViewCell

之外添加此代码
protocol NewTableViewCellDelegate {
    // Indicates that the profile view option selected 
    func showProfileView(cellView : NewTableViewCell)
}

NewTableViewCell

中添加委托变量
var delegate: NewTableViewCellDelegate?

然后在行动表中:

actionsheet.addAction(UIAlertAction(title: "Modifier", style: UIAlertActionStyle.default, handler: { (action) -> Void in
       delegate!.showProfileView(cellView: self)
}))

现在通过使用协议继承UITableViewCell来确认控制器文件中的UITableViewCell协议。 喜欢:

class ViewController: UIViewController, NewTableViewCellDelegate {

    func showProfileView(cellView : NewTableViewCell)
   {
        // push new view controller in stack 
       let storyboard = UIStoryboard(name: "Main", bundle: nil)
       let vc = storyboard.instantiateViewControllerWithIdentifier("profileViewController") as profileViewController
       vc.profileDate = profileDate
       self.navigationController?.pushViewController(vc, animated: true)
  }

}