2014-06-10 3 views
3

비 문서 기반 응용 프로그램 인 Swift에서 NSWindowRestoration 프로토콜을 구현하려고했습니다. 그러나 응용 프로그램 시작시 restoreWindowWithIdentifier 메서드는 호출되지 않습니다. 누군가 내 실수를 지적 할 수 있습니까? 사전에Swift에서 NSWindowRestoration을 구현하는 방법은 무엇입니까?

class AppDelegate: NSObject, NSApplicationDelegate, NSWindowRestoration { 

    var windowController : MyWindowController? 

    func applicationDidFinishLaunching(aNotification: NSNotification?) { 
    windowController = MyWindowController(windowNibName:"ImageSequenceView") 
    } 

    class func restoreWindowWithIdentifier(identifier: String!, state: NSCoder!, completionHandler: ((NSWindow!,NSError!) -> Void)!) { 
    NSLog("restoreWindowWithIdentifier: \(identifier), state: \(state)") 
    } 

} 

class MyWindowController: NSWindowController { 

    override func windowDidLoad() { 
    super.windowDidLoad(); 
    window.restorationClass = AppDelegate.self 
    } 
} 

감사 : 여기

는 (컴파일하고 잘 실행) 코드의 부분 집합이다!

+0

호기심에서 벗어나서,'super.windowDidLoad()'의 순서를 바꾸고'window.restorationClass'를 설정하면 어떻게 될까요? – Erik

+0

죄송합니다. 그냥 시도했지만 차이를 만들지 않습니다 ... –

+0

'self.dynamicType'을 사용할 때 내 시스템 환경 설정이 창을 복원하도록 설정되어 있는지 확인하십시오. 내 클래스의'restoreWindowWithIdentifier'가 호출됩니다. –

답변

1

당신은 복원 클래스와 또한 식별자를 설정해야합니다 : 당신이 시도처럼 당신은 또한, 다른 클래스가 창을 복원 할 수 있습니다 물론

class MyWindowController: NSWindowController { 
    override func windowDidLoad() { 
     super.windowDidLoad() 

     self.window?.restorationClass = type(of: self) 
     self.window?.identifier = "MyWindow" 
    } 
} 

extension MyWindowController: NSWindowRestoration { 
    static func restoreWindow(withIdentifier identifier: String, state: NSCoder, completionHandler: @escaping (NSWindow?, Error?) -> Void) { 
     if identifier == "MyWindow" { 
      // Restore the window here 
     } 
    } 
} 

. 이 경우 restorationClass으로 AppDelegate.self을 할당해야합니다.

또한 어리석은 이유가 무엇이든 window restoration setting now defaults to "off"입니다.

+0

나를 위해 작동합니다. 허용 된 대답이어야합니다. – Klaas

관련 문제