简体   繁体   English

SWIFT:如何使用来自不同类的performSegueWithIdentifier

[英]SWIFT: how to use performSegueWithIdentifier from different class

I'm trying to make my App to automatically go back to the main menu after 2 minutes of inactivity. 我试图让我的应用在不活动2分钟后自动返回主菜单。 So far I have both parts working, but not together.. 到目前为止,我的两个部分都在工作,但没有一起工作。

The App starts a counter if there's no touch input: 如果没有触摸输入,则应用程序将启动计数器:

see user4806509's anwer on Detecting when an app is active or inactive through touches in Swift 请参阅user4806509关于通过Swift中的触摸检测应用是处于活动状态还是非活动状态的答案

And from my main viewcontroller I can control the segue I need with code: 从我的主viewcontroller中,我可以使用代码控制我需要的segue:

func goToMenu()
{
    performSegueWithIdentifier("backToMenu", sender: self)
}

I've implemented the code from Rob's answer on How to call performSegueWithIdentifier from xib? 我已经实现了Rob关于如何从xib调用performSegueWithIdentifier的答案中的代码

So I've created the following class and protocol: 因此,我创建了以下类和协议:

protocol CustomViewDelegate: class {
    func goToMenu()
}
class CustomView: UIView
{
weak var delegate: CustomViewDelegate?
    func go() {
        delegate?.goToMenu()
    }
}

The Function go() gets (successfully) called when the timer runs out. 计时器用尽时,函数go()被成功调用。 But the delegate?.goToMenu() doesn't work. 但是委托?.goToMenu()不起作用。 If I change it to: delegate!.goToMenu(), the App crashes with: 如果我将其更改为:委托!.goToMenu(),则应用程序崩溃并显示:

fatal error: unexpectedly found nil while unwrapping an Optional value

My main viewcontroller is a CustomViewDelegate and the viewDidLoad contains: 我的主要viewcontroller是CustomViewDelegate,并且viewDidLoad包含:

let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0] as! CustomView
myCustomView.delegate = self

I have the correct xib file, and that part is working. 我有正确的xib文件,并且该部分正在工作。

I can't seem to find the solution to this seemingly easy problem, does anyone have a fix? 我似乎找不到解决这个看似简单的问题的方法,有人解决了吗? Or better yet, a more elegant solution to my problem? 还是更好的解决我的问题的更好方法?

Thank you! 谢谢!

edit: SOLUTION: 编辑:解决方案:

I've removed all my old code and implemented the NSNotification method: 我删除了所有旧代码并实现了NSNotification方法:

In the UIApplication: 在UIApplication中:

let CallForUnwindSegue = "nl.timfi.unwind"

func delayedAction()
{
    NSNotificationCenter.defaultCenter().postNotificationName(CallForUnwindSegue, object: nil)
}

In the main ViewController: 在主ViewController中:

let CallForUnwindSegue = "nl.timfi.unwind"
func goToMenu(notification: NSNotification) 
{
    performSegueWithIdentifier("backToMenu", sender: self)
}
deinit 
{
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

In the viewDidLoad: NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PeriodViewController.goToMenu), name:CallForUnwindSegue , object: nil) 在viewDidLoad中: NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PeriodViewController.goToMenu), name:CallForUnwindSegue , object: nil)

I believe that your issue is that because your delegate is declared as a weak var, your delegate is getting disposed while you wait for the timer to complete, thus the reference to the optional is lost (and returns nil when you force unwrap it). 我相信您的问题是,因为您的委托被声明为弱变量,所以在等待计时器完成时您的委托正在被处决,因此对可选参数的引用会丢失(当您强制拆开它时返回nil)。

try removing the weak keyword from your CustomView class. 尝试从CustomView类中删除弱关键字。

Another solution would be to use the notification center to notify your view to call for the segue. 另一个解决方案是使用通知中心来通知您的视图以请求segue。

Something along the lines of what I proposed here 在这里提出的建议

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SWIFT-如何使用tableviewcell中的tableview中的performeguewithidentifier - SWIFT - How to use performseguewithidentifier from a tableview in tableviewcell 如何从模型类中使用performSegueWithIdentifier? - How to use performSegueWithIdentifier from a model class? Swift-不可能使用performSegueWithIdentifier - Swift - Impossible to use performSegueWithIdentifier 如何在Swift中调用performSegueWithIdentifier - How to call performSegueWithIdentifier in Swift 斯威夫特-从右到左表演eguewithidentifier过渡风格 - Swift - performseguewithidentifier transition style from right to left 如何从XIB调用performSegueWithIdentifier? - How to call performSegueWithIdentifier from xib? 执行函数以从另一个类调用performSegueWithIdentifier - Execute function to call performSegueWithIdentifier from another class 是否可以从另一个类调用performSegueWithIdentifier? - Is it possible to call a performSegueWithIdentifier from another class? 如何从视图的子视图的视图的自定义类中调用performSegueWithIdentifier? - How to call performSegueWithIdentifier from within a custom class of a view that's a subview of view controller? 如何在iOS Swift中使用不同类的选择器功能,主要用于NotificationCenter - How to use selector function from different class in iOS Swift, mainly for NotificationCenter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM