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.
Related
I'm using xcode version 13.3.1 to learn swift for macOS. I wanted to know how I can have an input box show up after a button is pressed and use that input in my program. So far I have a function that is connected to a button.
Learning Swift and learning how to create apps with an event-driven UI are two different, non-trivial tasks. Then there's learning the Appkit for developing apps for Mac OS.
You might want to start with building a command-line tool. That would let you concentrate on first learning Swift, and then you could move on to learning AppKit and how to build event-driven GUI apps.
As to your specific question. It's been a while since I've done MacOS development, so my answer may not be exactly correct. You'll probably have a view controller that has an IBAction attached to your button. You'll probably define your UI in a storyboard.
Then you'll probably want to create a text field (NSTextField or NSTextView). Add that to your storyboard and get your window's layout the way you want it (using auto-layout constraints) Attach your text field/text view to an IBOutlet in your view controller. Set the text field's hidden property to true so it starts out hidden. Also implement the appropriate NSTextField/NSTextView delegate methods in your view controller, then connect the text field/view's delegate link to the view controller.
In your button's IBAction method, set the text field's hidden property to false to show it.
Then you'll need to figure out how to respond to user input. Do you want to respond as the user types, wait until they press enter, have a button for the user to tap, or something else?
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?
I have a custom ViewController which allows selection of an image from a grid. The ViewController uses the AQGrid control and is really just a modified version of the samples shipped with AQGrid.
I'm trying to hook it up in InterfaceBuilder, but documentation on how to do this seems sparse. In standard Cocoa apparently I should create an IB plugin to use with IB, but I'm just looking for the quickest option really as there are a few of these custom ViewControllers which I need to hook up (including a rating control). I've also tried dragging the "Object" controller in IB to my tabpage, but it just gets added to the tree outside of the tabcontroller (see screenshot).
As far as I understand your question, you have to add a generic UIViewController to your nib file and then change its class in the Identity inspector to whatever you need as long as it's a subclass of UIViewController.
Use a "Custom View" object and assign the appropriate class in the inspector's info tab. Using Interface Builder plugins isn't really advisable anymore, because Xcode 4 doesn't support them.
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.
This should be a pretty easy fix, but I haven't figured out from reading the Apple documentation or other SO questions how to do this simple switch from creating my Interface programmatically to using Interface Builder.
I am basing my code around this framework:
http://www.pushplay.net/blog_detail.php?id=27
The only difference is that, where each View is programmatically created (View01.m, View02.m) in ViewDidLoad, I instead want to import from a nib (while still using this framework) for each view (each view has a unique IB design).
Thanks for the help.
Think of IB as an Object Creator and not a code generator. That really helps. What IB does is actually create instances of objects as they are dragged on to the desktop/view/XIB window. It then allows you to start creating various connections (with a control drag on the mouse) from one object to another object. You then instantiate the entire XIB by unarchiving it from your bundle. This is highly automatic and reading up on UIViewController should move you along a bit. Look at:
initWithNibName:bundle:
You basically have two types of connections:
Outlet: This is how you teach one object about the existence of another object. For example, you might have a controller object that needs access to a button. You create an outlet (either in XCode Text Edit in the controller.h file/property area or in IB by adding an outlet) in your controller and then control-click-drag from the outlet to the button.
Actions: This is how you trigger an event on one object to call a method on another object. Actions will have a prototype of:
- (IBAction) someMethod:(id) sender;
I think the ":(id) sender" is optional if your method does not need a link to the object causing the event.
Within IB, you can arrange objects and set various attributes like size, color, position, target/actions, user interactions, Files Owner...
That brings me to files owner. Big concept here. It tends to be the Controller that loads the NIB (OK: I have a custom window controller I have used for over 15 years but Apple has a really good one UIViewController that does all sorts of goodness.) and acts as a proxy in IB. It is not actually instantiated in IB but it will be when you alloc and request it to load the NIB (XIB files are XML files that are turned into NIB files by the compile process)