2017-04-01 6 views
-1
//This is the label 

let changeLbl = UILabel(frame: CGRect(x: 20, y: 170, width: 300, height: 21)) 

self.view.addSubview(changeLbl) 

//This is the button 

let submitButton = UIButton(type: .system) // let preferred over var here 

submitButton.addTarget(self, action: #selector(buttonAction) , for: UIControlEvents.touchUpInside) 

self.view.addSubview(submitButton) 

//and this is action of above button 

func buttonAction(sender: UIButton!) { 

} 

次のボタン機能で上記のラベルを呼び出したいですか? likeボタンアクションでラベルをプログラムで呼び出す/アクセスする方法は?

func buttonAction(sender: UIButton!) { 
    changeLbl.ishidden = true 

//ここでラベルにアクセスしますが、この方法では動作しません。 }

+0

あなたが直面している問題は何ですか? – shallowThought

+0

ボタン機能のラベルにアクセスできない... @shallowThought – Bilal

+0

「ラベルにアクセスできない」とはどういうことでしょうか?コンパイルエラー?ランタイムエラー?どちら? – shallowThought

答えて

1

changeLblにアクセスする場合は、このようにインスタンス変数として定義し、クラス内にグローバルスコープを指定する必要があります。

class ViewController: UIViewController { 

    var changeLbl : UILabel! 
    var submitButton : UIButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //This is the UILabel 
     changeLbl = UILabel(frame: CGRect(x: 20, y: 170, width: 300, height: 21)) 

     self.view.addSubview(changeLbl) 

     //This is the button 
     submitButton = UIButton(type: .system) // let preferred over var here 

     submitButton.addTarget(self, action: #selector(buttonAction) , for: UIControlEvents.touchUpInside) 
     self.view.addSubview(submitButton) 

    } 

    //and this is action of above button 

    func buttonAction(sender: UIButton!) { 
     changeLbl.isHidden = true 
    } 
} 
0

changeLblはローカル変数である。ラベルが作成されるメソッドの範囲でのみ表示されます。

あなたはインスタンス変数を使用しない場合 - 通常の方法である - そして唯一のUILabelあなたは、アクセスラベルをしたい場合は、subviews配列

func buttonAction(sender: UIButton) { 
    if let changeLbl = self.view.subviews.filter({ $0 is UILabel}).first { 
     changeLbl.ishidden = true 
    } 
} 
+0

このアプローチは壊れやすく、推奨しません。同様に 'UILabel'から' UITextField'にラベルビューを変更することにした場合 –

0

からラベルを取得することができますがありますグローバルに宣言してください。 以下のコード:

class LabelDemo: UIViewController{ 
    var changeLbl: UILabel! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     changeLbl = UILabel(frame: CGRect(x: 20, y: 170, width: 300, height: 21)) 
     changeLbl.backgroundColor = UIColor.black 
     self.view.addSubview(changeLbl) 

     //This is the button 

     let submitButton = UIButton(type: .system) // let preferred over var here 
     submitButton.backgroundColor = UIColor.green 
     submitButton.frame = CGRect(x: 50, y: 150, width: 50, height: 30) 
     submitButton.setTitle("hide", for: .normal) 
     submitButton.addTarget(self, action: #selector(buttonAction) , for: UIControlEvents.touchUpInside) 

     self.view.addSubview(submitButton) 


    } 
    func buttonAction(sender: UIButton!) { 
     if changeLbl.isHidden { 
      changeLbl.isHidden = false 
     }else{ 
      changeLbl.isHidden = true 
     } 

    } 
} 
+0

'labelDemo'のようなクラス名は大文字で始まらなければなりません –

+0

jignesh Vadadoriyaはまず完全で正しい答えを提供しましたあなたの答えはそれに何を加えていますか? –

関連する問題