Same IBOutlet from different storyboards? - ios

I have multiple storyboards in my project, each having an almost identical view controller (only dimensions are different). The storyboard used depends on the device in use. What I want to do is have the equivalent element from each storyboard under the same IBOutlet. This way, whatever I do to an element in the storyboard being used, the same would be done for all other storyboards. This is instead of creating an IBOutlet for the same element in each different storyboard.
For example, we may have two buttons, one in each storyboard. They are meant to be the same button but in different sizes, I set this button's alpha to 0 at one point the in Swift file. How could I do this for both buttons under one name (the same IBOutlet)? I know this means doing something on a storyboard which isn't even being used and therefore not accessible on the device, and I'm not sure whether it'll spit up an error or not. Surely this is a way around this though, because there are apps which use multiple storyboards.
I could imagine possibly stating if (storyboard == xnamex) {execute code for specific storyboard}, but this would mean having multiple if statement with the whole code repeated for different storyboards, and having to create an IBOutlet for each element, which is unrealistic. How would I get around doing this?
Many thanks.

If it's exactly the same button except as you mention the size on it. You can just pull an outlet to the same name and they both will be contained in there. As mentioned [Multiple buttons connected buttons best practise you pull the outlets to the same place and then action to the same place as well. However. Sometimes the action can be tricky, if you get problem there, just create a new action with the exact same name and then remove it. It will still be connected to the same name.

Related

How to add items to an Outlet Collection in Xcode

I am creating a quiz app and for the buttons I need an outlet collection. So far in the array, I only have one button but I want to add the other three. On the other tutorial I've watched, it says to click the button on the side and drag to the thing you want to add (image link below). This doesn't work for me, is there a certain way you need to click? I've done this before, but this time its not working. The difference this time is that I've created a new class. I am not very familiar with Xcode or swift, sorry. Thank you.
Xcode is really buggy as far as outlet connections are concerned. Emptying the Derived Data folder as well as clean / rebuild help. But results could still be problematic. However, the old way to connect things which has been around since before the existence of an Assistant editor still works reliably. Select the ViewController and drag from ITS outlets in the Connection editor to the storyboard items (i.e. buttons).
Use Interface Builder alone. Use the Identity inspector to make sure your view controller has the correct class, the class where you put your outlet collection property. Now use the Connections inspector for your view controller. You'll see the outlet collection listed there; drag from it to your buttons, one by one.

Custom component in Swift

I have a storyboard that I want to reuse many times in my app. You can see it here.
The problem is, I don't know how to use that xib to create a custom component to use it. I will need many instances of that component, and once you click on the UIImageView in the right, or in the label, it will get you to a different scene, depending on which individual instance you clicked. Also, the left UIImageView will be different in every instance.
I have done similar in Android, and now I need to do it in iOS, with Swift.
Ive searched, and almost everything I found its in Objective-C. Does anybody knows about a good tutorial to do it in Swift?
Thank you.
EDIT
More elaborated explanation:
1) I have a scene where I need to put that component, as a whole, four times.
2)In every individual component, I need the image view in the right (the one with the white arrow) and the label to be clickable.
3) In every individual component, the click in the image or the label, will lead to a different screen in the app.
4) In Android, it looks like this. The label and the left ImageView are not seen, since they are set programmatically, but they are there. In the upper component, the click leads to an Activity. In the second one, the click leads to a different Activity, and so on.
From the images and your description, I would still recommend UITableView and custom UITableViewCells. But if you prefer not, ...
To create instances of your component from the XIB file, use the NSBundle "loadNibNamed" method:
let viewArray = NSBundle.mainBundle().loadNibNamed("ClickableComponent", owner:self, options: nil)
let clickableComponent = viewArray[0] as! UIView // or your subclass
You can then tailor programmatically to load the correct images and assign the associated actions.

Is it necessary to create all views in storyboard in the beginning?

I am new to iOS and have recently finished some classes.
Now, I'm onto my first app with Swift. I wanted to know:
Is it necessary to define all views/screens in the storyboard right in
the beginning, with proper navigation controllers?
What is better, define all in the beginning or keep adding as need
arises in the future?
Keep adding as needed.
You'll always be finding or coming up with new features, and at times you'll need to insert view controllers between other view controllers (adding an extra step, for example).
Within view controllers, you'll also find the need to put items into containers. That's where clicking the item/s (selecting them), and going to the menu, selecting "Editor" and then "Embed In" comes in really handy. For example, you may want to put a series of items into a scrollview after you've already laid them out properly, because they don't display properly on an iPhone (and you had done all of your testing in Simulator on an iPad first).
No it's not, you can create all views programmatically without using a storyboard at all. Its's just your choice what works better for you.
First of all, you can use storyboards for your UI (and navigation), use .xibs (only UI) or create everything programmatically. Each of these has its pros and cons. Also you can mix and match all these. For example have a storyboard, some views with .xibs and some views created programmatically all in the same project. You just have to choose what fits best your needs.
You don't have to create everything in the storyboard right in the beginning. Create only the views you need to start coding and then add more views as you progress your app development.

iOS/XCode 6: Tips for when to connect storyboard with code?

I'm a beginner programmer and could do with as many 'pointers' as possible (or not!) :/
Just wondering if anyone can give me some guidelines on when controls, etc., added via the interface builder to your storyboard in Xcode 6, need to be connected to in your code?
I guess I'm looking for some general, and more specific, guidelines on what exactly you do in interface builder needs to be qualified in your code?
If there is something you do in the interface builder that you need to tell your code?
I know there is a lot but I am just looking for a kind of summary overview I think.
If you need to get or change the property of some object you made in IB, then you should make an IBOutlet to it.
If you have a control (button, switch, slider, etc.) that sends an action method, you should connect it to an IBAction in your code. You don't need to do this if you connect a segue up from a control since the segue is executed by touching the control (a button or cell generally).
You don't need to connect a view with the code, if you're not accessing it. For instance, some views might just be containers to hold other views, so you might not need to do anything with them.
There are exceptions to these generalizations of course. You could make an IBOutlet to a button, and add its action and target in code, but that's not generally how you would do it (except for buttons in table view or collection view cells, where this technique is quite common). You can also access views by their tag (with viewWithTag:), so it's not an absolute necessity to have an IBOutlet to access a view, though generally it would be better to have one.

How can I customize instances of a template xib

I'm learning about xib files and just starting to understand why people use them as well as or instead of storyboards. My question is about how and when it's appropriate to use the xib as a "template".
Let's say I have a xib mapped to my custom UIView subclass - I know how to set that up in IB - and my xib has a UILabel subview. This is a very simplified example just for the purposes of the question, but basically I'm trying to create a view that can be reused for each screen of an iOS "introduction" walkthru, like the panels of https://github.com/MatthewYork/MYBlurIntroductionView
So I want to set most of what's in the view up at design time, and most of it will be common to each instance. The text I want to put in the UILabel is going to be static (i.e. I know it now at design-time) but each instance of the view will have different text. So let's say that I want to position the UILabel in different places in each instance, depending on how much text is in it etc and whether it's covering something else important. Now, I know I could do this programmatically, i.e. have the label as a #property linked up in IB and then set frame position in the code, but as far as I understand it the beauty of using xibs is that you can do known things like this at design-time.
As far as I can see my options are:
Load new instances of the xib and set the position etc programmatically as mentioned above (would rather not if possible)
Create my template xib, setting all the common stuff, and then make copies of it "CustomView1.xib", "CustomView2.xib", etc. (a bit yuck but not too bad)
After creating my template xib, use the storyboard to drag in new UIViews and somehow set each to be linked to my one "CustomView.xib", and then somehow do my static repositioning of the subview UILabel within each of those UIView instances on the storyboard. Is that possible? If so that'd be great. Obviously I know I can set each of those UIViews to be instances of my UIView subclass, but I'm just missing the link between doing that and customising each instance. Does the file's owner have something to do with it?

Resources