Real simple . . . say I create a slider, and I want to bind it to the value of a variable somewhere in my app. How do I do that?
In particular, I downloaded Apple's Temperature Converter sample code. But if I delete one of the bindings, I can't see any way to bring it back.
Select the slider in Interface Builder and choose Tools -> Bindings Inspector. There, select Value and enter the specifics of the binding.
Related
I'm pretty new to programming in Swift, and I'd like to know if there is an easy way to add settings/preferences to my Cocoa application in Swift. If possible, I'd like a step by step guide. I mostly want to know how you store the user's preferences on disk and the code part. In my current code it will need to check which setting the user has chosen, and based on that perform an action. I'm using Xcode 7.1 and Swift 2. Thanks in advance!
The NSUserDefaults class is very easy to use in code, and its shared instance is readily available for binding to controls in Interface Builder.
For example, if I wanted to have an integer preference named "elmer" and set its value to 7, it's as easy as:
NSUserDefaults.standardUserDefaults().setInteger(7, forKey: "elmer")
To read the value back:
let elmer: Int = NSUserDefaults.standardUserDefaults().integerForKey("elmer")
To bind the value to a control in Interface Builder, set the Controller Key to "values", and the preference name for the Model Key Path:
I would recommend reading the "Preferences and Settings Programming Guide", and also to familiar yourself with the "NSUserDefaults Class Reference".
SWITF 5.x
The class changed the name so now you do:
UserDefaults.standard.set("1234", forKey: "userID")
To set a key that can hold any type. Or you can be type specific like this
UserDefaults.standard.bool(forKey: "IsConfigured")
The UI binding still working in the same fashion #ElmerCat well explained.
In Xcode 4 or above, it has a handy function allowing us to CTRL + drag an object from the interface to the .h file to quickly connect the object with an event method (assume the Assistant Editor is enabled).
Say we have an UIButton in the interface, and we want to add an IBAction for its "touch up inside", we enable the assistant window and press/hold CTRL + Drag the button to the .h file to quickly generate the necessary codes.
In the popup prompt box, say we set "connection" as "Action".
In the "Type" drop-down, we can select "id" or "UIButton". <--- this is where my problem is.
The strange thing in Xcode 4.2.1 is: no matter what I select, it always generates code: "(id)sender" as the argument.
I know it is easy to manually change it to "(UIButton *) sender", but what is the point of this drop-down when it always generates "(id)"?
Is this is a bug of Xcode or am I missing something to make it directly generate the code "(UIButton *) sender" when I select "UIButton" in this drop-down?
Edited on 27/Feb/2012: This is confirmed solved in Xcode 4.3
- (void)action:(id)sender is just the way actions are defined. you can, in theory, connect different UI elements to the same action. after you've created the connection, you can manually change id to whatever class you want, or just do a cast inside the method.
I'm in the process of teaching myself basic cocoa application development and so I'm stepping up to the classic "calculator" project.
What I was wondering is this: to create the actual calculator interface would it be best to just add 4 rows of 4 NSButton controls and edit them to have the label/functionality I want or is there a more efficient way to create the layout?
I know this is a basic application but following OS X rules I want my code to be as efficient as possible so I want to make sure I'm doing it right.
Thanks for any help guys.
There's really nothing wrong with creating 4x4 buttons and you'll have the most flexibility with this approach. The most efficient way though would be to use an NSMatrix. To create a matrix of buttons in Interface Builder, create one button (your prototype), then select Layout->Embed Objects in->Matrix from the menu. When you now select the matrix, you can specify 4 rows and 4 columns in the Attributes tab of the inspector window.
Im working on an app that needs a source list like the the ones found in Finder. So far I've gotten Core Data working with an NSOutlineView but the group headings don't look very source list like. A real source list group heading looks like and the standered on in an NSOutlineView looks like . It appears that the only major differences are text color and capital letters. Is it possible to change the color of only the group headings or is there a source list heading "theme" I can use?
In Interface Builder make sure to set the TableView's Highlight value to Source List (the default is regular). Then make sure you implement the -outlineView:isGroupItem: delegate method and return YES for any group item.
Check out the SourceView sample code in Apple's developer documentation. They provide a really good example of how to do this. It requires a bit of code in the NSOutlineView delegate, and there is no default "theme" or anything, but this should put you on the right track.
Does this control have a name? Or is it just a bunch of simple controls merged together? If so, what controls are they?
http://img8.imageshack.us/img8/3002/picture2xrb.png
It looks like an NSTableView with an a custom cell type and no column header. Have a look at the documentation for NSTableView's tableView:dataCellForTableColumn:row:. For columns which have the same type for all rows you may also set the cell class in interface builder.
I doubt the search box is part of the same control.
You could open the Application's Nib file to see what is in there. Look inside the application bundle. If the application is called Example then you should be able to find the Nib at Example.app/Contents/Resources/English.lproj/MainMenu.nib.
The best tool for investigating this is fscript, specifically FScriptAnywhere which will let you determine the class and much other information about any visual element of any Cocoa program (and do a lot of other interesting things with Cocoa programs).
In addition to what toholio said, an easy way to get the look and feel of the bottom button bar is with BWToolkit.