我有两个观点。首先是“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"
}
我不知道我能做些什么?
答案 0 :(得分:3)
答案 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的绝对初学者)