2014-12-22 17 views
5

Tôi đang cố tạo màn hình tạm dừng trên trò chơi của mình. Tôi đã thêm một 'PauseScreen' viewController trong kịch bản của tôi với Storyboard ID và ID Phục hồi thiết lập như là "PauseScreenID" và để chuyển sang màn hình tạm dừng tôi đã đã tạo ra các chức năng trong "GameScene":Thêm chế độ xem vào hệ thống phân cấp cửa sổ

func pauseSceenTransition(){ 

    let viewController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("PauseScreenID") as UIViewController 

    let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate) 

    currentViewController.window?.rootViewController?.presentViewController(viewController, animated: false, completion: nil) 
} 

Tuy nhiên khi nó được gọi, tôi nhận được lỗi:

Cảnh báo: Cố gắng để trình bày < AppName .PauseScreen: 0x7fae61fe5ff0> trên < AppName .StartScreenViewController: 0x7fae61f79980> mà xem không có trong hệ thống phân cấp cửa sổ!

"StartScreenViewController" là bộ điều khiển chế độ xem cho màn hình bắt đầu của tôi và là bộ điều khiển chế độ xem ban đầu. Sau đó nó đi đến "GameScene" là nơi mà "PauseScreen" cần phải đi. Nó hoạt động nếu tôi làm cho bộ điều khiển xem ban đầu "GameViewController"/"GameScene" vì vậy tôi đoán rằng tôi cần phải thay đổi dòng thứ hai:

let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate) 

để nó được trình bày "PauseScreen" trên "GameViewController" , không phải trên "StartScreenViewController" nhưng tôi không chắc chắn làm thế nào để làm điều đó.

Trả lời

11

Lỗi này cho bạn biết chính xác những gì đang xảy ra.

Warning: Attempt to present <AppName.PauseScreen: 0x7fae61fe5ff0> on <AppName.StartScreenViewController: 0x7fae61f79980> whose view is not in the window hierarchy!

(UIApplication.sharedApplication().delegate as AppDelegate).window?.rootViewController được trỏ đến một thể hiện của StartScreenViewController. Điều này là xấu: rootViewController nên trỏ đến một phiên bản của GameScene.

Nguyên nhân gốc phải là cách hiển thị GameScene. Từ mô tả của bạn:

Các "StartScreenViewController" là bộ điều khiển xem ... Sau đó nó đi vào "GameScene" ...

này phải là nơi vấn đề của bạn là. Làm thế nào để bạn truy cập vào GameScene từ StartScreenViewController?

Tôi đoán là bạn đang thêm cửa sổ mới vào ứng dụng. Thay vào đó, bạn cần đặt số rootViewController.

let gameScene = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("GameSceneID") as UIViewController 
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) 
appDelegate.window?.rootViewController = gameScene 

Khi bạn quay lại màn hình bắt đầu, bạn lại đặt rootViewController.

let initialViewController = UIStoryboard(name: "Main", bundle:nil).instantiateInitialViewController() as UIViewController 
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) 
appDelegate.window?.rootViewController = initialViewController 

Bạn có thể sử dụng transitionFromViewController(,toViewController:, duration:, options:, animations:, completion:) animate thiết lập bộ điều khiển xem gốc.

+0

Xin chào, Tôi đang gặp sự cố với vấn đề tương tự này ngay bây giờ và tôi tò mò muốn biết liệu các giải pháp đã thay đổi trong một năm rưỡi hay chưa. Tôi đang cố gắng gọi một 'UIAlertController' từ bên trong một' if' lồng nhau bên trong một nút. Các câu lệnh 'if' thông thường trong nút hiển thị kết quả' UIAlertView', nhưng kết quả 'true' của các câu lệnh if sẽ ném cùng một lỗi ở trên. – Ethan

+0

@Ethan Đôi khi bạn cần tạo một cửa sổ để đặt 'UIAlertController' vào: http: // stackoverflow.com/questions/26554894/how-to-present-uialertcontroller-khi-không-trong-một-view-controller –

+0

Bất cứ điều gì bạn biết về điều đó được viết nhanh chóng? – Ethan

Các vấn đề liên quan