ios - 如何通过单击按钮传递标签文本?
问题描述
我希望当我单击我的第一个按钮时,标签的文本会打印在位于下一个视图控制器中的另一个标签上。
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)
}
}
我希望当我单击按钮时,标签文本会显示在下一个视图控制器中。在我的下一个视图控制器中,只有一个标签。因此,标签文本总是通过单击任何按钮来更改。按钮前面的标签,因此当我单击该按钮时,仅显示该标签文本。
解决方案
在您的 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?
推荐阅读
- android - 更改字段文本的 PDFbox 字体
- android - 如何在 react-native 中添加 box-shadow?
- python - tkinter 标签中表格结构的 Tkinter 问题
- node.js - MongoDB - 批量查找和 updateOne 无法按预期工作
- javascript - 使用 EJS 的骰子滚轮
- php - 如何在POST方法的下一个php页面上接收从标题(位置:.....)发送的数据?
- python - 使用 Python 仅从 txt 文件中提取所需的列?
- ios - 如何将 AppiumDriver 焦点更改为出现在不同图层上的下拉菜单?iOS
- javascript - JQuery在页面上输出“未定义”
- javascript - Jquery Validation:在提交时验证一个字段一次