내 코드가 작동하지 않습니다. 나는 이유를 모른다. 문제는 switchChanged 함수의 속성입니다. 속성이 비어 있으면 코드가 작동합니다.UISwitch를 올바르게 사용하는 방법은 무엇입니까?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rect = CGRectMake(130, 100, 0, 0)
let uiSwitch = UISwitch(frame: rect)
uiSwitch.setOn(true, animated: true)
uiSwitch.addTarget(self, action: "switchChanged", forControlEvents: UIControlEvents.ValueChanged)
self.view.addSubview(uiSwitch)
}
func switchChanged(uiSwitch: UISwitch) {
var message = "Turn on the switch"
if uiSwitch.on {
message = "Turn off the switch"
} else {
message = "Turn on the switch"
}
let alert = UIAlertController(title: "Information", message: message, preferredStyle: UIAlertControllerStyle.Alert)
let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
}
오류 : "libc의 ++ abi.dylib : 유형 NSException의 캐치되지 않는 예외로 종료"
이미 태그에 Swift 사용을 표시 했으므로 제목에 추가 할 필요가 없습니다. –