I am new to using interface builder. Generally I create stuff programmatically but for this project in particular I have to use it, because I am working with a guy that just knows how to work with IB.
Said, that, I have to create a NSObject class and I want to create outlets from objects on interface builder to that class. Dragging the New Referencing Outlet (+) from interface builder to the class header is not doing the trick.
How do I do that?
In your class *.h add an outlet to the inferface, e.g.
IBOutlet NSButton *connectToThisButton; //choose the name you like
In IB CONTROL-drag from your button to the class, where your IBOutlet is defined. Leave the mousebutton and select connectToThisButton. Maybe you have to save it in XCode, before you can do this in IB.
Define a method for action after the end of the interface-section:
( IBAction) doSomeActionWithButton:(id)sender; //choose the name you like
In IB CONTROL-drag from your class to the button, where your IBOutlet is defined.
Related
I have this OSX storyboard-based application that starts with a NSSplitViewController like this:
This splitViewController has two viewControllers: master and detail.
Inside the window I have a NSToolbar. I dragged a NSProgressIndicator to that toolbar and Xcode embedded it inside a NSToolbarItem.
Now I need to create an outlet (not an action as explained on other stackoverflow questions) from the NSProgressIndicator to some class. First question is which one?
Xcode will not let I create an outlet. I have tried these options:
dragged from the ToolbarItem to masterController class file, detailController class file and to NSSplitViewController class.
dragged from the ToolbarItem to the delegate class.
dragged from the NSProgressIndicator to masterController class file, detailController class file and to NSSplitViewController class.
dragged from the NSProgressIndicator to the delegate class.
dragged from both the NSToolbarItem and from the NSProgressIndicator to the Window Controller First Responder.
In all cases dragging does not make a window appear to allow me to create the outlet.
For heavens sake, how do I create an outlet like that? To which class I drag it and how do I do that?
I'll assume your setup is more like this image:
Your Window scene is backed, by default, by an NSWindowController, to which you cannot add new outlets. You need to create a subclass of that, associate it with your Window and then you should be able to create outlets in that.
File > New File > Cocoa Class
Specify a name like "SpaceDogsWindowController", as a subclass of NSWindowController.
Then use select the window controller icon (blue circle) and select the Identity Inspector in Xcode. (CMD+ALT+3). Specify the name of your new class in the "Class" field.
Then try to hookup an outlet:
1) Show the Assistant Editor
2) Use the Jump Bar to ensure your custom class is visible (It's at the top of the assistant editor pane, it should say Automatic and you can tap that to then select your new class; If it says 'Manual', change it to Automatic).
3) If your are control-dragging and it's still not offering to make a connection, try dragging from the document outline (also shown in the screen shot).
You could then edit that progress indicator from other view controllers, which are descendants of that window's view hierarchy, using code like this:
if let windowController = self.view.window?.windowController() as? CustomWindowController {
windowController.progressIndicator.doubleValue = 0.4
}
or, in Objective-C, something like this:
CustomWindowController *myWindowControllerSubclass = self.view.window.windowController;
windowController.progressIndicator.doubleValue = 0.4;
Hope that helps.
I'd like to create a custom NSTableCellView instantiated by Interface Builder. I've set my Table Cell View class to MyTableCellView, and properly created MyTableCellView : NSTableCellView .m/.h files.
However, I just can't CTRL+Drag a simple button from inside this view to MyTableCellView.h in order to create an IBOutlet.
Here is a video to show what happens: http://youtu.be/sNNbuVT-SZs.
How the view is subclassed:
How I try to CTRL+Drag a button
Also, sometimes, Interface Builder just don't allow the cell view's class to be modified. What's happening ?
I finally found a solution, that is a little weird but works as expected. Instead of connecting the NSButton to MyTableCellView header directly, I used the reversed path:
Manually create an outlet:
#property(retain, nonatomic) IBOutlet NSButton* button;
Then click the empty circle on the left, and drag it to your XIB file's button:
I have no idea why it works this way, please let me know if you know the anwser.
I'm fairly new to Xcode and Cocoa development, so please excuse me if I use the wrong terminology.
I have a window which has an NSTableView bound to a NSArrayController. The NSTableView has some predefined columns and the NSArrayController is populated with data when my window is loaded.
This all works fine, but I now need to re-use the functionality in a number of other windows.
After much reading I think and NSViewController is what I need, to provide an object that I can re-use in different windows in multiple .xib.
So what I have done is created a new NSViewController sub class in Xcode which also created a new xib for that controller.
The xib contains a custom view, where I have placed my NSTableView.
#interface KeyViewController : NSViewController
#property (weak) IBOutlet NSTableView *keyTable;
#end
The files owner class is set to the KeyViewController, and the view outlet is set to the files owner.
I then placed a ViewController object into the original window and hooked up the view to a new custom view in the window
I then set the nib name for the ViewController in its properties.
The new view never gets displayed and the view controller initWithNibName never gets called.
Am I missing something vital or missing the plot completely. Should you be able to do this just with interface builder or do I need to alloc and init the view in code?
If I do have to do it in code, whats the purpose of the ViewController object in IB and the Nib Name property for it?
Thanks..
From my understanding the delegating class is the one which always inherits from NSResponder class and delegate is only available to classes that inherits from NSResponder. NSMenu inherits directly from NSObject, so then why do i see deletgate in the outlets?
Or is it that i misunderstood the options that you see after ctrl-click on an object in Interface Builder? I see Outlets, Referencing outlets, Received Actions , what does these mean then?
I am using Xcode 4 (10.7.3), if that matters.
I have no idea where you saw this but it's completely and totally incorrect. Delegation is just a pattern, there are no restrictions on the type of objects that can implement a delegate.
In fact, you are encouraged to use the delegation pattern in your own classes.
NSMenu declares its delegate property as an outlet in the header, which is why you see it in Interface Builder. You can do the same in your own code like so:
#interface YourObject : NSObject {}
#property (weak) IBOutlet id delegate;
#end
In Interface Builder, Outlets refers to the outlets that are defined in your class's header and that you can connect to other objects. Referencing Outlets and Referencing Actions connections from other objects to your object.
I have added a XIB based UIViewController to my solution and dragged some UIViews into it.
Now I want some of the views not to be UIView but RoundedRectView
(https://github.com/Krumelur/RoundedRectView) which inherits from UIView.
How to achieve this? I tried to change the class in Interface Builder but that did nothing. Then I manually modified the designer.cs file but that resulted in a failure.
Then I tried modifying the fake ObjC code but that failed too.
(I'm using Xcode 4.2 and MD 2.8.6.4)
Your view subclass needs at least two things:
Register attribute
[Register("MyView")]
public class MyView : UIView {}
IntPtr constructor
public MyView(IntPtr handle) : base(handle) {}
You then open the XIB in Interface Builder, add a UIView and set its Class in the Identity Inspector to the name you passed to the Register attribute. When you connect it to an outlet, you will see that it has the correct type.