为什么标签的文字没有更新?

时间:2017-08-22 03:56:44

标签: ios swift uitableview segue

我正在尝试将数据从表格视图单元格按钮传递到视图控制器。根据选择按钮的行,我希望相应地设置以下视图控制器标签的文本。但是,当我尝试这样做时,以下视图控制器中的标签保持空白,不会更改。我怎么能改变这个?

我在表视图控制器中向tipButton添加了一个操作,该操作会为KeysController FYI创建segue。

表格视图单元格的代码:

class DrillsTableViewCell: UITableViewCell {

var videoURL:[URL] = [URL(fileURLWithPath: "/Users/RalphSimmonds/Desktop/plainjayne/coolinDB/cook.mov"), URL(fileURLWithPath: "/Users/RalphSimmonds/Desktop/plainjayne/coolinDB/check.MOV")]
var video = URL(fileURLWithPath: String())
var initialRow = Int()
var firstTips = ["Tip 1: Stay Hydrated", "Tip 1: Keep elbow tucked", "x", "Tip 1: Take quick breaks:", "Tip 1: Keep your head up", "Tip 1: Don't cross your feet", "Tip 1: Don't do more than 15 reps"]

var player: AVPlayer?
var playerController = AVPlayerViewController()

@IBOutlet weak var drillTitle: UILabel!
@IBOutlet weak var tipButton: UIButton!
@IBAction func tipsButton(_ sender: UIButton) {
    print(String(initialRow))

    let tipsVC = UIStoryboard(name: "main", bundle: nil).instantiateViewController(withIdentifier: "KeysController") as! KeysController
    tipsVC.key1.text = firstTips[initialRow]
}

我正在尝试更新其标签的视图控制器的代码:

import UIKit

class KeysController: UIViewController {
    @IBOutlet weak var topLabel: UILabel!
    @IBOutlet weak var key1: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

2 个答案:

答案 0 :(得分:3)

在视图加载之前,您的插座没有接线。

尝试在KeysController中创建一个属性,例如:

var keyText: String?

在实例化视图控制器后设置属性:

tipsVC.keyText = firstTips[initialRow]

然后在您的KeysController.viewDidLoad方法中:

key1.text = keyText

答案 1 :(得分:0)

因为标签key1尚未加载,所以最好在viewdidload上更新它

 @IBAction func tipsButton(_ sender: UIButton) {
     print(String(initialRow))

     let tipsVC = UIStoryboard(name: "main", bundle: nil).instantiateViewController(withIdentifier: "KeysController") as! KeysController
     tipsVC.key1String = firstTips[initialRow] }




  import UIKit


      class KeysController: UIViewController {
     var key1String:String = ""


     @IBOutlet weak var topLabel: UILabel!
     @IBOutlet weak var key1: UILabel!


     override func viewDidLoad() {
         super.viewDidLoad()
         key1.text = key1String

     }