iDev
UIAlertController 사용법
KraZYeom
2016. 1. 23. 18:59
반응형
난이도: 하
오랜만에 iOS 코딩을 하려니 많은게 바뀌었다.
UIAlertView
는 9.0에서 아래와 같이 deprecated 되었고, UIAlertController
를 사용하는 것을 권장한다.
@available(iOS, introduced=2.0, deprecated=9.0, message="UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead")
그리고 UIActionSheet
도 UIAlertController
를 사용하면 된다. 참고로 UIActionSheet
는 아래와 같이 iOS 8.3에서 deprecated 되었다.
@available(iOS, introduced=2.0, deprecated=8.3, message="UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead")
사용법
let alertController = UIAlertController(title: "Test Title", message: "Test Message", preferredStyle: UIAlertControllerStyle.Alert)
// .Alert .ActionSheet
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in
print("Okay")
}
alertController.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (UIAlertAction) -> Void in
print("cancel")
}
alertController.addAction(cancelAction)
let destructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.Destructive) { (UIAlertAction) -> Void in
print("Destructive")
}
alertController.addAction(destructiveAction)
self.presentViewController(alertController, animated: true) { () -> Void in
print("presentViewController")
}
Alert과 ActionSheet의 구분은 preferredStyle로 구분을 해주면 되고 나머지는 동일하다.
Alert과 ActionSheet 공통으로 주의해야 할 점은 .Default와 .Destructive는 여러 개를 .addAction해서 추가 해도 되는데, .Cancel은 2개 이상 만들어서 self.presentViewController 하면 crash 된다.
UIAlertController를 만들고, 각각의 UIAlertAction을 추가해주면 된다. 워낙 간단하니 다른 설명은 하지 않도록 하겠다.
반응형