简体   繁体   English

Swift:PrepareForSegue正在运行但没有加载ViewController

[英]Swift: PrepareForSegue running but not loading ViewController

I'm still trying to get to grips with linking UIViewControllers etc in iOS . 我仍然试图在iOS链接UIViewControllers等。 I've had a ridiculous number of issues with segues but that will be my fault for getting thrown in at the deep end with hardly any training at all! 我有一些荒谬的细胞问题,但这对我来说是完全没有任何训练就被抛到深处的错!

Anyway, I currently have a prepareForSegue that runs every line perfectly and then should head via a UINavigationController to the destination. 无论如何,我目前有一个prepareForSegue完美地运行每一行,然后应该通过UINavigationController到达目的地。 However the destination UIViewController doesn't show and the app silently crashes - no errors at all. 但是目标UIViewController没有显示,应用程序静默崩溃 - 根本没有错误。

Is there any way to get some kind of list of potential causes of such a crash based on experience? 有没有办法根据经验获得某种类似崩溃的潜在原因列表?

Things like 'check segue spelling' and 'do not perform segues in ViewDidLoad' have already been ruled out. 已经排除了“检查segue拼写”和“不在ViewDidLoad中执行segues”之类的事情。

I feel that checklists are a good way for novices like me to learn about pitfalls in coding but if it's impossible to answer or too many answers then let me know and I'll kill the question. 我觉得清单对于像我这样的新手来说是一个很好的方法来了解编码中的陷阱,但是如果它不可能回答或者答案太多那么请告诉我,我会解决这个问题。

Thanks. 谢谢。

Checklists can be fixed by implementing protocols.. Check the WWDC following: 可以通过实施协议来修复检查清单。检查以下WWDC:

https://developer.apple.com/videos/play/wwdc2015-408/ https://developer.apple.com/videos/play/wwdc2015-408/

Furthermore, in regards to segues, there's a few things to note (the same applies to OSX)... 此外,关于segues,有几点需要注意(同样适用于OSX)......

Note: 注意:

i) A segue, per definition is a transition between one one part to another (of a scene in a film or a part in music). i)根据定义,segue是一个部分到另一个部分(电影中的场景或音乐中的一部分)之间的过渡。 So they're meant to be a "go-between" when it comes to view controllers... 因此,当涉及到查看控制器时,它们应该是一种“中间人”......

 i.e. ViewController1 <-- segue ->> ViewController2

ii) When you subclass and override prepareForSegue you're telling your code that you want to handle the transition yourself. ii)当你prepareForSegue并覆盖prepareForSegue你告诉你的代码你想要自己处理转换。 ie in a UITabviewController (or NSStabViewController), you override their default behaviours with some you want to implement of your own in the overrides 即在UITabviewController(或NSStabViewController)中,您可以覆盖其中一些要在覆盖中实现自己的默认行为

Part 1) PrepareForSegue - Before it happens: 第1部分PrepareForSegue - 在它发生之前:

This is where you instantiate, configure, set up etc... your view-controllers.. If they're already set-up then you should otherwise be super-calling here. 这是你实例化,配置,设置等...你的视图控制器..如果他们已经设置,那么你应该在这里超级调用。

//i.e.   super.PrepareSegueForIdentifier(identifier)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "something" {
        // Do your thing here
        // MARK: IFYES
    } else {
        prepareForSegue(segue, sender: sender)
    }
}

If you have pre-preparation work for your "to be" view controller, then set it up in the first part of the "if" statement which I marked 'IFYES'. 如果你有“待定”视图控制器的预备工作,那么在我标记为“IFYES”的“if”语句的第一部分中进行设置。 You set up things like the animations, from and to colors, sizes etc... 你可以设置动画,颜色,尺寸等等......

After this has been done, then there's another method: 完成此操作后,还有另一种方法:

performSegueWithIdentifier:

And an example I use is: 我使用的一个例子是:

override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
    if (identifier == "yourIdentifier") {
        Do something here
    } else {
        super.performSegueWithIdentifier(identifier, sender: sender)
    }
}

This is when the segue, with the identifier of yourIdentifier actually does something. 这是具有yourIdentifier标识符的yourIdentifier实际执行的操作。 ie when your viewController calls in an action or function : 即当您的viewController调用一个动作或函数时:

func someFunction(){
    self.performSegueWithIdentifier("yourIdentifier", sender: senderObjectName)
}

This is of course if you've made sure that your segue's between controllers etc... have the correct identifiers and types... 当然,如果您确保控制器之间的segue ...具有正确的标识符和类型......

Ok so after looking high and low for errors in classes, delegates etc it turns out it's an error in Swift/XCode itself. 好吧,看看高级和低级的类,代表等错误后发现它是Swift / XCode本身的错误。

The culprit is simply the TextView on the page which needs to have its default text set to blank in order for the page to load. 罪魁祸首就是页面上的TextView,它需要将其默认文本设置为空白才能加载页面。 Any default text needs to be set in the viewDidLoad instead. 需要在viewDidLoad中设置任何默认文本。

The full solution is contained here - iOS 9 Segue Causes App To Freeze (no crash or error thrown) 完整的解决方案包含在这里 - iOS 9 Segue导致应用程序冻结(没有崩溃或错误抛出)

It would still be good to know if there is any way to see a log of errors thrown during view init but in this case it all comes down to an insignificant element on the view. 知道是否有任何方法可以查看在视图init期间抛出的错误日志仍然是一件好事,但在这种情况下,这一切都归结为视图中的一个无关紧要的元素。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM