迅速:点击按钮即可选择 [英] swift: segue on button click

查看:153
本文介绍了迅速:点击按钮即可选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用segue在单击按钮时移至下一个控制器。我需要获取下一个控制器中的按钮数量。



这是我控制器中的代码:

  import UIKit 

class ViewController2:UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet var tblTable:UITableView!

var buttonTitles = [一个,两个,三个,四个,五个,六个,七个,八个,九个,十个 ]


覆盖func viewDidLoad(){
super.viewDidLoad()
tblTable.delegate = self
tblTable.dataSource = self
}


func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int {
return buttonTitles.count
}

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: buttoncell)为! ButtonCell

让buttonTitle:字符串= buttonTitles [indexPath.row]
cell.btnButton.setTitle(buttonTitle,用于:.normal)
cell.btnButton.tag = indexPath.row
cell.btnButton.addTarget(self,action:#selector(self.buttonClick(button :)),for:.touchUpInside)
cell.selectionStyle = .none
返回单元格
}

@objc func buttonClick(button:UIButton)->无效{
print( btnButton在索引处单击-\(button.tag))
button.isSelected =!button.isSelected

if button.isSelected {
button.backgroundColor = UIColor.green
}否则{
button.backgroundColor = UIColor.yellow
}
}

}


class ButtonCell:UITableViewCell {

@IBOutlet var btnButton:UIButton!

覆盖功能setSelected(_选择:布尔,动画:Bool){
super.setSelected(选择,动画:动画)

如果选择{
btnButton.backgroundColor = UIColor.green
}否则{
btnButton.backgroundColor = UIColor.yellow
}

}

覆盖func setHighlighted(_高亮显示:布尔,动画:布尔){
super.setHighlighted(高亮显示,动画:动画)

如果高亮显示{
btnButton.backgroundColor = UIColor.green
} else {
btnButton.backgroundColor = UIColor.yellow
}
}
}

如何用我的代码解决问题?

解决方案

这很简单。 / p>

请按照以下步骤从表格单元格按钮(单击)创建序列。


  1. 打开情节提要布局(查看c

  2. 添加新的(目标)视图控制器。

  3. 选择您的按钮。

  4. 按&按住键盘上的 ctrl 按钮并将鼠标光标从按钮拖到新的(目标)视图控制器。

  5. 现在将以下代码添加到您的源代码视图控制器文件(源代码)

-

  override func performSegue(withIdentifier identifier:String,sender:Any?){
print( segue-\(identifier))

如果让destinationViewController = segue.destination为? < YourDestinationViewController> {
如果让按钮=发送者为? UIButton {
secondViewController。< buttonIndex> = button.tag
// //注意:添加/定义var buttonIndex:< YourDestinationViewController>中的Int = 0并在viewDidLoad中打印。
}

}
}






另一种处理此问题的方法。


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?

解决方案

It's very simple.

Follow these steps to create segue from your tableview cell button (click).

  1. Open your storyboard layout (view controller)
  2. Add new (destination) view controller.
  3. Select your button.
  4. Press & hold control ctrl button from keyboard and drag mouse cursor from your button to new (destination) view controller.
  5. 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.

这篇关于迅速:点击按钮即可选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆