storyboardを使わない設定と、SceneDelegate導入への対応について
storyboardを使わずに、NavigationControllerとTableViewを使って、シンプルな画面遷移を構築したい。
発生している問題・エラーメッセージ
Main.storyboardをゴミ箱に送り、infoからもMainを消したが、"could not find storyboard"のエラーメッセージがでる。また、SceneDelegateの導入により、AppDelegateに書いた処理が実行されない(?)とのことなのでqiitaを参考にコードを追加したが、エラーが直らなかった。
エラーメッセージ Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle NSBundle ### 該当のソースコード //SceneDelegate.swift import UIKit @available(iOS 13.0, *) class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). guard let _ = (scene as? UIWindowScene) else { return } let myFirstViewController: ViewController = ViewController() let nc = UINavigationController(rootViewController: myFirstViewController) let window = UIWindow(windowScene: scene as! UIWindowScene) self.window = UIWindow(frame:UIScreen.main.bounds) self.window = window self.window?.rootViewController = nc window.makeKeyAndVisible() } //遷移前の画面(ViewController.swift) import UIKit class ViewController: UIViewController { let mySections = ["",""] let Settings = [["一般","コントロールセンター","画面表示","壁紙","siri","touch ID"], ["ITunes","Wallet","パスワード"] ] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. var tableView: UITableView = UITableView() tableView = UITableView(frame:CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height),style: .grouped) tableView.delegate = self tableView.dataSource = self self.view.addSubview(tableView) } } extension ViewController: UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return Settings[section].count } func numberOfSections(in tableView: UITableView) -> Int { return mySections.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return mySections[section] } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: UITableViewCell = UITableViewCell(style: .default, reuseIdentifier: "cell") cell.textLabel!.text = Settings[indexPath.section][indexPath.row] return cell } } extension ViewController: UITableViewDelegate{ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("(indexPath.section+1)番目のセクション、(indexPath.row+1)番目の行が選択されました。") tableView.deselectRow(at: indexPath, animated: true) switch indexPath.section { case 0: switch indexPath.row { case 0: let secondViewController = generalViewController() self.navigationController?.pushViewController(secondViewController, animated: true) break default: break } default: break } } } //遷移先画面(genaralViewController.swift) import Foundation import UIKit let mySections2 = ["","",""] let General = [["情報","ソフトウェア・アップデート"], ["AirDrop","Handoff","Carplay"], ["ホームボタン"] ] class generalViewController: UIViewController { var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView = UITableView(frame: CGRect(x: 0,y: 0,width: self.view.bounds.width, height: self.view.bounds.height),style: .grouped) tableView.dataSource = self self.view.addSubview(tableView) } } extension generalViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return General[section].count } func numberOfSections(in tableView: UITableView) -> Int { return mySections2.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return mySections2[section] } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: UITableViewCell = UITableViewCell(style: .default, reuseIdentifier: "cell") cell.textLabel?.text = General[indexPath.section ][indexPath.row] return cell } } // AppDelegate.swift import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. if #available(iOS 13, *){ }else{ let myFirstViewController: ViewController = ViewController() let vc = UINavigationController(rootViewController: myFirstViewController) self.window = UIWindow(frame:UIScreen.main.bounds) self.window?.rootViewController = vc self.window?.makeKeyAndVisible() } return true } // MARK: UISceneSession Lifecycle @available(iOS 13.0, *) func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { // Called when the user discards a scene session. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } } ```ここに言語名を入力 swift ### 試したこと https://qiita.com/omochimetaru/items/31df103ef98a9d84ae6b ・こちらの「以前のiOSのサポート」の部分 ・infoのMain消し ・Main.storyboardをMove to trush ### 補足情報(FW/ツールのバージョンなど) simulatorはIphone 11 Pro Max
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/03 01:51
2019/10/03 02:11
2019/10/03 02:19
2019/10/03 02:23
2019/10/03 02:33
2019/10/03 02:45
2019/10/03 03:31