How to connect iPad and iPhone UI XIB elements to same UIViewController - ios

this may be a noob question, but I'm new to iOS programming and I didn't find an answer to my question elsewhere...
Following issue: I am programming a universal app for iPad and iPhone using IB and storyboards. The app is already set up correctly and I have an iPhone and iPad storyboard in my project and both are connected to the right (same) view controller as owner.
So far, so good...
My iPhone app is close to completion and I now want to add the iPad UI, which, apart from the layout and maybe some rearrangement of buttons and views to make use of the larger display, will have the same elements and functionality.
Now here's my problem: when I DragDrop my iPad UI element (e.g. a UILabel) to the view controller to connect it I (obviously) can't use the same name...because the iPhone one is already there.
If the item is called 'myTextField' on iPhone I'll have to call it e.g. 'myTextFieldiPad' for the iPad, which means I'll have to branch out every time I want to access the text field depending on the platform. Analogous for IBActions.
In a nutshell: same view controller, two practically identical XIB files for iPad and iPhone with identical UI elements, how?
Am I overlooking something?
Help to point me in the right direction would be highly appreciated...
Beschi

Don't create new properties using drag and drop.
You can connect controls from different xibs to the same property on your view controller using drag and drop, instead.
What I mean is, create your properties using drag & drop from iPhone's xib, and then connect your iPad xib controls to the SAME properties you created from your iPhone's xib.

Try create two classes which are children of your BaseViewController. First controller has name iPhoneViewController: BaseViewController, second - iPadViewController: BaseViewController. In IB for first (iPhone) xib you need connect first class. For second - second class. After that where you create viewcontroller you need check idiome. For iPhone create object of iPhoneViewController and for iPad - second.

Related

Cant control drag a reference object to the ViewController in Xcode 9.2 (9C40b) iOS project

How to a create an outlet for an iOS control on the storyboard that is connected to my swift ViewController code?
All the tutorials for iOS development show that you can control drag a UI element to your ViewContoller swift code for an outlet. However it never works for me, it shows the blue line from the storyboard, but on the drop it never brings up the dialog to add a new outlet.
Creating a new Action drag and drop works just fine for buttons. I have seen many ask this question and there seems to be no definite answer.
Is there something different with iOS projects?
I tried to set the View Controller custom class to my specific ViewController class, and then I can drag and drop a new outlet. However the project fails to compile.
LaunchScreen.storyboard: error: Illegal Configuration: Launch screens may not set custom classnames
I seem to be missing something here, but all the tutorials I watch/read (none seem to cover Xcode 9 or swift 4) all show this behavior as being the normal way to create an outlet.
Launch screens are not able to have outlets attached to them. They are static. If you'd like to have a "launch screen" that you can manipulate, duplicate your launch screen layout into an initial view controller and do what you like there.
I think this is because you are connecting it from the LaunchScreen.xib instead of the main.storyboard files.

Create view for iPad out of existing views suitable for iPhone

To put it simply, my app has two main views: the first serves to choose some values, the second displays a report. Both are TableView based.
The app looks well on iPhone with any screen size and for any orientation, but since the app’s thought to be universal, I want to get rid of those gaps between elements when the app runs on iPad.
I decided to have the app displaying both views on iPad screen simultaneously as official YouTube app does:
(1) and (2) are the first and the second View controller.
I see it this way:
1) Some initial view controller (additional navigation view controller?) checks idiom (iPhone / iPad) and chooses the next viewController. If idiom is iPhone, the first view controller is pushed.
2) If idiom is iPad, the «new» view controller is pushed. It has two view containers containing the fist view and the second view. Containers have autolayout constraints etc.
Questions:
1) Is it a right idea in context of Apple app design philosophy? What’s the better way to create iPad view combining existing ‘iPhone’ views?
2) Where to check for device type? Shall I use the second storyboard or something? Maybe there’s a good tutorial for this case, I didn’t find one.
Thanks in advance!
For the 2nd part of your question I think you want to look into
I think you want to look into Size Classes and Auto Layout
You can make a single view but depending on the size / orientation of the device - you can enable/disable certain components of the view.
You can pin views so that in portrait you have your 1st layout and in a landscape you have your 2nd layout.
With regards to your 1st question - you do have the ability to embed a view inside of another view
I'm not sure if this is what you are looking for - but as far as what apple is pushing I believe size classes is what they "suggest". You can do a lot. The only thing is make sure you turn on the assistant editor into storyboard preview mode it will help a lot.

Different view controllers for different screen sizes

On my storyboard, I have created 2 view controllers; one for iPhone 5 and iPhone 6. How can I access different view controllers for different phone sizes. Online I found something that let me switch different storyboards but I only want to change one screen. Is there any way I will be able to do this or is making a duplicate storyboard with one different screen the way to go?
I find that size classes are so error prone that I was tempted to use that approach at some point (ended up making all my constraints totally independent of screen size and orientation but that's another, interesting but irrelevant, story).
One way you could do this is using custom segues with a naming convention. Let's say you already have a storyboard segue that links to a view controller for iPhone5 and you want to redirect it to your iPhone6 variant.
Give that segue a name that finishes with ".iPhone5". Then create a custom segue (dragging from the red square of the source viewController to the target one) and give it the same name but with a ".iPhone6" extension.
You can implement override shouldPerformSegueWithIdentifier in the calling controller and, if you're on an iPhone6, respond with false and programatically trigger (performSegueWithIdentifier) the iPhone6 segue using the identifier you received as parameter replacing ".iPhone5" with ".iPhone6".
You could centralize that code in a function or an extension to UIViewController to make it easier to implement on your various view controllers.

IBOutlet to two viewControllers on two storyboards... is that possible

I have an app with two targets, for iPhone and for iPad.
This project is not using size classes.
On the iPhone version I have a storyboard that contains a viewController called “termsAndConditions”. This viewController has class files termsAndConditions.h and .m.
Inside termsAndConditions.m I have an outlet linked to a textView on the viewController.
Then I have the second target for iPad. I have the same viewController there called “termsAndConditions”. My idea was to use the same class files termsAndConditions.h and .m from the iPhone version with this other target. But there is a problem.
Remember that the outlet is pointing to the textView on the viewController of the iPhone storyboard? When I assign the iPad termsAndConditions viewController to use the same iphone class files, it apparently works, but when I try to access the textView on the iPad version nothing happens. On the other hand, when I pass the mouse over the outlet on the implementation, textViews on both viewControllers, on both storyboards, highlight.
Apparently the class file outlet is really linked to both textViews on different storyboards, but this appears to be fake.
Is there a way to do that or will I have to duplicate the termsAndConditions.h and .m with another name to use on the iPad storyboard? This appears to be a stupid solution.
thanks.
You may connect 1 object to your class per view controller. Double check that you set the classes correctly; I often accidentally autocomplete the incorrect class. Ex. TermsAndConditionsTableViewController instead of TermsAndConditionsViewController
This IBOutlet is connected to three different views and works correctly for me. Click on the dot to show the connections. Sometimes you need to make the connections again (rare but does happen).

Do I need multiple view controllers for the iPhone & iPad storyboards?

I'm still relatively unfamiliar with all the new features of iOS 5, and what I can do in Xcode now. So, a good explanation would be appreciated.
I'm designed a single-view application and I have both an iPhone and iPad storyboard. I chose 'Single View Application' when I first started, so Xcode created a ViewController for me. Both storyboards list this view controller as their own.
Back in iOS 4 the way that I linked button actions to my view controller was to Right-Click on the button on the nib, pick the action that I wanted, then drag it over into the view controller's '.h' file, which auto-created a method/property for me.
I am confused about how to accomplish this now, since I have multiple storyboards but only one view controller. Do I need to have multiple links for each button; one for the button on the iPhone and one for the iPad? Or is there a better way to accomplish what I am trying to do now?
You do it the same way you did it in iOS4. But obviously you never built an universal app there ;-)
It's totally okay to have a single UIViewController class for two different nib files.
And if you use storyboards it's fine to use different storyboards and a single viewController too.
You can even use the same viewController for different scenes inside a single storyboard.
The connections to the viewController are saved in the nib or storyboard. So you can't overwrite them while designing the other user interface.
Open the iPhone storyboard, make your connections to actions and outlets. Then open the iPad storyboard and make totally independent connections.
In response to the first reply, I was under the impression that a view controller could only support two scenes in a storyboard layout. I say that because I found this thread.

Resources