如何通过单击按钮来传递标签文本?

时间:2019-01-13 03:55:45

标签: ios swift uitableview uicollectionview

我希望当我单击第一个按钮时,将标签文本打印在下一个视图控制器中的另一个标签上。

var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"] 
var buttonnames = ["one","two","three","four","five"]

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let  frame = tableView.frame
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40))
    let button = UIButton(frame: CGRect(x: tableView.frame.width - 70, y: 0, width: 70, height: 40))
    button.setTitle("more", for: UIControlState.normal)
    button.backgroundColor = UIColor.red
    button.setTitle(buttonnames[section], for:  UIControlState.normal)
    button.addTarget(self, action: #selector(gotonext(_:)), for: .touchUpInside)
    headerView.addSubview(button)
    let  label = UILabel(frame: CGRect(x: 5, y: 0, width:tableView.frame.width/2 , height: 40))
    label.text = self.categories[section]
    headerView.addSubview(label)
    return headerView
}

@ objc func gotonext(_ sender:UIButton){
    let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
    self.navigationController?.pushViewController(nextVC, animated: false)
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return categories[section]
}

func numberOfSections(in tableView: UITableView) -> Int {
    return categories.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
    return cell
}

我的手机代码是:

import UIKit

class CategoryRow : UITableViewCell{
    @IBOutlet weak var collectionView: UICollectionView!
}

extension CategoryRow : UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! VideoCell
        return cell
    }

}

extension CategoryRow : UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        let itemsPerRow:CGFloat = 4
        let hardCodedPadding:CGFloat = 5
        let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
        let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
        return CGSize(width: itemWidth, height: itemHeight)
    }
}

我希望当我单击按钮时,标签文本显示在下一个视图控制器中。在我的下一个视图控制器中,只有一个标签。因此,总是通过单击任意按钮来更改标签文本。标签位于按钮的前面,因此当我单击该按钮时,只会显示该标签文本。

image

1 个答案:

答案 0 :(得分:0)

在表格视图中的viewForHeaderInSection ..使用

button.addTarget(self, action: #selector(gotonext(_:)), for: .touchUpInside)
button.tag = section
headerView.addSubview(button)

然后在您的函数中

@ objc func gotonext(_ sender:UIButton){
    let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
    nextVC.myTitle = self.categories[sender.tag]
    self.navigationController?.pushViewController(nextVC, animated: false)
}

在您的NextViewController中创建一个带有名称的变量。您将在myTitle中获得该值

var myTitle:String?