Wiring up Outlet Collection Trouble - ios

From what I can tell there are some challenges people are having in XCode6 in terms of wiring up outlet collections. I just wanted to see if i'm doing this right or what I need to do correctly! I reviewed here Can't hook up an outlet collection in Xcode 6 using storyboard as well as other areas, but I wanted to be more basic about this first.
I have 7 images on a screen. I'd like to iterate through all of them in a loop or something. I read an outlet collection would be a sensible way to do this. To create an outlet collection, I am doing this:
While splitting the screen (storyboard on left, assistant editor on right) I control+click on one of my 7 images, and drag it into the view controller code on the right. This will prompt me to create an outlet, or outlet collection (I select outlet collection).
Now that this is done, a circle appears in the left margin next to this variable indicating that the image is associated with that variable. If I hover over the circle, the first image lights up.
What you're supposed to do (can someone confirm this please??) is you just hover your mouse over the circle, and a + symbol appears, and all you do is hold down the left mouse button, and drag over to another image (for example image#2), and it should automatically add that to the collection. The idea is to simply drag to each image so they are all added.
Is that right in step #3 above? Is this the normal way you would add items to a collection? Drag from the + to the image within your controller? For me, when I do this, the line appears, but it doesn't seem to respond to connecting the line to anything. I just hover it over the images, and it doesn't highlight the image or select it or anything, and when I let go, nothing happens.
I'm still somewhat new to this - what am I doing wrong?
Thanks so much for your patience/help!

OK I had the same issue and I think I have just found a workaround
1) Do steps one and 2 with the first image
2) Now repeat exactly what you did using the second image - drag and drop just below the first outlet collection (this would simply create a second outlet collection use exactly the same name )
3) Now Xcode won't like it as you have two outletCollections with the same name. Now comment out the first one
4) Now you only have one outlet collection. And it would be wired up to both your images. confirm it by hovering your mouse pointer over the little 'circle' next to the IBOutlet to see your images being highlighted.
This seems to work for me. So hope this helps :-)

There is an error with Xcode and Interface Builder that causes problems with outlet collections. You are doing it right, Xcode is problematic.
A workaround is to:
Go to your DerivedData folder (Xcode preferences -> locations)
Clean build folder from the Product menu while holding option (alt) button
Quit Xcode
Delete the folder starting with your app's name in the derived data folder
Go to your xcodeproj file, right click it, show package contents.
Delete the xcuserdata folder (warning: don't touch the others!)
Open Xcode again.
When you delete user data, window positions etc. will be reset to defaults and breakpoints will be deleted. Your project should work smoother (after a few seconds of re-indexing) and outlet collections should work properly.

I have had some success by just changing my workflow as follows (no need to restart Xcode).
First, create the outlets in code in the controller class file.
Second, drag from the objects (in the outline view of IB) to the controller, or back (for actions).
This has always worked without fail. It is slightly more effort, but actually negligible. Definitely an Xcode bug.

Related

Xcode 8 Adding Layout Margins Crash

When I try to add layout margins to a label on a TableViewCell, using the right panel on Xcode, I try to add Explicit Margins (top, Left, Bottom, Right). But, as soon as I input a number value, or tap the arrow to change the value, Xcode crashes.
I've attached a photo to show the panel I am trying to add the layout margins in, and where it crashes.
Any ideas why it wont let me adjust the margins?
try to clear the constraints, connect/ reconnect the outlet and apply new constraints.
Had that problem. Its Xcode bug. Already fixed, so don't worry :)
I was facing similar problem.
The answer that I found and which I used in real project (but for UICollectionView) was to get the outlet of CollectionViewFlowLayout and to define the margins (line spacing and item spacing) by myself in code.
For example
collectionViewFlowLayout.minimumLineSpacing = getCollectionViewWidth() * 0.05
Where getCollectionViewWidth() is a function, that returns the size of collection view (for different screens it will be different) and 0.05 is just random coefficient.
I hope it may someway help you, you can feel free to ask anything else.
If you have crash when you add margins on any view in your storyboard you can try to get their outlets as well and to redefine their frames. It's kinda bad style, but if you won't figure out the problem its the only one I can recommend to you and which I used myself.
Also try to Clean project by clicking Product - Clean in you task bar when in X-Code.
Clear all the constraints and try from applying one by one. That way you can resolve this issue.
Please remove all the constraint applied and connected to the outlet. And Try to apply new constraints.
Some time we delete Outlet from you files .h or .m or swift file which was connected to view in Storyboard or xibs at that time this problem occurs.
Clearing all constraints is the way to go. Look around every nook and cranny and right click every little thing in both your Main.storyboard and the ViewControllerScene menu to its immediate left. If you can get the formatting back to its most base level your constraints will work fine!
Clear all the constraints and try from applying one by one.and take .xib file and connecte with Table view Cell
Xcode stores some user state information inside the Xcode project file, which is really a folder. That state information might have become corrupted. You can normally throw away everything inside your .xcodeproj folder except the project.pbxproj file. This might fix your problem.
Open up the folder containing your .xcodeproj file. Right-click the .xcodeproj file and choose Show Package Contents. Then throw away everything except the project.pbxproj file.
If you know what an Xcode workspace is, and you're actually making use of it, you might not have a project.xcworkspace file to throw away, or you might not want to throw it away. But if you don't know what an Xcode workspace is, then you're not using it so you can just throw away project.xcworkspace. Xcode will recreate it automatically.
see This Link

How do I wire up the storyboard to Swift code in XCode UI?

This seems like a basic question, but I haven't been able to pin it down.
I have an Apple Watch Glance that is meant to have, going from the top down, a thin label and then a thick label in the upper area, and then an image in the bottom area.
I have IBOutlets in my Swift code:
#IBOutlet var clockFaceImage: WKInterfaceImage!
#IBOutlet var hourLabel: WKInterfaceLabel!
#IBOutlet var modernLabel: WKInterfaceLabel!
When I right-click on the labels on the storyboard, it has "Referencing Outlets" with an option of "New referencing outlet". Nothing interesting happens when I click or right-click on "New referencing outlet". The circle to the right converts to a plus sign when I hover, but nothing interesting happens when I click on that.
What should I be doing to set things up so that GlanceController methods can assign values to the image and labels?
Just as a note, Ctrl+Click and Right-click offer the same functionality.
There are two ways to hook up outlets from your interface builder file. Both of them require having your assistant editor open, with the Interface builder in one pane (usually the left) and the relevant source code open in the other pane.
Method 1
You can ctrl+click directly on the UI element in question and without unclicking, drag it straight into the source control file where you want to add a property (or over an existing property). If the property already exists, you can drag to that property and hook it up. Otherwise, you can drag to empty space and when you release the click, you will get a pop-up dialog asking you some information about what sort of outlet/action you want to create (IBOutlets, IBOutletCollections, and IBActions are all created in the same way).
Method 2
You can right-click (or ctrl+click) on the UI element in question to get the black pop-up you mentioned. From here, find the circle for the thing you want to hook up (in this case outlet), click and drag from there to the source code where you want to hook it up, just as previously described.
Generally speaking, I personally usually use the first approach, however the second approach certainly has value when you need to hook up different actions of the same UI element, or if you just need to check whether or not it even has been hooked up yet.

Drag and move something in Interface builder, without changing parent/superview

I have some views in interace builder. I have a background view that takes up my entire view area
and above it I have some buttons which are NOT in the background view.
I want to drag those around to reposition them; but when I do, IB will reparent them when I let go of them.
That is to say, IB seems to only understand drag/drop in this case as a reparenting action; not a moving action.
Is there a hotkey, or something I can do to make IB just move things, without changing the parent when I let go?
This has to be the most painful part of working with IB for me.
If you hold the command key down while dragging, the dragged element will not change superviews. At least in Xcode 7.3.
This has been bothering me as well. I've found a workaround which I find slightly less annoying. Sounds like a lot but it's really quite easy.
Select the item you want to move
Goto Editor -> Embed In -> View (this creates a parent view for your item)
Resize (but don't move) the parent view to encompass the area you want to move to
Move the item within the parent view
Select the parent view and Goto Editor -> unembed
Also, since resizing doesn't cause the parent to change you could actually move a view with two resizes. However it's tedious to get the size right again which is why I like the embed method.
You can press function key and drag the object: Fn + drag

Beginner IOS development - how to get rid of a controller I accidentally added

I am not sure what I clicked, but I was making a home screen for my first app on which I am learning, and I added something called the "Root View Controller" How do I get rid of it? I didn't mean to add it.
Also, are controllers here kind of like the code which gets processed after a button is clicked? I have not gotten to that part of the tutorial :)
Thanks!
If you are using Xcode 4, you can easily delete controls by selecting them on the inspector area on the left. Just click the item to delete and hit backspace.
Edit: Saw the image now. Just click what you want to delete and hit back space. As simple as that.
The code that gets processed after clicking (touching actually) is linked through IBActions. You should look those up, since they are a VERY important part of apps. Let's say they are the "event handlers". Good luck.
Click on that "Root View Controller" to select it, then press your "Delete" key on the keyboard. That should remove it.
View controllers usually receive actions (i.e. the IBActions that #Fiire referred to... +1 to him!) when you press on a button. If you didn't set up any actions pointing to the accidentally created root view controller, you should be completely safe to delete the view controller. And if you did set up actions, you can find them using the object inspector and then "X" (cancel) out any connected actions from buttons or controls to the view controller.
Two points here, just to add to the other answers. First is that Xcode has a lot of levels of undo, so usually control-z can bring you back. But Xcode does crash and then you cannot undo. For this reason, I (like many other devs) use Git obsessively as they work to be able to recover from situations like yours. A test project like this is a perfect place to get your git on... or Mercurial or whatever solution you happen to like.

Why storyboard constraints are changed automatically?

Friends my project is in ending state ,but even in my backup all constraints are changed i,don't know how and why ,but if i run all is available but in storyboard can't see any thing.
it is storyboard image :
it is simulator image :
how i have to solve this ,i need my previous constraints how its changed automatically ?
No issues, you can get your storyboard clean and restored.
Goto your storyboard, select the particular view controller/ table view controller.
Choose the file inspector on the right side, goto Interface Builder Document section uncheck Use Size Classes
Proceed by choosing Disable Size Classes option from the dialog popup. Find that all your views have been visible now, and there is the possibility of constraint warnings.
Click on the yellow mark to the right side of your view controller, update the constraints one by one.
At the end, your previous viewcontroller/tableviewcontroller state is restored.
If you have more than one viewcontroller/tableviewcontroller has this problem, do the above steps for all.
Note: Don't forget to check the Use Size Classes back.
Happy coding :)

Resources