프로그래밍 방식으로 UISwitch를 만들려고합니다 (IBOutlets 또는 IBActions 없음). 눌렀을 때 스위치가 상태를 변경하는 방법을 파악할 수 없습니다. 나는 생각했다 ... 그 Swift - UISwitch가 켜지거나 꺼질 때 업데이트되지 않습니다.
mySwitch.addTarget()
... 스위치가 켜기/끄기 전환 할 때마다 호출 될 것이지만, 그런 경우가 될 것 같지 않습니다. 누군가 내가 뭘 잘못하고 어떻게 수정 설명 할 수 있을까요.
import UIKit
import XCPlayground
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320 * 0.75, height: 568 * 0.75))
view.backgroundColor = UIColor.whiteColor()
view.layer.borderColor = UIColor.grayColor().CGColor
view.layer.borderWidth = 1
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 320 * 0.75, height: 50))
label.text = "Label"
label.textAlignment = NSTextAlignment.Center
view.addSubview(label)
func switchChanged(sender: UISwitch!) {
if sender.on == true {
label.text = "Switch is ON"
} else if sender.on == false {
label.text = "Switch is OFF"
}
}
let mySwitch = UISwitch()
mySwitch.center = view.center
mySwitch.setOn(true, animated: false)
mySwitch.onTintColor = UIColor.redColor()
mySwitch.addTarget(label, action: Selector(switchChanged(mySwitch)), forControlEvents: UIControlEvents.ValueChanged)
view.addSubview(mySwitch)
XCPlaygroundPage.currentPage.liveView = view
놀이터에서 이런 심각한 상호 작용을 시도하지 마십시오. 개구리에게 키스를하는 것과 같습니다. 아무 일도 일어나지 않아 개구리가 화가납니다. – matt