はてな記法でSwift言語を扱ってみる
Swiftで遊ぼう!の古い記事-> Life-LOG OtherSide
あしたさぬきブログから「Swiftで遊ぼう!」をHatenaに移そうと考えたのは、HatenaでSwiftのコードブロックをシンタックス・ハイライトできるということを知ったからです。
はてな記法とMarkdownのシンタックス・ハイライトで、Swift言語に対応しました
ということで、ちょっと使ってみる。
@IBAction func buttonPressed(sender: UIButton) { let controller = UIAlertController(title: "いいんですか?", message: nil, preferredStyle: .ActionSheet) let yesAction = UIAlertAction(title: "はい!いいですよ", style: .Destructive, handler: { action in let msg = self.nameField.text.isEmpty ? "気楽にいこう 大丈夫だよ" : "気楽にいこう\(self.nameField.text)," + "大丈夫だよ" let controller2 = UIAlertController(title: "何かをやり遂げたよ", message: msg, preferredStyle: .Alert) let cancelAction = UIAlertAction(title: "わおー", style: .Cancel, handler: nil) controller2.addAction(cancelAction) self.presentViewController(controller2, animated: true, completion: nil) }) let noAction = UIAlertAction(title: "駄目だ!", style: .Cancel, handler: nil) controller.addAction(yesAction) controller.addAction(noAction) if let ppc = controller.popoverPresentationController { ppc.sourceView = sender ppc.sourceRect = sender.bounds\ } presentViewController(controller, animated: true, completion: nil) }
このコードはボタンを押すと出てくるAction SheetとAlertをコントロールするUIAlertControllerとUIAlertActionを扱っているbuttonPressed()メソッドのコードだ。
このコードを理解するためにUIAlertControllerクラスとUIAlertActionクラスを知らないといけないだろう。
私はこのAPIクラスを全く知らないのでこれから勉強する。
簡単に理解するとUIAlertControllerはUIViewControllerのサブクラスになるので、アラート画面をコントロールすることができる。アラート画面といってもデフォルトで用意されているのが、Action SheetとAlertだ。
そして、このUIAlertControllerで制御できるのは画面だけで、その中に設置するボタンはUIAlertActionクラスで用意してやらないといけない。
なんかややこしいですね。でも先人の知恵、慣れないといけませんね。
じゃあコードの解説はこれからです。今日はシンタックス・ハイライトを使ってみました。