首页 > 解决方案 > 如何通过单击按钮传递标签文本?

问题描述

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

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)
    }
}

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

图片

标签: iosswiftuitableviewuicollectionview

解决方案


在您的 tableview 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?

推荐阅读