How to add items to an Outlet Collection in Xcode - ios

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.

Related

Can't link to outlet collection in XCode9

I have some constraints that a shuffle around during an animation. After upgrading to XCode9 I tried to make some tweaks, and the editor seems to no longer allow me to add constraints to an constraint collection IBOutlet. I even created a new project and tried to make a simple outlet collection and ran into the same issue. Here's a short video of what I'm talking about:
I've tried the usual things of deleting derived data, restarting xcode, creating new xibs, praying to the xcode gods...etc. As you can see in the gif, it even happens to me in a brand new project. Anybody else seeing this? Any work-around I'm missing?
I think this is a legit bug, I submitted it to apple.
I'm surprised I don't see anybody else running into this.
I had the same issue. This is the only workaround I could find to get it working is to create the Outlet Collection in code (or create the first one by dragging from the storyboard as you're doing). Then drag from the 'add' icon in the ViewController's line margin back to the objects on the storyboard rather than the usual way around.
This also seems to be an alternative way:
https://stackoverflow.com/a/45597939/1320134
In summary, you need create the first outlet collection as you are currently doing, then drag from the newly created 'referencing outlet collection' in the Connections Inspector to the other objects you want to add to the collection in the storyboard.
I tried the same and got the following result. I guess it's working the other way. Check this link out.
Steps:
Drag the required outlet to the view controller file.
Select the outlet collection, name it and add it to view controller class file.
Select the outlet collection created in view controller file from the circle button showing the connection.
Drag from outlet connection to your desired outlet of the same type and it will be automatically added to the same outlet collection.
Or you can do it by going to connection inspector on the utility box on the right-hand side.
Find the desired outlet.
Click on connection circle and drag to the outlet you want to connect and it will be added directly to the outlet collection.
Hope this helps you.
Thanks!

Same IBOutlet from different storyboards?

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.

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.

Hiding some views in the interface builder

Is there a possible way to hide some views in Interface Builder? I don't mean to hide it when the app launches, but to hide it just in Interface Builder to be easy for me to see other views.
Actually I want to do that because my xib file has a lot of views and it is hard to see !
Admdrew,
Unfortunately there is no way you can do that in storyboard. The only thing you can do to view the Views which is overlapped by others, is drag it to the top.
For example, I drag PickerView to the red line to make it to the top, then edit PickerView, and drag it back to the previous position when finish: https://www.dropbox.com/s/e18nmr9mya9y0s8/DizSfOW_3F3a_wV4YVXgFk__h0PoFWbd3rs1HbcTN-4.png
I prefer to use many View Controllers, it's better for your performance. But I must say that in some cases, you still have to use many UIView place on top of others, and I think this is the best way to edit each View.
Cheers,
UPDATE AUGUST 17, 2015
Since XCODE 6.x already added in the feature called "Installed" in the right panel, so now you can easily disable the upper view for temporary by uncheck "Installed", then do-whatever-you-are-planning-to-do with the lower view. Just remember to return it to be checked after you finish.
Step by step following this:
1. Click to select the upper view.
2. On the right panel, look for "Installed".
3. Click to uncheck it.
That's it!
Cheers!
You'd use the Hidden property in the Attributes Inspector. But I'm not sure is this is what you'd like to achieve.
By doing that you'd need to set the attribute to Hidden = NO before running your app.
I think that using groups of views could be also a better way to handle lot of views in the same xib files. You could do that Editor -> Embed In -> View. Selecting all related views and making groups could help you to work with them easily by hiding or moving some groups.

how to remove all references for outlet

I accidently set event for command button as "outlet" than "action". Due to auto-drag feature, xcode generated references in applicationDelegate and .h and .m files. I removed the entry from .h and .m files but simulator wont run because it is still looking for that missing reference. I could not find that outlet referred anywhere. I am fairly new to iOS programming. Can anyone suggest a solution for this?
You have to unlink the outlet from the item in interface builder. Select the UI element that you linked the outlet to and then remove it in the "Connections Inspector".
Click the little "x" ^^^
I think there are two different reasons that people may come here wondering how to get rid of an unwanted Outlet reference.
Created an unwanted outlet while using the Interface Builder.
Copied some Outlet code from another projects View Controller.
Both reasons are fairly easy to solve.
Unwanted Outlet in IB
Method One
Right click the view in the storyboard and then click the little x by the referencing outlet.
Method Two
Right click the view name in the Document Outline. Then click the little x by the referencing outlet.
Method Three
Select the view on the storyboard and then click the Connections Inspector. Then you can click the little x to remove an outlet reference.
Unwanted Reference in Code
If you copy the view controller code from one project into another project you will notice a strange thing happening with the outlet. It is referencing the view from the first project without you ever connecting any outlets!
If you left click the little circle and click the reference link you will even be taken to the storyboard view in the other project. Trying to figure out how to get rid of this reference can drive you crazy.
Well, the good news is that there is really nothing for you to fix. You didn't copy over some deep, hard to find referencing link. Xcode is just getting confused because the View Controller and the outlet name are the same in both projects. Basically just ignore what that little circle says. You can close the first project and Xcode will figure things out eventually. Use the methods in part one of this answer to see if a view in your current project has a referencing outlet or not.
No need to delete Derived Data. That doesn't solve the problem anyway. Just try opening both projects at the same time again and you will get the same strange behavior. The way I figured this out was to separately create two new projects that had view controllers with the same name and a referencing outlet with the same name. I never copied anything but Xcode showed one was referencing the other. Like I said, ignore it.
It will be listed in the storyboard/xib as well, on the command button outlets.
Delete it from there as well, and you should be good.

Resources