반응형
swift 2.0에 와서 UIActionSheet는 duplicated되었다. 기존의 Alert처럼 UIAlertController를 이용해 스타일을 적용하고, 액션을 추가한다.
아래와 같은 코드를 작성하면,
1 2 3 4 5 6 7 8 9 10 | let dialog = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.ActionSheet) let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil) let cancel = UIAlertAction(title: "cancel", style: UIAlertActionStyle.Cancel, handler: nil) let delete = UIAlertAction(title: "delete", style: UIAlertActionStyle.Destructive, handler: nil) dialog.addAction(ok) dialog.addAction(delete) dialog.addAction(cancel) self.presentViewController(dialog, animated: true, completion: nil) | cs |
아래와 같은 화면을 볼 수 있다.
핸들러에 클로저를 추가해서 사용자 임의 행동을 할 수도 있다.
1 2 3 | let delete = UIAlertAction(title: "delete", style: UIAlertActionStyle.Destructive) { (parameter) -> Void in print("사용자 임의 행동") } | cs |
함수의 마지막 파라미터가 클로저일 경우 밖으로 뺄 수 있으므로 빼주고 클로저 내용을 작성한다.
그 뒤 delete키를 클릭하면,
로그에 잘 찍힌다.
얼럿 컨트롤러에 텍스트 필드도 추가할 수 있다.
추가하는 방법은
UIALertController.addTextFieldWithConfigurationHandler { (textField: UITextField) -> Void in
// 숫자 입력 키패드를 띄우기
textField.keyboardType = UIKeyboardType.NumberPad
}
단, 텍스트 필드는 ActionSheet에는 적용되지 않는다. 액션시트에 적용할 경우
2015-10-21 14:15:02.648 Day3_study[18668:2503472] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'
위 메세지를 볼 수 있다.
반응형
댓글