gpt4 book ai didi

objective-c - 在 View Controller 和 OS X 之间转换

转载 作者:IT王子 更新时间:2023-10-29 05:07:24 26 4
gpt4 key购买 nike

我正在尝试编写一个单窗口计时器应用程序,当用户按下开始按钮时,我希望它显示另一个带有倒计时等的 View Controller 。我也在 Xcode 中使用 Storyboard,在那里我有一个 segue它连接开始按钮和第二个 View Controller 。但是,只有三种不同的样式,即模态、表单和弹出。我想将第一个 View Controller 替换为窗口中的第二个 View Controller 。我找不到办法做到这一点。我尝试为 segue 使用自定义样式,并在其中使用 presentViewController: animator: 方法,但我不知道要发送什么作为 animator: 的参数。

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

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

最佳答案

我认为最简单的方法是交换 NSWindowcontentViewController

// 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。

最后一个选项是使用 NSViewControllerpresentViewController:animator:。下面的代码是用于溶解动画的自定义 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 - 在 View Controller 和 OS X 之间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28454291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com