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

【メモ帳アプリ作成①】XIBファイルを使って画面遷移ができるようになったのでメモ

Last updated at Posted at 2025-02-08

メモ帳アプリをMain.storyboradとSenceDelegateを使わず作ろうとしています。
XIBを使った画面遷移がうまくいかなくて苦戦していましたが、
やっとエラーを潰せたので
メモ代わりにソースコードを載せようと思います。

①ViewController.swift
・メモの一覧表を載せます(未実装、後日追加する)
・ナビゲーションコントローラーに
   ・タイトル「メモ一覧」を表示
   ・+ボタン(Addボタン)を追加
・+ボタンを押したら画面遷移(メモの新規作成画面に移動)

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var Label: UILabel!
    //**テスト用に表示させてるだけ**//

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white  // 背景色を設定(確認用)

        // ナビゲーションバーのタイトルを設定
        title = "メモ一覧"

        // ナビゲーションバーに「+」ボタンを追加
        navigationItem.rightBarButtonItem = UIBarButtonItem(
            barButtonSystemItem: .add,
            target: self,
            action: #selector(addMemo)
        )
    }
    @objc func addMemo() {
        let addMemoVC = AddMemoViewController()
        navigationController?.pushViewController(addMemoVC, animated: true)
    }
  
}

②AddMemoViewController.swift(メモの新規作成画面)
・ViewController.swiftの+ボタンを押したらここに画面遷移

AddMemoViewController.swift
import UIKit

class AddMemoViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white // 背景色を設定(確認用)
    }
    
    init() {
        super.init(nibName: "AddMemoViewController", bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

まだ何の処理もできていませんが、画面遷移できたのが嬉しくてメモ代わりに
この記事を書きました。

あとはメモの詳細画面を表示させたり、
設定画面を表示させたりしたいので
今度は複数の画面を扱えるようにがんばります。
とりあえず1画面分、画面遷移できるようになった!

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