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

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

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.

UIViewController defined in a xib file doesn't load in the storyboard

I'm trying to load a subclass of a UIViewController with its views defined in a xib file into a storyboard. Let's call it a NibViewController.
The point of this approach is to reuse the same ViewController in multiple screens of the app.
I know it's possible to do it manually in the code, but I'm looking for a solution in the storyboard. I've tried suggestions from other topics like this one, but nothing worked. The ViewController is correctly displayed in the simulator but not in the storyboard. Here is the code: https://github.com/srstanic/NibViewControllerInStoryboard
and here is the screenshot:
Am I mistaken to expect the contents of the NibViewController to appear in the storyboard?
Am I mistaken to expect the contents of the NibViewController to appear in the storyboard?
Yes, you are mistaken. Your app is working perfectly so you should stop worrying and just proceed.
By deleting the view from the view controller in the storyboard, you have specifically instructed the storyboard: "Do not make a view for this view controller. At runtime, the view should come from the xib file, not from you."
And that is exactly what does happen at runtime. So just design your interface in the xib file and all will be well.

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

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.

How can I rewire a MainView-iPad.xib to the viewController?

I am porting my Phone app to iPad.
I changed my target's "Devices" to Universal under "Summary".
I put the conditional (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) to determine whether the app is running on iPad.
I created a new XIB dedicated for iPad and placed a view in there.
How can I rewire the IBOutlets back into the iPad version of the XIB?
Thanks
You probably need to define the class of the File's Owner:
Since you create a new XIB, it should be set to UIViewController, just select from the list your UIViewController's subclass.
The new image:
Just a small detail:
Avoid using this kind of stuff: (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad). It completely destroys your application architecture when creating a version for iPad and iPhone of the same application, which share the same code. Just sub-class the class you want. Keep the common code on the parent and the specifics in each sub-class.
select "Identity inspector" window on new Xib.
change class name to your viewController
then select file owner and your outlet automatically show up
there is the Controller class is same for both Xib
You don't need to rewrite any IBOutlets. But just assign the proper class to the xib's file owner and all the IBOutlets of that class is available to you and as you have used them in iPhone xib use them for iPad's xib.
Happy Coding :)

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