My project has a UIView located in a View Controller. The UIView has a subclass named AdminView. I want to add IBOutlets and IBActions on the subclass and connect them to controls on the UIView. I have found this is difficult to do with Xcode 8.2.1.
Image with IB and subclass set to AdminView
At first I selected the UIView in IB. I then clicked the Assistant Editor hoping it would bring up my subclass. I first set the jump bar for the Assistant Editor to Automatic Mode. It didn't bring up the correct subclass in Automatic Mode. So next I put the jump bar for the Assistant in Manual mode and tried to navigate to AdminView.swift. Sometimes I couldn't do that because the Manual jump bar only showed interfaces.
Image with IB and Assistant Editor, side by side
Why doesn't Automatic mode select the proper file?
When in manual mode, why does it sometimes show interfaces rather than my .swift files?
When it works properly, I was able to navigate to my AdminView.swift file. Then I tried to control-drag connections from the IB to the file. Xcode would not make those connections.
Why was it not able to make connections?
Next I selected the UIView in IB and opened the Connections Inspector. The connections inspector showed the controls that are inside the view. I then tried to drag from the dots to the right of each control to AdminView.swift. That did not work either.
Image with IB and Connections Inspector
Why did this not work?
Now for some good news. Next I went to AdminView.swift and added IBOutlets and IBActions for the objects in my UIView. I made sure to copy IBOutlets and IBActions from other parts of my project in order to make sure the syntax was correct. The I built the app. Dots appeared in the gutter next to the IBOutlets and IBActions I had just created. Then I put AdminView.swift in the Assistant Editor. Now I dragged (not control-dragged) from those dots to their respective controls in IB. Thankfully, this made the connections I needed.
Image with some Outlets and Actions manually added
Image with some connections
Why was this the only method that worked?
Does anyone know of any other methods besides this to make these connections?
Is there anything I can do differently to make the other two methods of making connections work?
Related
I'm new to Xcode and I'm having a hard time understanding the platform. I would like to know why whenever I'm adding objects (buttons, labels, image views, etc.) to my storyboard, I don't see any change in my .swift file connected to its view controller.
If someone could explain it to me precisely it would be much appreciated.
Because there shouldn't be any in the first place. Connections between the Interface Builder (IB) and your code must be placed by hand (either by writing them first and connecting by ctrl-dragging to the function, or letting xCode generate them by ctrl-dragging them into place).
If you need to reference any views you have in interface builder from your code, make sure that:
IB knows the class the view belongs to (set this in the Identity Inspector to the right for the main container view, usually the UIViewController)
There is a corresponding #IBOutlet (or #IBAction) marked property (or function)
Then just ctrl-drag from the object in IB to the property in the code, and everything should run smoothly.
Here's some info if you need more help connecting the views in the IB to your code.
Happy coding!
I'm trying to create outlets using the Interface Builder, however when drag the UILabel from the IB to my controller's .h file the big lock symbol flashes for a few seconds and it doesn't actually create the bind between the UILabel and the outlet (although the code is generated). I also noticed an empty circle is created by this new definition. When it worked, a filled in circle was created on the side).
I've checked the UILabel and all parent views and have checked that their "Lock" property is set to "Inherit (Nothing)" (and have even set them all to "Nothing").
In the IB the UILabel doesn't show the "x" that appear when a UILabel is locked either.
So I have no idea if I'm missing to unlock something, or what I'm doing wrong to be able to create this outlet.
I don't know if this is relevant, but I'm using a custom view controller, and I'm using this same view controller subclass for 2 different view controllers in the same storyboard. I had no problems creating outlets this way for the first view controller.
Tryed reopening XCode (using 4.4.1) with no luck.
Any help is appreciated!
So are you dragging both UILabels from each View Controller into the same file? I don't think that this is possible, on the second view controller, try to Ctrl drag to the file owner within the IB view, that is, assuming that both views are connected to the same instance.
Also, in your identity tab in storyboard, under Localization Locked, make sure your storyboard locked setting is set to default as mentioned in https://stackoverflow.com/a/11169636/720175
I tried adding a totally new scene and would also get the lock symbol. So it wasn't only the scenes, but the whole storyboard. So a quick search on SO lead me to the answer:
https://stackoverflow.com/a/11169636/720175
My View has a button, myButton, in my XIB. In the XIB the File's Owner is of class HelloWorldViewController. And the view for File's Owner is my View in the XIB.
In the HelloWorldViewController(which resides in MainWindow.XIB) I have - (IBAction) doMyButton:(id)sender and I have the corresponding definition in the HelloWorldViewController.h file.
For my View I have Events (again in the XIB) Touch Up Inside --> File's Owner doMyButton.
For File's Owner I have Received Actions doMyButton --> Rounded Rect Button Touch Up Inside.
I have a debug point on the first line of code that is inside the 'doMyButton' method. When I touch the button (with my mouse in the simulator) I do not hit my break point.
WTF?
Screen Captures of IB if it helps:
Overview
Properties Of Button
If every thing is fine then you are not setting the break point i.e. you are running your code using cmd+R use cmd+y or cmd+enter. cmd+r will run without considering your break points.
Do any of your IBOutlet items function?
If they don't (and since you renamed your XIB file), be sure the File's Owner has the correct custom class set in the interface builder. Otherwise it could be calling the wrong class which could be why your break point isn't firing.
Note: This is unlikely as such an issue should cause a build problem, but I've seen stranger things happen.
Thanks to a co-worker I found out what the problem was. The view I had with the button was in my HelloWorldViewController.xib as it is supposed to be. However, I had drug that view into the Main.xib to be the primary view for the NavController I had setup. I was editing the view in the HelloWorldViewController.xib when I should have been editing (read: making the connections) in the Main.xib where the view was duplicated for the NavController.
I assumed that NavController was simply pointing to the other XIB where my original view was defined, not running off a copy of it. Kind of messed up for a newb perspective.
I'm reading through a beginners iPhone text book and just finished writing all the code for a route tracker app that uses Map Kit and Core Location. I have the app running with no errors on my iphone 4 device but when I tried interacting I realized that none of my IBOutlets were connected to anything. When I referenced the beginning of the tutorial in the text, all it says is to "connect the appropriate outlets".
Here's why this usually simple task has me confused. The tutorial says to create a Window Based Project, so there is no ViewController. Then, in Interface Builder, the view is built in MainWindow.xib. The only IBOutlets of the project are located in Controller.h / Controller.m files that you create and which contain mostly all of the code for the app.
I usually ctrl-drag from File's Owner to the UI in Interface Builder, but in this project there seems to be no way for me to access the IBOutlets in Controller.h / .m from the MainWindow.xib file.
I'm frustrated because it seems like this should be such an easy fix but I'm totally stumped.. any help is really appreciated. Thanks
As I see it, you have two choices:
Add the outlets to the app delegate. This is probably not the best plan.
Put an instance of your controller class in the .xib file.
I think option 2 is what you want. If you check the Controllers section of the library, you'll see a component called "Object". Drag one of those into your xib, then inspect it. Select the info pane in the inspector (the circle with a white i in it) and change the Class to the name of your controller class.
How does one bind an iPhone SDK control (say a UISlider) using Interface Builder?
Unlike regular Cocoa applications, the bindings tab does not seem to be present when the iPhone-app XIB is opened using Interface Builder
Cocoa bindings is not available in the iPhone SDK. You have to do everything "by hand" much as you used to have to do on the Mac.
Declare the control as an IBOutlet in your viewcontroller .h file. In IB, set the File's Owner for the view as your viewcontroller class (Select File's owner, in Inspector window click on the (i) button and set the class as your viewcontroller)
Now with the File's Owner selected, click on the blue button with a white arrow in the Inspector window. Bind the IBOutlet for your UISlider to the UISlider on your view.
To get the value of the slider when it changes, create a method in the viewcontroller with the return type IBAction. In the inspector window, if you connect this to the UISlider, it gives you multiple options to select from. Select the "Value changed" option. Now every time the value of the slider is changed your IBAction method will be called.
Hope that helps.
The iPhone's lack of cocoa bindings support is documented in Apple's dev centre. This is a useful page in general to read if you're a Cocoa developer starting iPhone development.
Key Value Observation (KVO) is still fully supported, however, but you'll need to do the parts you would have done with bindings manually. This makes presenting sorted, dynamic lists of items in, for example, UITableView's much more painful than on Mac OS X where you would just hook in an NSArrayController. See also this question on SO.