質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1417閲覧

Swift5 StoryBoardを用いないUIの実現

saku_panda

総合スコア20

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/10/23 04:16

編集2019/10/23 05:09

前提・実現したいこと

独学でswiftを学んでいます。
現在はswift5.0,iOS12.3想定で、storyboardを使わずにToDoリストの作成をしています。

https://qiita.com/nagisawks/items/222c881d6798c46a390f
https://qiita.com/mochizukikotaro/items/f053495eb130e92e13e8
https://qiita.com/TD3P/items/8f474358d1dd789557f3
上記三つの投稿を参考に構築しています。

UINavigationBarとUILabelの実装を試みましたが、
Thread 1: signal SIGABRTで落ちます。
MainStoryboardは削除済みですが、
併せてDeployment InterfaceでMain InterfaceのMain指定も削除しています。

発生している問題・エラーメッセージ

Thread 1: signal SIGABRT

該当のソースコード

Swift 5.0 ViewController import UIKit extension UIColor { static let normalText = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1) } extension UIFont { static let normalText = UIFont.systemFont(ofSize: 14) } class ViewController: UIViewController { let label = UILabel() let mainNav = UINavigationBar() let addNav = UINavigationBar() var addBarBtn: UIBarButtonItem! // let backBarBtn: UIBarButtonItem! override func viewDidLoad() { super.viewDidLoad() // Do any additioßnal setup after loading the view. view.backgroundColor = UIColor.white // 画面に名前をつける。ナビゲーションバーのtitleになる。バー内のボタンの設定 self.title = "Home" addBarBtn = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: Selector (("onClick"))) // barButtonSystemItemのコマンドhttps://developer.apple.com/documentation/uikit/uibarbuttonitem/systemitem self.navigationItem.rightBarButtonItem = addBarBtn label.text = "ToDoList" label.textColor = UIColor.normalText label.font = UIFont.normalText // viewにラベルを追加 // constraintsよりも前に追加する必要がある self.view.addSubview(label) // ラベルをトップから10px話した位置に固定 label.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true label.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } // addBtnをタップしたときのアクション func onClick() { let second = SecondViewController() self.navigationController?.pushViewController(second, animated: true) } }
Swift 5.0 AppDelegate import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? // 追加したナビゲーションコントローラー var myNavigationController: UINavigationController? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let first: ViewController = ViewController() // let second : SecondViewController = SecondViewController() myNavigationController = UINavigationController(rootViewController: first) self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.rootViewController = first // window?.rootViewController = ViewController() window?.makeKeyAndVisible() return true } // アプリがアクティブになる度に呼ばれる func applicationDidBecomeActive(application: UIApplication) { } // アプリ閉じそうな時 func applicationWillResignActive(_ application: UIApplication) { } // アプリを閉じた時 func applicationDidEnterBackground(_ application: UIApplication) { } // アプリを開きそうな時 func applicationWillEnterForeground(_ application: UIApplication) { } // アプリを開いた時 func applicationDidBecomeActive(_ application: UIApplication) { }

補足情報(FW/ツールのバージョンなど)

Swift 5.0
Xcode 11.0
iOS 12.3
MacOS Mojave 10.14.6

都合上、アップデートができません。
古いバージョンで申し訳ありませんが、ご助力いただけますと幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

試していませんが、

self.navigationItem.rightBarButtonItem = addBarBtn ```が問題ではないでしょうか。 > **navigationItem の Declaration より一部抜粋** you should not access this property if you are not using a navigation controller to display the view controller. Avoid tying the creation of bar button items in your navigation item to the creation of your view controller's view. The navigation item of a view controller may be retrieved independently of the view controller's view. (Google翻訳) Navigation Controllerを使用してView Controllerを表示していない場合は、このプロパティにアクセスしないでください。 ナビゲーションアイテムのバーボタンアイテムの作成をView Controllerのビューの作成に結び付けないでください。 View Controllerのナビゲーション項目は、View Controllerのビューとは無関係に取得できます。 AppDelegateで

let first: ViewController = ViewController()
・・・
self.window?.rootViewController = first

としているので、UINavigationControllerを使用(表示)していません。

投稿2019/10/23 08:23

dsuzuki

総合スコア1682

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

saku_panda

2019/10/24 00:59

dsuzukiさん なるほど、ありがとうございます。 試してみたところ、無事に動きました。 一次情報の読み込みが甘く、お恥ずかしいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問