问题
I want to move to the next controller on button click with using segue. I need to get number of press button in next controller.
This is code from my controller:
import UIKit
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tblTable: UITableView!
var buttonTitles = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]
override func viewDidLoad() {
super.viewDidLoad()
tblTable.delegate = self
tblTable.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return buttonTitles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "buttoncell") as! ButtonCell
let buttonTitle: String = buttonTitles[indexPath.row]
cell.btnButton.setTitle(buttonTitle, for: .normal)
cell.btnButton.tag = indexPath.row
cell.btnButton.addTarget(self, action: #selector(self.buttonClick(button:)), for: .touchUpInside)
cell.selectionStyle = .none
return cell
}
@objc func buttonClick(button: UIButton) -> Void {
print("btnButton clicked at index - \(button.tag)")
button.isSelected = !button.isSelected
if button.isSelected {
button.backgroundColor = UIColor.green
} else {
button.backgroundColor = UIColor.yellow
}
}
}
class ButtonCell: UITableViewCell {
@IBOutlet var btnButton: UIButton!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
btnButton.backgroundColor = UIColor.green
} else {
btnButton.backgroundColor = UIColor.yellow
}
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
if highlighted {
btnButton.backgroundColor = UIColor.green
} else {
btnButton.backgroundColor = UIColor.yellow
}
}
}
How to solve the problem it with my code?
回答1:
It's very simple.
Follow these steps to create segue from your tableview cell button (click).
- Open your storyboard layout (view controller)
- Add new (destination) view controller.
- Select your button.
- Press & hold control
ctrl
button from keyboard and drag mouse cursor from your button to new (destination) view controller. - Now add following code to your source view controller file (source code)
-
override func performSegue(withIdentifier identifier: String, sender: Any?) {
print("segue - \(identifier)")
if let destinationViewController = segue.destination as? <YourDestinationViewController> {
if let button = sender as? UIButton {
secondViewController.<buttonIndex> = button.tag
// Note: add/define var buttonIndex: Int = 0 in <YourDestinationViewController> and print there in viewDidLoad.
}
}
}
Another way to handle the same.
回答2:
You need to use performSegueWithIdentifier("yourSegue", sender: sender)
to segue on an event. This takes in your segue identifier in place of "yourSegue".
This will go in the func that you call when the user presses the button. If you need to send the amount of button clicks to the new View Controller then I would do something similar to this:
let secondViewController = segue.destination as! ViewController
secondViewController.buttonClicks = myButtonClicks
来源:https://stackoverflow.com/questions/45739295/swift-segue-on-button-click