xcode swift不会更改标签文字

时间:2015-09-16 19:33:10

标签: ios swift uilabel exc-bad-instruction

我有两个观点。首先是“homeView”,第二个是“detailView”。

在homeView我有两个按钮 BUTTON1 BUTTON2

in detailView我有标签 label1的

当我按下button1时,应该更改为“detailView”,label1.text应为“button1gedrückt” 当我按下button2时,应该更改为“detailView”,label1.text应为“button2gedrückt”

当我使用代码时:

@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!

@IBAction func button1(sender: UIButton) {

    let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("detailview")
    self.showViewController(vc as! UIViewController, sender: vc)


    // self.label1.text = "button 1 gedrückt"

}

视图会改变,但label1.text不会改变。 如果我使用

@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!

@IBAction func button1(sender: UIButton) {

    let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("detailview")
    self.showViewController(vc as! UIViewController, sender: vc)


    self.label1.text = "button 1 gedrückt"

}

我会收到错误: enter image description here

我不知道我能做些什么?

2 个答案:

答案 0 :(得分:3)

确保您的标签已连接到您的插座或是否重复。如果可以,请尝试删除连接并再次连接。图像低于我所说的内容。

enter image description here

答案 1 :(得分:0)

非常感谢。 现在我可以使用此代码更改label1中的文本

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var pickerView: UIPickerView!

    let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // pickerView delegate n dataSource
        pickerView.dataSource = self
        pickerView.delegate = self
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return UnitDataArray.count
    }

    //MARK: Delegates
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return UnitDataArray[row]
    }

    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
    {
        var pickerLabel = UILabel()
        pickerLabel.textColor = UIColor.blackColor()
        pickerLabel.text = UnitDataArray[row]
        if let pickerViewFont = UIFont(name: "HelveticaNeue-Thin", size: 30) {
            pickerLabel.font = pickerViewFont
        }
        pickerLabel.textAlignment = NSTextAlignment.Center
        return pickerLabel
    }

    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40.0
    }
}

现在又是另一个问题?我可以将这个“覆盖功能”放入IBAction吗?因为我想添加更多按钮。每个按钮都应该在label1中有另一个文本。

谢谢(我是Xcode和swift的绝对初学者)