简体   繁体   中英

Connect IBOutlet to universal Storyboard Xcode6

Using Xcode 6 beta 6 developing an iOS application that is universal application. I am facing an issue with connecting IBOutlets and using universal storyboards. If i am using "w any h any" configuration then i am not able to design two different interfaces for iPhone and iPad. if i design using different configurations ie "w compact h regular" for iPhone and "w regular h regular" for iPad then i am able to design two different interfaces but not able to connect Same IBoutlet to both of them. If i connect it to iPhone configuration ie "w compact h regular" first it runs smooth but as soon as i connect it to iPad configuration ie "w regular h regular" then the connection with iPhone get disconnected automatically. Pls help me out i am not able to get a solution i have searched a lot on google but couldn't find anything as not many resources are available on this.

EDIT If i connect IBOutlets to the view in "w any h any" configuration and then switch to iPhone configuration and add constraints there storyboard displays some errors like conflicting constraints. Pls see the image

在“ w compact h normal”中添加约束时发生错误

This is for iPhone configuration after connecting the outlets in "w any h any" and adding constraints in iPhone configuration

I'm not sure if this is bad practice, but when I made my app universal, I duplicated my viewControllers and linked them with the appropriate storyboards. I had one storyboard named iPadStoryboard.storyboard which was linked with iPadViewController.swift . The other storyboard was named iPhoneStoryboard.storyboard which was linked with iPhoneViewController.swift . Hope this helps! Sorry if it is inefficient!

This question is a year old, but just in case there are folks still struggling with this, here is the solution.

If you are planning on hooking up the same IBOutlet in multiple layouts it is required that you lay it out in the Any - Any layout and connect it to the IBOutlet in your code from there first. Also to avoid compiler warnings setup some constraints for it, even if you will never use that particular layout.

For example lets say your universal app is setup to only run in portrait on either iPhone or iPad, and lets say want to add a textField and a button. First select Any-Any and place the textField dead center of the screen and set a constraint for that, then put the button right below it and constrain it there as well, and then connect each of them to your code.

Next switch to Compact-Regular for portrait iPhones, and drag the textField down to the lower left part of the screen and put a constraint to keep it there, and place the button right next to it and constrain it as well. You DO NOT need to reconnect these to the code. They remain connected since you did that on the Any-Any Layout. Now switch to the Regular-Regular layout and repeat the same thing you just did, but this time put the button and textField in the upper right hand side of the screen and constrain them there.

That's it. They are still connected to the code, but the constraints will apply correctly based on the device. If you run the app on a portrait iPhone the button and textField will be displayed on the lower left of the screen, on an iPad they will display on the upper right. If you switch your iPhone to landscape they will be constrained to the center since that is where you put them on the Any-Any layout. All of these are connected to the same IBOutlet that you connected them to on the Any-Any layout.

Be forewarned however, that if you make changes on the Any-Any layout it will affect the others. So layout Any-Any first and make absolutely sure it is correct, then switch to the other ones and make changes as necessary.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM