我想通过 UITableViewRowButtonAction 打开一个新的 ViewController
I want to open a new ViewController through UITableViewRowButtonAction
我有一个 UITableViewRowAction
所以当我滑动时有 3 个选项可供选择。如果我单击 Call
按钮,我希望在整个屏幕上弹出一个新的 ViewController
。如果我点击新 ViewController
中的按钮,我希望它被关闭 。
单击“呼叫”按钮会打开一个 ViewController 作为弹出窗口
这在我点击按钮后打开,点击底部的 x 按钮后应该会消失
这是我的代码
func buttonToDismiss (sender: AnyObject) {
self.presentedViewController?.dismiss(animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let callButton = UITableViewRowAction(style: .default, title: "Call", handler: { (action, indexPath) in
self.tableView.dataSource?.tableView?(
self.tableView,
commit: .delete,
forRowAt: indexPath)
let vc = UIViewController(nibName: nil, bundle: nil)
vc.view.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
vc.view.backgroundColor = UIColor(red: 62/255.0, green: 70/255.0, blue: 80/255.0, alpha: 1.0)
vc.modalPresentationStyle = .popover
let declineButton = UIButton()
declineButton.frame = CGRect(x: 150, y: 484, width: 75, height: 75)
declineButton.backgroundColor = UIColor(red: 36/255.0, green: 44/255.0, blue: 55/255.0, alpha: 1.0)
declineButton.tintColor = UIColor.white
declineButton.layer.cornerRadius = declineButton.frame.size.height / 2
declineButton.layer.masksToBounds = true
declineButton.clipsToBounds = true
declineButton.setTitle("X", for: .normal)
declineButton.addTarget(self, action: Selector(("buttonToDismiss:")), for: UIControlEvents.touchUpInside)
vc.view.addSubview(declineButton)
let popover = vc.popoverPresentationController!
let cell = tableView.cellForRow(at: indexPath)!
var cellAbsolutePosition = cell.superview!.convert(cell.frame.origin, to: nil)
cellAbsolutePosition.x = cell.frame.width - 60
popover.sourceRect = CGRect(origin: cellAbsolutePosition, size: cell.frame.size)
popover.sourceView = tableView
self.present(vc, animated: true, completion: nil)
return
})
我知道我认为代码很混乱,但我还不太擅长编写应用程序。
感谢您的帮助,并提前感谢您的努力。
做这个而不是你正在做的事情:
- 创建您想要在选择
call action
时显示的 ViewController
- 将
dismiss
按钮放在 ViewController
上并将其连接到 IBAction
- 当用户选择
call action
时,从 storyboard
实例化 ViewController
并简单地显示它
这是一个简单的例子:
假设这是您要在触发呼叫操作时显示的 ViewController
在 call action
中实例化并呈现 ViewController
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
var rowActions = [UITableViewRowAction]()
let callAction = UITableViewRowAction.init(style: .default,
title: "Call") { (action, cellPath) in
//instantiate the view controller with storyboard ID
let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
self.present(vc, animated: true, completion: {
})
}
rowActions.append(callAction)
return rowActions
}
只需将按钮与 IBAction
连接起来
class DetailViewController: UIViewController {
var delegate: DetailDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func didTapBtn(_ sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
}
试试这个代码: 在 Swift 3.
中测试
主VC:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let callAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Call") { (UITableViewRowAction, NSIndexPath) -> Void in
self.performSegue(withIdentifier: "callSegue", sender: self)
}
let videoAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Video") { (UITableViewRowAction, NSIndexPath) -> Void in
}
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Delete") { (UITableViewRowAction, NSIndexPath) -> Void in
}
callAction.backgroundColor = .blue
videoAction.backgroundColor = .green
deleteAction.backgroundColor = .red
return [deleteAction,videoAction,callAction]
}
DestVC
@IBAction func dissmissButton(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
我有一个 UITableViewRowAction
所以当我滑动时有 3 个选项可供选择。如果我单击 Call
按钮,我希望在整个屏幕上弹出一个新的 ViewController
。如果我点击新 ViewController
中的按钮,我希望它被关闭 。
单击“呼叫”按钮会打开一个 ViewController 作为弹出窗口
这在我点击按钮后打开,点击底部的 x 按钮后应该会消失
这是我的代码
func buttonToDismiss (sender: AnyObject) {
self.presentedViewController?.dismiss(animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let callButton = UITableViewRowAction(style: .default, title: "Call", handler: { (action, indexPath) in
self.tableView.dataSource?.tableView?(
self.tableView,
commit: .delete,
forRowAt: indexPath)
let vc = UIViewController(nibName: nil, bundle: nil)
vc.view.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
vc.view.backgroundColor = UIColor(red: 62/255.0, green: 70/255.0, blue: 80/255.0, alpha: 1.0)
vc.modalPresentationStyle = .popover
let declineButton = UIButton()
declineButton.frame = CGRect(x: 150, y: 484, width: 75, height: 75)
declineButton.backgroundColor = UIColor(red: 36/255.0, green: 44/255.0, blue: 55/255.0, alpha: 1.0)
declineButton.tintColor = UIColor.white
declineButton.layer.cornerRadius = declineButton.frame.size.height / 2
declineButton.layer.masksToBounds = true
declineButton.clipsToBounds = true
declineButton.setTitle("X", for: .normal)
declineButton.addTarget(self, action: Selector(("buttonToDismiss:")), for: UIControlEvents.touchUpInside)
vc.view.addSubview(declineButton)
let popover = vc.popoverPresentationController!
let cell = tableView.cellForRow(at: indexPath)!
var cellAbsolutePosition = cell.superview!.convert(cell.frame.origin, to: nil)
cellAbsolutePosition.x = cell.frame.width - 60
popover.sourceRect = CGRect(origin: cellAbsolutePosition, size: cell.frame.size)
popover.sourceView = tableView
self.present(vc, animated: true, completion: nil)
return
})
我知道我认为代码很混乱,但我还不太擅长编写应用程序。
感谢您的帮助,并提前感谢您的努力。
做这个而不是你正在做的事情:
- 创建您想要在选择
call action
时显示的ViewController
- 将
dismiss
按钮放在ViewController
上并将其连接到IBAction
- 当用户选择
call action
时,从storyboard
实例化ViewController
并简单地显示它
这是一个简单的例子:
假设这是您要在触发呼叫操作时显示的 ViewController
在 call action
ViewController
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
var rowActions = [UITableViewRowAction]()
let callAction = UITableViewRowAction.init(style: .default,
title: "Call") { (action, cellPath) in
//instantiate the view controller with storyboard ID
let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
self.present(vc, animated: true, completion: {
})
}
rowActions.append(callAction)
return rowActions
}
只需将按钮与 IBAction
class DetailViewController: UIViewController {
var delegate: DetailDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func didTapBtn(_ sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
}
试试这个代码: 在 Swift 3.
中测试主VC:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let callAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Call") { (UITableViewRowAction, NSIndexPath) -> Void in
self.performSegue(withIdentifier: "callSegue", sender: self)
}
let videoAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Video") { (UITableViewRowAction, NSIndexPath) -> Void in
}
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Delete") { (UITableViewRowAction, NSIndexPath) -> Void in
}
callAction.backgroundColor = .blue
videoAction.backgroundColor = .green
deleteAction.backgroundColor = .red
return [deleteAction,videoAction,callAction]
}
DestVC
@IBAction func dissmissButton(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}