iOSアプリでボタンを押してアラートを表示してみる
iOSアプリでボタンを押してアラートを表示してみる。
右上のアシスタントエディタのボタンを押して、コードの画面を表示し、
ボタンを右クリックして、ドラッグする。
Actionを選択して、名前を入力し、[Connect]を押すと、
アラートを表示するコードを記入する。
let alertController = UIAlertController(title: "test", message: "Hello, world!", preferredStyle: .Alert) let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil) alertController.addAction(defaultAction) presentViewController(alertController, animated: true, completion: nil)
ちなみに、Main.storyboardのxmlには、下記のような感じでconnectionsタグが追加されている。
・・・ <connections> <action selector="helloWorld:" destination="・・・" eventType="touchUpInside" id="・・・"/> </connections> ・・・
テキストフィールドに入力された文字列をアラートで表示してみる
messageの所を修正する。
let alertController = UIAlertController(title: "test", message: "Hello, world!", preferredStyle: .Alert)
↓
let alertController = UIAlertController(title: "test", message: aaa.text, preferredStyle: .Alert)