LoginSignup
24
24

More than 5 years have passed since last update.

Swift×Eurekaで簡単に設定画面を作る

Posted at

背景

私が作ったアプリに設定画面を追加したくなり、色々探してみて簡単に作れたので備忘録として残します。
https://itunes.apple.com/jp/app/dunda/id1072574796?l=en&mt=8

設定画面を作る方法案

とりあえずぐぐってみると、便利そうなライブラリは下記2つくらいでした。
https://github.com/xmartlabs/Eureka
https://github.com/escoz/QuickDialog

QuickDialogはSwiftで実装している記事がなかなか見つからなかったのと、全然更新されていなかったのでEurekaを使うことに。

導入

GitHubのReadMeの下の方にInstallationがあるのでそこ参考にすればいけるかと。

設定画面を作る

まず、Storyboardで設定画面のViewを作成します。適当にSettingsViewControllerとかにしました。対応するSwiftファイルを作成し、下記のようにコードを書きました。

import Foundation
import Eureka


class SettingsViewController : FormViewController{

    let userDefault = NSUserDefaults.standardUserDefaults()
    let itunesURL:String = "itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=xxxx"

    override func viewDidLoad() {
        super.viewDidLoad()

        form +++ Section("通知時刻の設定")
        <<< TimeRow(){
            $0.title = "通知時刻"
            if let temp:AnyObject = self.userDefault.objectForKey("Time") {
                $0.value = temp as? NSDate
            }

            }.onChange{row in
                self.userDefault.setValue(row.value, forKey: "Time")
        }

        form +++ Section()
            <<< LabelRow("Review"){
                $0.title = "AppStoreで評価する"
                }.onCellSelection(){row in
                    self.userDefault.setBool(true, forKey: "Review")
                    let url = NSURL(string:self.itunesURL)
                    let app:UIApplication = UIApplication.sharedApplication()
                    app.openURL(url!)
        }
    }
}

どんな動きをするかは、コードを見ればわかると思いますが、実際触ってみたい方はアプリをインストールしてみてください。どんなアプリかは下記参照。
http://seiya-orz.hatenablog.com/entry/2016/01/17/202834

入力された値は「row.value」に格納されます。ただ、型がわからなかったのでどう扱えばよいか苦しみました。そんなときは「row.value.dynamicType」をprintすればどんな型で格納されているかわかるので、デバッグしてみてください。

詳しい書き方はGitHub見るか、下記サイトを見ればなんとかなると思います。
http://blog.personal-factory.com/2015/12/29/eureka-tutorial/

おわり。

24
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
24