4

I have a show segue called showPage from a view controller to a table view controller and am calling performSegueWithIdentifier() to get it to show after clicking OK on a button in an alert:

@IBAction func enterManuallyTap(sender: AnyObject) {
    var code : String!
    if #available(iOS 8.0, *) {
        let alert = UIAlertController(title: "Enter code", message: "Please enter code", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addTextFieldWithConfigurationHandler({ (input:UITextField) in
            input.placeholder = "your code"
        });
        alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (_) in
            barcode = alert.textFields!.first?.text
            self.performSegueWithIdentifier("showPage", sender: self)
        }))
        presentViewController(alert, animated: true, completion: nil)
    } else {
        let alert = UIAlertView(title: "Enter code", message: "Please enter code", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")
        alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
        alert.show()
    }
}

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    if buttonIndex == 1 {
        var code : String!
        code = alertView.textFieldAtIndex(0)?.text
        self.performSegueWithIdentifier("showPage", sender: self)
    }
}

This all works perfectly on my iOS 9 device, but when I try it on an iOS 7 device, the navigation bar disappears, so I can't see the title or click 'back'. The table view therefore starts at the very top of the screen and runs into the status bar.

I have tried changing the top bar from inferred to Translucent Navigation Bar and set nagivationBarHidden to false

    self.navigationController?.navigationBarHidden = false

but it still doesn't appear on iOS 7.

I just tried print(self.navigationController) in viewDidAppear() for the tableviewcontroller, and on my iOS 7 device, it prints nil, but on my iOS 9 device, it shows the controller: Optional(<UINavigationController: 0x12504e600>)! Why does it not exist on the older device? I've seen that the navigation controller won't exist if you have a modal segue, but I'm doing a normal show segue, and I can't understand why it should work on one device but not the other.

Why does this happen, and why only on iOS 7 (or a smaller size device)?

Could this be because the iOS 9 device I'm using has a larger screen? (It's an iPhone 6S) but the iOS 7 device is an iPhone 4, which has a smaller screen. How do I go about debugging this to check whether it is a matter of screen size? However, none of the cells in the table view controller the segue goes to are cut off in any way on either device.

My storyboard (click for bigger image):

enter image description here

The problematic segue is the one between the 'Scan' view controller and the choose scan view controller.

ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38

1 Answers1

2

I think the problem is with your showSegue (showPage)

From apple developer

Show (Push)

This segue displays the new content using the showViewController:sender: method of the target view controller. For most view controllers, this segue presents the new content modally over the source view controller. Some view controllers specifically override the method and use it to implement different behaviors. For example, a navigation controller pushes the new view controller onto its navigation stack.

Make sure your first viewController is embedded into a UINavigationController in Storyboard. Make the navigationController initialViewController if your first viewController is the initialViewController. Or else it could present the new viewController modally. That means no navigationBar on top of the new viewController. I think this is happening in your case.

You can also try delete and recreate a new segue. See this:
'Show' segue in Xcode 6 presents the viewcontroller as a modal in iOS 7

Community
  • 1
  • 1
Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
  • 1
    Thanks! But unfortunately the linked question's solution didn't work (my XML is already valid it seems, but I tried deleting and readding the segue but nothing happened anyway). However [this answer](http://stackoverflow.com/a/26665626/3541881) led to [this one](http://stackoverflow.com/a/27713843/3541881) and just adding an entirely new navigation controller from the problematic viewcontroller worked!!! – ᔕᖺᘎᕊ Jan 15 '17 at 12:16
  • I'll be giving you the bounty anyway :) but do you have any idea why it works on newer devices without the extra navigation controller? Again, it would be great if you could edit in the link to that last answer so it serves as a compete reference in case someone else finds this question in the future! :) – ᔕᖺᘎᕊ Jan 18 '17 at 16:17
  • Thanks @ᔕᖺᘎᕊ. May be in iOS 8 transition is inferred at runtime but on iOS 7 it is decided much earlier. In iOS 7 xcode loads `initialViewController`. It does not show 'NavigationBar' if it is not 'embedded` in a `navigationController` from `storyboard`. However I do not completely agree with that answer link. By embedded into a `navigationController` I wanted to mean adding a new `navigationController` (as you are doing). I agree with that part. But I'm also saying to make the new 'navigationController' the 'initialViewController' (a check box in xcode). I am unsure. Go ahead edit my answer. – Warif Akhand Rishi Jan 18 '17 at 17:47
  • This is how you embed: goto `storyBoard` select the `viewController`. Goto (menu) Editor >> Embed In >> Navigation Controller. I think this is the correct way. – Warif Akhand Rishi Jan 18 '17 at 17:50
  • 1
    Thanks, I did embed the second navigation controller that way, but I didn't try making that the initial view controller because I assumed that would break the entry point because it's not linked to my page view controller. I could try that though. And thanks for the explanation, that makes sense :) – ᔕᖺᘎᕊ Jan 18 '17 at 17:52