LoginSignup
2
2

More than 5 years have passed since last update.

[Swifty]MVCのModel

Posted at

Try! Swift 2016での発表『文化を調和させる: 関数型プログラミング、プロトコル志向プログラミング、オブジェクト指向プログラミングの優れたテクニックを取り入れる』の手法をXcodeの新規プロジェクト(iOS / Application / Master-Detail Application)で作成されたソースコードに適用してたことをシリーズで発表する。

発表では、データをViewControllerのインスタンス変数として保持したが、自分流では必ずModel(Dataコントローラ)を用意しているので、今回もそうした。

class Document: NSObject {
    static let sharedInstance: Document = {
        let instance = Document()
        return instance
    }()
    
    private override init() {
    }
    
    private let hand = Hand()
}
class MasterViewController: UITableViewController {
 
    var detailViewController: DetailViewController? = nil
    let document = Document.sharedInstance
 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.leftBarButtonItem = self.editButtonItem
 
        let addButton = UIBarButtonItem(barButtonSystemItem: .add,
            target: self, action:
            #selector(MasterViewController.addNewCard(sender:)))
        self.navigationItem.rightBarButtonItem = addButton
        if let split = self.splitViewController {
            let controllers = split.viewControllers
            self.detailViewController
                = (controllers[controllers.count-1]
                as! UINavigationController).topViewController
                as? DetailViewController
        }
    }
    :

データをViewControllerのプロパティにしてしまうと、データ管理ができないので、自分は必ずDocumentクラスを用意するようにしている。

ソースコード
GitHubからどうぞ。

https://github.com/murakami/workbook/tree/master/ios/Hand - GitHub

関連情報
文化を調和させる

【Cocoa練習帳】
http://www.bitz.co.jp/weblog/

http://ameblo.jp/bitz/(ミラー・サイト)

2
2
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
2
2