UILabelのタップイベントを処理する
UILabelのタップイベントを処理して、ラベルに表示する値をアラートダイアログで入力する方法です。
UIラベルのisUserInteractionEnabledをtrueにして、touchesEndedで処理します。
class ViewController: UIViewController { let timerLabelTag = 10 @IBOutlet weak var timerLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() timerLabel.tag = timerLabelTag timerLabel.isUserInteractionEnabled = true } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { for touch: UITouch in touches { let tag = touch.view!.tag if (tag == timerLabelTag) { // アラートダイアログで入力する let alert = UIAlertController(title: "タイマー時間セットアップ", message: "時間(秒)を入力してください", preferredStyle: .alert) let saveAction = UIAlertAction(title: "セット", style: .default) { (action:UIAlertAction!) -> Void in let textField = alert.textFields![0] as UITextField timerLabel = textField.text! } } let cancelAction = UIAlertAction(title: "キャンセル", style: .default) { (action:UIAlertAction!) -> Void in } // UIAlertControllerにtextFieldを追加 alert.addTextField { (textField:UITextField!) -> Void in } alert.addAction(saveAction) alert.addAction(cancelAction) present(alert, animated: true, completion: nil) } } } }
リンク
Excelを使って簡単にINSERT文を作る方法
今参加しているプロジェクトでは、データを挿入してテストすることがあるので、メモしておきます。 やっぱり、一つ一つInsert文を作成するより楽ですね。
リンク
iOS GameplayKitを使って見ました
@ITに、「iOS GameplayKitの「Agents, Goals, and Behaviors」で作る、鬼ごっごの鬼AI」という面白そうな記事がありましたので、試してみました。
GameplayKitを初めて使いましたが、簡単に使えるという印象を持ちました。 この連載には、AI的な要素もありそうで今後が楽しみです。
リンク
NotificationCenterを使ってみました
はじめに
iOSアプリで、ある画面で処理したタイミングで別画面の表示を更新したいことがあります。 このような場合には、Observerデザインパターンが有効です。 Observerパターンは、Listenerパターンとも呼ばれます。
Java言語の場合にはListenerをインタフェースとして実装することが多いです。
swiftでは、Observerデザインパターン用のクラスとしてNotificationCenterが提供されています。
実装
- 登録
- 登録解除
- 通知を送信
// Notification名を登録する public extension Notification { public static let MyNotificationName = Notification.Name("Notification.MyNotification") public static let MyNotificationNameUserInfo = Notification.Name("Notification.MyNotificationUserInfo”) } class Hoge : NSObject { override init() { super.init() // 登録 NotificationCenter.default.addObserver(self, selector: #selector(self.update), name: Notification.MyNotificationName, object: nil); // 通知を受けるメソッドです func update(notification: NSNotification) { print("receive Notification!") } // 登録解除 func removeObserver() { NotificationCenter.default.removeObserver(self, name: Notification.MyNotificationName, object: nil) } } var hoge = Hoge() // 通知する NotificationCenter.default.post(name: Notification.MyNotificationName object: nil)
通知するタイミングでデータを受け渡したい場合には、UserInfoが使えます。
class HogeUserInfo : NSObject { override init() { super.init() // 受信側(登録) NotificationCenter.default.addObserver(self, selector: #selector(self.update), name: MyNotificationNameUserInfo, object: nil) } // 通知を受けるメソッドです func update(notification: NSNotification) { print("receive Notification!") guard let userInfo = notification.userInfo, let message = userInfo["message"] as? String, let date = userInfo["date"] as? Date else { print("No userInfo found in notification") return } print(message); print(date); } } var hogeUserInfo = HogeUserInfo() // 通知する NotificationCenter.default.post(name: Notification. MyNotificationNameUserInfo, object: nil, userInfo:["message":"Hello there!", "date":Date()])
リンク
ホワイトハッカー入門(3)
引き続き、Kali Linuxの環境設定を行います。 なかなか肝心な部分に進めません。
ディスクにスペースがあれば、Virtual Boxのクローンを作成すると良いと思います。
- shの変更
- ディレクトリを日本語から英語に変更
- 日本語入力
shの変更
rootのshellが/bin/shだったのでbashに変更しました。
# chsh /bin/bash
ディレクトリを日本語から英語に変更
最初に行うのはディレクトリを日本語から英語に変更します。 作業はターミナル上で行うことが多いですが、その時日本語を入力するのは面倒です。 必要なパッケージをインストールしてから設定します。 確認ダイアログがでますので、Don’t ask againにチェックを入れUpdate Namesを選択します。
# apt-cache search xdg-user-dirs-gtk # apt-get install xdg-user-dirs-gtk # LANG=C xdg-user-dirs-gdk-update
日本語入力
日本語入力には、パッケージをインストールした後に、設定を行います。 設定 / Region & Language / 入力ソースで+ボタンを押してAnthyを追加します。 設定ダイアログは、アプリ検索でSettingを入力すると表示されます。 追加した日本語入力を有効に再起動が必要でした。
# apt-get update # apt-get install uim uim-anthy #apt-get install ibus-anthy
リンク
ホワイトハッカー入門(2)
引き続き、Kali Linuxのインストールを続けます。
Virtual Boxでは、ホスト機とのコピペやディレクトリを共有するために、additional toolsをインストールする必要があります。 特に、インストールが完了するまで、ホスト機とのコピペは必須です。事前にインストールしておきましょう。
Kali Linux を VirtualBox にインストールする | Webセキュリティの小部屋のページが良くまとまっています。Kali Linuxのインストールの解説ページで最後のあたりにadditional toolsのインストールの説明があります。
私の環境では、インストールが失敗しました。
追記) apt-get install -y linux-headers-$(uname -r)
を見落としていました。
# less /var/log/vboxadd-install.log Makefile:181: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR= and run Make again. Stop
このエラーが出た場合には、カーネルヘッダのインストールが必要となります。
# apt-get install linux-headers-$(uname -r)
Kali Linuxを再起動して、ホスト機とのコピペができれば成功です。 ちなみに、ショートカットは、Shift-Ctrl-C/Vです。ホスト機のショートカットとは異なるので注意が必要です。