xcode 您可以在没有按钮的情况下以编程方式关闭视图控制器吗?- OS X
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38126755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you programmatically close a view controller without a button? - OS X
提问by cheesydoritosandkale
I am trying to programmatically close a view controller in my Cocoa App. Is this possible? I saw a post about it in Objective-C, but I haven't been able to find one that works in Swift. I've tried this code inside a function, and then calling the function.self.dismissViewController(self)
However it gives me SIGABRT. I don't want the user to have to press a button or do anything, but just have the back controller to close once I "show" the next View Controller (I have a self pressing IBOutlet button that is automatically sending the view to the next page, but doesn't seem to be able to close the current page). Is there a working code for closing the Parent View Controller?
我正在尝试以编程方式关闭我的 Cocoa 应用程序中的视图控制器。这可能吗?我在 Objective-C 中看到了一篇关于它的帖子,但我一直没能找到一个适用于 Swift 的帖子。我已经在函数中尝试了这段代码,然后调用了该函数。self.dismissViewController(self)
但是它给了我 SIGABRT。我不希望用户必须按下按钮或做任何事情,但只要我“显示”下一个视图控制器就关闭后控制器(我有一个自动按下的 IBOutlet 按钮,它会自动将视图发送到下一页,但似乎无法关闭当前页面)。是否有关闭父视图控制器的工作代码?
回答by cheesydoritosandkale
So I figured out how to get this to work - thanks everyone for the suggestions. Here is what worked for me - I connected the button from the storyboard as both a Outlet (to click itself) and a function (to close itself) and I put the Opacity of the button to 0 so it isn't visible. I had to put it specifically like and add an NSTimer to click it like this because I needed it to programmatically open another view controller before it closes. Hope this helps someone in the future!
所以我想出了如何让它发挥作用 - 感谢大家的建议。这是对我有用的方法 - 我将故事板中的按钮连接为插座(单击自身)和函数(关闭自身),并将按钮的不透明度设置为 0,因此它不可见。我不得不特别喜欢并添加一个 NSTimer 来像这样单击它,因为我需要它在关闭之前以编程方式打开另一个视图控制器。希望这对未来的人有所帮助!
class Begin: NSViewController {
@IBOutlet var close: NSButton!
override func viewWillAppear() {
NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: #selector(click), userInfo: nil, repeats: true)
}
func click() {
close.performClick(nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func close(sender: AnyObject) {
self.view.window?.close()
}
}
回答by tunaki jento
if its a model vc the you can try [self dismissViewControllerAnimated:YES completion:nil]
; or you can open another vc [self dismissViewControllerAnimated:YES completion:nil];
如果它是一个模型 vc 你可以尝试[self dismissViewControllerAnimated:YES completion:nil]
;或者你可以打开另一个vc[self dismissViewControllerAnimated:YES completion:nil];
回答by Bruno Garelli
As you state it here, you may be using a navigationController, ?am I right?. If so, you can try navigationController.popViewControllerAnimated(animated: Bool)
正如您在此处声明的那样,您可能正在使用导航控制器,对吗?如果是这样,您可以尝试 navigationController.popViewControllerAnimated(animated: Bool)
Hope this can help you out, kind regards.
希望这能帮到你,亲切的问候。