我正在尝试编写一个单一的窗口计时器应用程序,在该应用程序中,当用户按下开始按钮时,我希望它显示带有倒数的另一个 View Controller 等。开始按钮和第二个 View Controller 。但是,只有三种不同的样式,即模式样式,工作表样式和 pop 样式。我想将第一个 View Controller 替换为窗口中的第二个 View Controller 。我找不到办法。我尝试为segue使用自定义样式,在该样式中使用presentViewController:animator:方法,但我无法弄清楚要发送的内容作为animator:的参数。

从一个 View Controller 在一个窗口中转换到另一个 View Controller ,反之亦然的最简单/正确的方法是什么?

同样在 Storyboard 中,当我选择一个 View Controller 时,它会显示一个名为“Presentation”的属性,该属性可以是多个和单个,它们代表什么?

最佳答案

我认为最简单的方法是将contentViewController交换为NSWindow

// in NSViewController's subclass
@IBAction func someAction(sender: AnyObject) {
    let nextViewController = ... // instantiate from storyboard or elsewhere

    if let window = view.window where window.styleMask & NSFullScreenWindowMask > 0 {
        // adjust view size to current window
        nextViewController.view.frame = CGRectMake(0, 0, window.frame.width, window.frame.height)
    }

    view.window?.contentViewController = nextViewController
}

这是选项1。

如果要使用segue,请创建自定义选项,并将其设置为IB中带有标识符的segue类。
class ReplaceSegue: NSStoryboardSegue {
    override func perform() {
        if let fromViewController = sourceController as? NSViewController {
            if let toViewController = destinationController as? NSViewController {
                // no animation.
                fromViewController.view.window?.contentViewController = toViewController
            }
        }
    }
}

这是选项2。

最后一个选择是使用presentViewController:animator:NSViewController。下面的代码是用于溶解动画的自定义NSViewControllerPresentationAnimator
class ReplacePresentationAnimator: NSObject, NSViewControllerPresentationAnimator {
    func animatePresentationOfViewController(viewController: NSViewController, fromViewController: NSViewController) {
        if let window = fromViewController.view.window {
            NSAnimationContext.runAnimationGroup({ (context) -> Void in
                fromViewController.view.animator().alphaValue = 0
            }, completionHandler: { () -> Void in
                viewController.view.alphaValue = 0
                window.contentViewController = viewController
                viewController.view.animator().alphaValue = 1.0
            })
        }
    }

    func animateDismissalOfViewController(viewController: NSViewController, fromViewController: NSViewController) {
        if let window = viewController.view.window {
            NSAnimationContext.runAnimationGroup({ (context) -> Void in
                viewController.view.animator().alphaValue = 0
                }, completionHandler: { () -> Void in
                    fromViewController.view.alphaValue = 0
                    window.contentViewController = fromViewController
                    fromViewController.view.animator().alphaValue = 1.0
            })
        }
    }
}

然后像这样呈现VC。
@IBAction func replaceAction(sender: AnyObject) {
    let nextViewController = ... // instantiate from storyboard or elsewhere
    presentViewController(nextViewController, animator: ReplacePresentationAnimator())
}

要解雇,请在显示的VC中调用presentingViewControllerdismissViewController:
@IBAction func dismissAction(sender: AnyObject) {
    presentingViewController?.dismissViewController(self)
}

Swift4版本
class ReplacePresentationAnimator: NSObject, NSViewControllerPresentationAnimator {
func animatePresentation(of viewController: NSViewController, from fromViewController: NSViewController) {
    if let window = fromViewController.view.window {
        NSAnimationContext.runAnimationGroup({ (context) -> Void in
            fromViewController.view.animator().alphaValue = 0
        }, completionHandler: { () -> Void in
            viewController.view.alphaValue = 0
            window.contentViewController = viewController
            viewController.view.animator().alphaValue = 1.0
        })
    }
}

func animateDismissal(of viewController: NSViewController, from fromViewController: NSViewController) {
    if let window = viewController.view.window {
        NSAnimationContext.runAnimationGroup({ (context) -> Void in
            viewController.view.animator().alphaValue = 0
        }, completionHandler: { () -> Void in
            fromViewController.view.alphaValue = 0
            window.contentViewController = fromViewController
            fromViewController.view.animator().alphaValue = 1.0
        })
    }
}

}

希望对您有所帮助。

关于objective-c - 在OS X的 View Controller 之间过渡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28454291/

10-09 01:26