1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MVCについて

Last updated at Posted at 2021-05-20

・Model

Modelでは、データ処理、ビジネスロジック、データの更新をViewControllerへ通知するなどの役割を担う

・View

Viewでは、表示やユーザーからの入力や出力を行う

・Controller

Controllerでは、ユーザーからの入力によってModelとViewの仲立ちを行う

Model

```swift struct CountModel { var count: Int = 0 } ```

view

```swift final class ThirdTableViewCell: UITableViewCell {
@IBOutlet private weak var countLabel: UILabel!

override func prepareForReuse() {
    super.prepareForReuse()
    
    countLabel.text = nil
}

func configure(countModel: Int) {
    countLabel.text = countModel.description
}

}


<h2>Controller</h2>
```swift
import UIKit

final class InitialViewController: UIViewController {
    
    static func makeFromStoryboard() -> InitialViewController {
        let vc = UIStoryboard.initialViewController
        return vc
    }
    
    
    @IBAction private func showSecondButton(_ sender: Any) {
        Router.showSecond(vc: self)
    }
}



import UIKit

final class SecondViewController: UIViewController {
    
    var countModel = CountModel.init(count: 0)
    
    @IBOutlet private weak var countLabel: UILabel! {
        didSet{
            countLabel.text = countModel.count.description
        }
    }
    
    static func makeFromStoryboard() -> SecondViewController {
        let vc = UIStoryboard.secondViewController
        return vc
    }
    
    @IBAction private func countUpButton(_ sender: Any) {
        print("カウントされました")
        countModel.count += 1
        countLabel.text = countModel.count.description
    }
    
    @IBAction private func showThirdButton(_ sender: Any) {
        Router.showThird(vc: self, countModel: countModel)
    }
    
}

import UIKit

final class ThirdViewController: UIViewController {
    
    
    private let CELL_NIB_NAME = "ThirdTableViewCell"
    private let CELL_ID = "ThirdTableViewCell"
    
    var countModel: [CountModel] = []
    
    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            let cellNIB = UINib(nibName: CELL_NIB_NAME, bundle: nil)
            tableView.register(cellNIB, forCellReuseIdentifier: CELL_ID)
            
            tableView.isHidden = true
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.isHidden = false
    }
    
    static func makeFromStoryboard(countModel: CountModel) -> ThirdViewController {
        let vc = UIStoryboard.thirdViewController
        vc.countModel.append(countModel)
        return vc
    }
    
}

extension ThirdViewController: UITableViewDelegate {
    
}

extension ThirdViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard
            let cell = tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath) as? ThirdTableViewCell
        else {
            
            return UITableViewCell()
        }
        
    
        let count = countModel[indexPath.row].count
        
        cell.configure(countModel: count)
        
        
        return cell
    }
    
    
}


Extension

```swift

import UIKit

// Storyboardの読み込みを1箇所にまとめる
extension UIStoryboard {
static var initialViewController: InitialViewController {
UIStoryboard.init(name: "Initial", bundle: nil).instantiateInitialViewController() as! InitialViewController
}

static var secondViewController: SecondViewController {
UIStoryboard.init(name: "Second", bundle: nil).instantiateInitialViewController() as! SecondViewController
}

static var thirdViewController: ThirdViewController {
UIStoryboard.init(name: "Third", bundle: nil).instantiateInitialViewController() as! ThirdViewController
}
}

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?