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

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

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

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

Q&A

解決済

2回答

5593閲覧

swiftのNavigationControllerについて

tamago0224

総合スコア71

Swift

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

1グッド

0クリップ

投稿2016/02/12 03:15

編集2016/02/12 07:58

swiftにおいて、以下のサイトを参考にSKViewを設定しました。
参考サイト

ここで見ていただきたいのは以下に記載したコードのviewWillAppear関数です。
AppDelegateでrootViewControllerをNavigationControllerに変更したのですが、そうすると

let skView = self.view as! SKView

の行でThread 1: SGBRTのエラーが発生します。
NavigationControllerのままこのエラーを解決する方法はありますか?
ここで、GameSceneクラスはSKSceneを継承したクラスです。

swift

1import UIKit 2import SpriteKit 3 4class ViewController: UIViewController { 5 6 override func viewWillAppear(animated: Bool) { 7 let skView = self.view as! SKView 8 9 let scene = GameScene(size: skView.bounds.size) 10 11 skView.presentScene(scene) 12 } 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view, typically from a nib. 17 } 18 19 override func didReceiveMemoryWarning() { 20 super.didReceiveMemoryWarning() 21 // Dispose of any resources that can be recreated. 22 } 23} 24 25

追記
AppDelegate.swiftコードの追加

import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. //cconfigure ViewController class instance. let viewController: ViewController = ViewController() //configure NavigationController class let myNavigationController = UINavigationController(rootViewController: viewController) //set myNavigationController to rootViewController self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window?.rootViewController = myNavigationController self.window?.makeKeyAndVisible() return true } func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }
ikuwow👍を押しています

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

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

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

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

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

YasuhiroMiyake

2016/02/12 04:41

エラーの発生原因については特定出来ているのでしょうか。 self.view as! SKView キャストしているコードですが、self.view は間違いなくSKView型なのでしょうか。 もう少し確認されたことも含めて記載したほうが良いですよ。
tamago0224

2016/02/12 05:04

原因がわからないので質問しているのですが? とりあえず他に原因究明に役立ちそうなものとしてエラー発生とともに 「Could not cast value of type 'UIView' (0x109724b20) to 'SKView' (0x10897ead0).」という文もデバッグエリアに表示されました。
jollyjoester

2016/02/12 05:33

rootViewControllerを変更している部分も掲載していただけますでしょうか?
AOKINAO

2016/02/12 06:03 編集

YasuhiroMiyakeさんが指摘しているのは、self.viewがSKView型となっているかどうかです。プログラムやエラーメッセージを見る限り、self.viewはSKView(またはそのサブクラス)ではありません。NavigationViewが必要でしたら、NavigationViewの中にSKViewを追加してみてはどうでしょうか。
YasuhiroMiyake

2016/02/12 09:19

AOKINAOさん、補足頂きありがとうございます。 ここは大人の対応? ということでスルーしたいと思います。 何かを書くと説教ぽくなってしまいますので。
guest

回答2

0

ベストアンサー

参考サイトの完成版コードを見るとStoryboardでViewControllerのベースとなるViewのクラスにSKViewが指定されています。なのでViewController.swiftではself.viewがSKViewであることを前提に書かれています。
イメージ説明

解決するにはfuzzballさんの書かれたようにロードの時点でself.viewをSKViewのviewに差し替えてしまう方法でもいいと思いますし、storyboardの設定を活かすのであれば下記の方法もアリかと思います。

①storyboardにIDをふって
イメージ説明

②AppDelegateでViewControllerをStoryboardから作る

Swift

1 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 2 3 //let viewController: ViewController = ViewController() 4 5 let storyboard = UIStoryboard(name: "Main", bundle: nil) 6 let viewController: ViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController 7 8 //configure NavigationController class 9 let myNavigationController = UINavigationController(rootViewController: viewController) 10 11 //set myNavigationController to rootViewController 12 self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 13 self.window?.rootViewController = myNavigationController 14 self.window?.makeKeyAndVisible() 15 16 return true 17 }

投稿2016/02/12 10:15

jollyjoester

総合スコア1585

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

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

tamago0224

2016/02/12 11:15

ご指摘の通りコードを変更したら正常に動きました。 先ほど、UIStoryboardのリファレンスを見て今回使用された関数の役割を見てきました。 インスタンス、storyboardはMain.storyboardのインスタンスを生成し、instantiateViewControllerWithIdentifier(identifier:)で指定したボードのインスタンスを生成するとのことでした。これをViewControllerのインスタンスに代入して.....。 なぜこれで先ほどのエラーが消えたのでしょうか? さっきまではviewControllerとmain.storyboardのviewControllerが正しく繋がっていなかったためでしょうか? しつこくて申し訳ないですが、時間があれば回答お願いします。
jollyjoester

2016/02/12 13:50

接続は関係ないです。 もともとのコードだと下記のようにViewControllerを直接生成していました。 > let viewController: ViewController = ViewController() これだとViewController中でself.viewにあたるものはデフォルトでUIViewクラスのviewが入っています。 UIViewクラスはSKViewクラスとは異なるものなので下記のようにキャストをしても失敗するのでエラーとなります。 > let skView = self.view as! SKView Storyboardでself.viewにあたるものにSKViewが入るように設定した後、storyboardからviewControllerを生成するとその設定を引き継いだViewControllerのインスタンスが取得できます。その場合、self.viewにはSKViewのviewが入っているので上記のキャストがうまくいきます。
tamago0224

2016/02/12 13:59

jollyjoesterさんわかりやすい解説ありがとうございます。 そして、fuzzballさん、YasuhiroMiyakeさん、AOKINAOさんも回答ありがとうございます。
guest

0

ViewCotrollerに、

swift

1override func loadView() { 2 self.view = SKView() 3}

を追加したらどうでしょうか?

【追記】
Storyboardを使うなら、NavigationControllerもStoryboard上に配置すればいいんじゃないかな?

  1. StoryboardにNavigationControllerを配置。
  2. NavigationControllerのIs Initial ViewControllerにチェックを入れる。
  3. NavigationControllerのrootViewControllerにViewControllerをつなぐ。

以上

AppDelegateにコードを書く必要もないし、ViewControllerは元のままで手を加えなくていいし。

投稿2016/02/12 08:56

編集2016/02/12 17:03
fuzzball

総合スコア16731

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

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

fuzzball

2016/02/12 17:04

遅まきながら、Storyboardを使う場合の追記をしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問