2016-10-05 10 views
1

Swift PlaygroundアプリでUIKitを使用しようとしています。 ラベルとテキストフィールドを動作させることができます。 UIButtonを操作して操作する方法(例えば、label.textを変更するなど)をタッチすると、そのUIButtonを取得する方法がわかりません。iPadのSwift PlaygroundアプリでUIButtonを実行する方法

import PlaygroundSupport 
import UIKit 

// 

class MyView: UIView { 

    //Method to be called 
    func printname() 
    { 
     print ("clicked") 
    } 

    func buttonPressed(sender: UIButton) 
    { 
     //sender.backgroundColor = #colorLiteral(red: 0.725490212440491, green: 0.47843137383461, blue: 0.0980392172932625, alpha: 1.0) 
     print ("Here") 
    } 
} 

func printname2() 
{ 
    print("button pressed") 
} 

let view = MyView() 
view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0) 
PlaygroundPage.current.liveView = view 

let lbl = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50)) 
lbl.text = "Hello, World!" 
view.addSubview(lbl) 

let txt = UITextField(frame: CGRect(x: 150, y: 200, width: 200, height: 50)) 
txt.placeholder = "Enter text here" 
//txt.font = UIFont.systemFont(ofSize: 15) 
txt.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) 
txt.borderStyle = UITextBorderStyle.roundedRect 
view.addSubview(txt) 

let button = UIButton(frame: CGRect(x: 50, y: 100, width: 100, height: 50)) 
button.backgroundColor = #colorLiteral(red: 0.721568644046783, green: 0.886274516582489, blue: 0.592156887054443, alpha: 1.0) 
//button.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected) 
button.setTitle("button", for: UIControlState.selected) 
button.layer.cornerRadius = 10 
button.addTarget(view, action: #selector(MyView.printname), for:  UIControlEvents.touchUpInside) 
view.addSubview(button) 

答えて

0

コードのマイナーチェンジが私のために働いた。同じ問題を満たす人のため

、提案:

  1. Responserクラスを定義します。
  2. あなたは、このクラス内のボタンにリンクするアクション
  3. は、インスタンスにResponser
  4. addTargetのインスタンスを定義し、アクション

コード例

import PlaygroundSupport 
import UIKit 

// 
let view = UIView() 
view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0) 
view.frame = CGRect(x: 0, y: 0, width: 400, height: 300) 


let lbl = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50)) 
lbl.text = "Hello, World!" 
view.addSubview(lbl) 

let txt = UITextField(frame: CGRect(x: 150, y: 200, width: 200, height: 50)) 
txt.placeholder = "Enter text here" 
//txt.font = UIFont.systemFont(ofSize: 15) 
txt.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) 
txt.borderStyle = UITextBorderStyle.roundedRect 

view.addSubview(txt) 

let button = UIButton(frame: CGRect(x: 50, y: 100, width: 100, height: 50)) 
button.backgroundColor = #colorLiteral(red: 0.721568644046783, green: 0.886274516582489, blue: 0.592156887054443, alpha: 1.0) 
//button.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected) 
button.setTitle("button", for: UIControlState.selected) 
button.layer.cornerRadius = 10 
view.addSubview(button) 

class Responser: NSObject 
{ 

    //Method to be called 
    func printname() 
    { 
     print ("clicked") 
     lbl.text = "aha" 
    } 
} 

let responder = Responser() 
button.addTarget(responder, action: #selector(Responser.printname), for:.touchUpInside) 

PlaygroundPage.current.liveView = view 
をリンク書きます
関連する問題