Ios swift:点击按钮继续
我想移动到下一个控制器上的按钮点击使用segue。我需要得到下一个控制器中的按钮数 这是来自我的控制器的代码:Ios swift:点击按钮继续,ios,swift,uitableview,button,uistoryboardsegue,Ios,Swift,Uitableview,Button,Uistoryboardsegue,我想移动到下一个控制器上的按钮点击使用segue。我需要得到下一个控制器中的按钮数 这是来自我的控制器的代码: import UIKit class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var tblTable: UITableView! var buttonTitles = ["One", "Two", "Three", "Fou
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
}
}
}
如何用我的代码解决这个问题?您需要使用
performsguewithidentifier(“yourSegue”,sender:sender)
对事件进行分段。这会将您的segue标识符替换为“yourSegue”
当用户按下按钮时,这将进入您调用的func中。如果需要将按钮点击量发送到新的视图控制器,则我将执行类似的操作:
let secondViewController = segue.destination as! ViewController
secondViewController.buttonClicks = myButtonClicks
这很简单
按照以下步骤从tableview单元格按钮(单击)创建segue
ctrl
按钮,并将鼠标光标从按钮拖动到新的(目标)视图控制器override func performsgue(带标识符:String,发送方:Any?){
打印(“segue-\(标识符)”)
如果让destinationViewController=segue.destination as{
如果let button=发送方为UIButton{
secondViewController.=button.tag
//注意:在中添加/定义变量按钮index:Int=0,并在viewDidLoad中打印。
}
}
}
另一种处理方法。有什么问题?为什么要使用@objc。?你有什么错误吗?
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.
}
}
}