1

I'm still trying to get to grips with linking UIViewControllers etc in iOS . 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. However the destination UIViewController doesn't show and the app silently crashes - no errors at all.

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.

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.

psk
  • 162
  • 1
  • 13
RobertyBob
  • 803
  • 1
  • 8
  • 20
  • 2
    can you share your code and image of storyboard? – Ujjwal Khatri Feb 10 '16 at 11:37
  • Sry ujjwal - I don't have the authorisation to do that. At the moment I'd prefer a way to be able to see errors thrown when building controllers cos a silent crash is pretty impossible to start debugging – RobertyBob Feb 11 '16 at 13:03

2 Answers2

3

Checklists can be fixed by implementing protocols.. Check the WWDC following:

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)...

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). 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. i.e. in a UITabviewController (or NSStabViewController), you override their default behaviours with some you want to implement of your own in the overrides

Part 1) PrepareForSegue - Before it happens:

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'. 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. i.e. when your viewController calls in an action or function :

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...

Adrian Sluyters
  • 2,186
  • 1
  • 16
  • 21
  • Many thanks for the extensive answer, Adrian. As far as I could tell everything was getting to the segue ok but then failing. I've redirected the segue to a bare-bones ViewController and it worked ok so assume that means that there is something within the init phases of the destination controller that it doesn't like - so have a starting point at least. – RobertyBob Feb 10 '16 at 13:13
  • Have you checked that `(i) the from-ViewController has it's class set` the `(ii) to-viewController has it's class set too` and that the segue itself has it's class set... And that the segue has its identifier set too? – Adrian Sluyters Feb 10 '16 at 13:16
  • Alternatively zip it up and put it on github for me (and the other SE users to see) so we can see what's up :) – Adrian Sluyters Feb 10 '16 at 13:16
  • All 3 have classes set and correct. Is there a clever way to work out why a ViewController isn't loading? Or is it just a case of commenting out blocks until it shows? – RobertyBob Feb 10 '16 at 14:35
  • So commenting out isn't easy since there are about 40 IBOutlets, classes, delegates etc relating to the controller which cause the build to fail if code blocked - is there really no way to be able to see a physical log of the errors thrown when loading a controller? Thanks. – RobertyBob Feb 11 '16 at 12:30
  • Adrian - I've up-voted your answer as it will help a lot of people with their problems even tho in this case the error was caused by a bug in Xcode – RobertyBob Feb 12 '16 at 13:03
0

Ok so after looking high and low for errors in classes, delegates etc it turns out it's an error in Swift/XCode itself.

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. Any default text needs to be set in the viewDidLoad instead.

The full solution is contained here - iOS 9 Segue Causes App To Freeze (no crash or error thrown)

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.

Community
  • 1
  • 1
RobertyBob
  • 803
  • 1
  • 8
  • 20