Closed rtamesis closed 11 years ago
Just to be sure that this wasn't a fluke, I built another project for the iPad, imported all your files (except for the SecondViewController.h and .m files) then wired everything up, using a regular viewcontroller scene for the first viewcontroller (which contains a textfield and button) and a tableview controller scene for the second viewcontroller. I created a subclass of UITableViewController and named it SecondViewController and assigned it to the tableview controller scene. Again, I'm seeing the same behavior where the first viewcontroller appears blank but then comes on after pressing the swap button twice. If you'd like, I can email you the project so that you can take a look at it. The only other modification I made was adding the QuartzCore framework and making a UIView subclass for the container in order to put a shadow around it. I did determine that this wasn't the cause of the problem because the problem is still present even without these files and the framework.
I had the gnawing feeling that the tableview controller was somehow messing things up, so I duplicated the project and basically switched the two view controllers around so that the tableview controller was the first viewcontroller and the regular view controller with the button and text field was second, and it worked just fine. Weird.
On further testing, it looks like the problem may actually be in the storyboard for the ViewController class and not in the code. I've been using the storyboard that comes ready made when building a single view app as my view controller to hold the container view, so I'm going to try deleting that and starting with a brand new one from the object library and see if that solves the problem. Update: Deleting the initial view controller and creating a brand new one did not work. Right now I'm fresh out of ideas as to why the first viewcontroller does not display its controls initially but does so after being switched with the tableview controller.
Ok, I found a possible culprit. If I turn off Autolayout, then the controls in the first view controller show up on launch, and when I turn it back on, they don't. I'll have to explore this further and see if pinning the textfield and button in place with constraints will fix the problem. Update: It's official. All my problems are the result of Autolayout freakiness. Careful positioning of the controls solves the problems.
It's official. All my problems are the result of Autolayout freakiness and not because of a problem with the code. Careful positioning of the controls eventually reveals them and solves the problem of them appearing in place initially, although I still wonder why swapping the viewcontrollers around fixes the display problems as I previously described.
Glad you got it figured out. Thanks for sharing your knowledge.
For what it's worth, I think the reason it happens only first time through is because of this line:
((UIViewController *)segue.destinationViewController).view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
The container view controller is created first time around before the autolayout pass.
(The solution for this is to move performSegueWithIdentifier: to viewDidAppear:)
I experimented further by adding a textfield and a button to the FirstViewController and making the SecondViewController a table view controller on the iPad. When the application launches, the container view is initially blank instead of displaying the first view controller properly. After pressing the swap button, the second viewcontroller with its tableview displays properly, and when the swap button is pressed again, the first view controller displays properly. Any ideas on how to fix the initially blank first view controller?