본문 바로가기

SWIFT

AlertView 띄우기 (iOS7,8)- Swift



Swift에서 AlertView나 ActionSheet를 뿌리는데 UIAlertController가 사용되는데ㅡ 
이게 iOS8에서만 잘 돌아가고 iOS7에서는 안된다;;; 
디바이스 버전대로 다르게 타는 메소드를 하나 만들어봤다. 
버튼을 추가로 달고 싶다면 속만 떼어서 만들면 될듯. 

func showAlert(Msg: NSString, viewController : UIViewController) -> Void {

        let floatVersion = (UIDevice.currentDevice().systemVersion as NSString).floatValue

        if (floatVersion >= 8.0){

            println("showAlert_swift")

            let alert = UIAlertController(title:NSLocalizedString("AppTitle", comment: "Common"), message: Msg, preferredStyle: UIAlertControllerStyle.Alert)

            let okButton = UIAlertAction(title:NSLocalizedString("OK", comment: "Common"), style: UIAlertActionStyle.Default) { (okSelected) -> Void in

                println("Ok Selected")

            }

            alert.addAction(okButton)

            viewController.presentViewController(alert, animated: true, completion: nil)

        }else{

            var alert = UIAlertView();

            alert.title  = NSLocalizedString("AppTitle", comment: "Common")

            alert.delegate = self

            alert.message = Msg

            alert.addButtonWithTitle(NSLocalizedString("OK", comment: "Common"))

            alert.cancelButtonIndex = 0;

            alert.show()

        }

    }



'SWIFT' 카테고리의 다른 글

스위프트 ARC와 순환참조와 클로저  (0) 2017.09.07
guard 문  (0) 2016.04.07
Swift Singletons 구현 방법  (0) 2014.12.09
Apple Swift Programming Language for KOREAN - 배포중  (0) 2014.06.12