when I try to create an IBOutlet or an IBAction by linking my interface to the header file, I don't get the option to create one.
Yeah, this COULD be a duplicate but I havent found my answer yet on ANY other post!
Thanks :)
#property (assign) IBOutlet NSWindow *window;
- (IBAction)calculateClicked:(id)sender;
#property (weak) IBOutlet NSTextField *ATextField;
#property (weak) IBOutlet NSTextField *BTextField;
#property (weak) IBOutlet NSTextField *CTextField;
I'm not sure what kind of object you wanted to add your outlets/actions to, but one might infer from the presence of the window reference that you're trying to add that to your app delegate. In that case, you just need to make sure that your app delegate appears in the list of objects, and that you've specified the base class for that app delegate:
If, however, you were using some custom controller object, you would drag an generic object from the library to your NIB's list of objects. Then specify the custom controller class for that object (MyController in my example):
Having done that, when you drag your outlets/actions from the window to the assistant editor, in addition to the app delegate, your custom controller object interface/implementation files will be options that you can use.
My original answer was an iOS-centric answer. The above should describe the Cocoa equivalent. I'll keep this original answer here for reference.
Original answer:
In Interface Builder, make sure to specify the base class for the object you're linking the outlets to. If putting these outlets in a view controller, make sure your storyboard's scene has the view controller's class defined. And it's a little easier if your assistant editor is set to "automatic":
The above screen snapshot is relevant if you're using storyboards. If using NIBs, the idea is the same, but you need to make sure you set the NIB's file owner:
If your IBOutlet references are in a UIView subclass, you analogously have to specify the base class for your storyboard scene's view (or the NIB's view).
I've seen Xcode occasionally do this (bug). Closing/reopening Xcode has fixed it for me before.
Also, make sure that your interface file is Class is pointing to this class/header.
Related
I have a project that I got handed me (half working / half not working) and I have never seen something like to this before.
I have a storyboard and one of the scenes is called 'CompletionVC' and inside that scene I have this:
and in my header file for this scene I have this:
UIPickerViewDataSource,UIAlertViewDelegate>
{
IBOutlet UIButton *areaButton;
IBOutlet UIButton *unitButton;
IBOutlet UIView *searchView;
__weak IBOutlet UILabel *userLabel;
}
and none of those IBOutlets have any Referencing Outlets.
and in my .m file for this scene I have this:
- (IBAction)AreaButtonPressed:(UIButton *)sender
and this does not have a sentEvent assigned to it.
But somehow they are still appearing in my app, which is fine, but I want to adjust them and I cant find them anywhere....based off the information I provided, can someone give me a tip or two about where I these IBOutlets? I would like to make some adjustments, but I can't find them anywhere...also sometimes these IBOutlets have a Sent Event and Referencing Outlet, but it was referencing a file that is not there. The IBOutlets appear and are functional...weird eh?
I have created an extremely simple test program. It has one button. Clicking the button brings up an NSPopover with a label. That label is on a separate xib file with its own NSViewController.
The Goal is to, when on the main screen, when I click the button, a popover will show the xib file of the viewcontroller. And the label of the xib file should have it's text set to "It works".
Well.. It works, but only on after the second loading of the popover. On the first click of the button, the label still has its old default value. But from the 2nd click and onwards, "It works". Does any one have an idea what can be causing this issue? Its only about 5 lines of code The code can be seen on this repository --> https://github.com/patchthecode/testtest
Call [mainScreenPopoverViewController view]; in - (void)windowDidLoad method. This will load your view into memory.
Before call [mainScreenPopoverViewController view]; (textfield address is 0x0)
You should not use strong property for all IBOutlet.
#property (nonatomic, strong) IBOutlet NSTextField *textField;
Take a look at Resource Programming Guide
From a practical perspective, in iOS and OS X outlets should be
defined as declared properties. Outlets should generally be weak,
except for those from File’s Owner to top-level objects in a nib file
(or, in iOS, a storyboard scene) which should be strong. Outlets that
you create should therefore typically be weak, because:
Outlets that you create to subviews of a view controller’s view or a
window controller’s window, for example, are arbitrary references
between objects that do not imply ownership.
The strong outlets are frequently specified by framework classes (for example,
UIViewController’s view outlet, or NSWindowController’s window
outlet).
I created a custom view. It has a delegate that it will notify when some of its buttons are pressed.
I added this to the view controller's viewDidLoad:
self.myCustomView.delegate=self;
So far everything works fine.
But that's not cool enough. I want to do it the table view style where I can just right drag from the view to the File's Owner and (ta-da!) it's automatically set.
Add the IBOutlet keyword to the delegate property of your class:
#property (nonatomic, assign) IBOutlet id<MyViewDelegate> delegate;
The IBOutlet keyword tells the nib editor to let you make a connection to that property.
You could also say this:
#property (nonatomic, assign) IBOutlet MyView *delegate;
And then IB will only allow you to connect it to a MyView. But a protocol or id is more flexible.
EDIT: One point has been lost due to focus on IB
You said:
I created a custom view. It has a delegate that it will notify when some of its buttons are pressed.
Receiving button events is the role of a view controller, sometimes however a delegate will be used for finer grained control of certain events.
In general dont create a delegate for button presses on a view that is a primary window in which case you should be handling your button events in a ViewController. Buttons sit on a parent view that will forward events up the responder chain (you still need to connect them to a target). Im not saying you shouldn't ever use the delegate pattern for button presses but, buttons have their own connections to view controllers in IB with drag and drop as you wish. But there are situations like yours which are perfectly valid.
I created 2 View Controllers: on the first I created a button when click on it you will open another View Controller with button and label. I already created outlet and variable for them.
My question is: when I'm trying to connect the button\label with variable\outlet I can't see selection around the button\label -> I mean that I can't connect them all. Why?
P.S. DON'T PUT A MINUS I JUST NEW PROGRAMMER!
Can you post your IBOutlet declarations?
You have to declare UIButtons like this to make them work (connect) in Interface Builder :
#property (nonatomic, strong) IBOutlet UIButton *aButton;
For a label its just the same :
#property (nonatomic, strong) IBOutlet UILabel *aLabel;
I think you have either forgotten to write IBOutlet or your variable type is wrong.
Ok! I resolved the task.
I just needed to create a new Objective-C class, select that new class in Identity Inspector of the second View Controller and write all code in that new class.
I missed this one too.
For anyone still confused, select the View Controller (the yellow box) in your Storyboard and then under Inspector select the Identity Inspector. See images.
Now you will be able to connect up the IBOutlet items in your interface .h file.
Sorry if this has been asked before or it's a really dumb question, but I can't figure it out. I have an NSView in my interface, and I have created a subclass of NSView in Xcode. Then using the identity inspector, I set my NSView's class to be the newly created NSView subclass. The view draws fine, but now I need to redraw it to change a string inside the view. I'm pretty sure this has to do with setNeedsDisplay, but what do I send the message to? I don't have a particular instance of my view in the code, since it's in Interface Builder, so what do I do?
Again, sorry if this is dumb. I haven't done much with NSView yet. Ask for more info if you need it. Thanks!
In the view controller subclass you have, add an ivar with type of your NSView subclass. Declare a property on it, and mark it as an outlet.
// ViewControllerSubclass.h
ViewType *myView;
#property(readwrite, assign) IBOutlet ViewType *myView;
// ViewControllerSubclass.m
#synthesize myView;
Now you have an outlet, connect it to the view you designed via IB. To do so, right click in IB on your view controller subclass (the file's owner), you should see the outlet in the list.
Once you have done that, you are now able to send messages to the view in your code.
To mark the view as needing redraw :
[myView setNeedsDisplay:YES];