recalculateKeyViewLoop doesn't work - cocoa

In my cocoa application I want to change the key order of my views. I fill in the nextKeyView reference for all subviews in the desired order. In the view's awakeFromNib function I do the following:
[[self.view window] setInitialFirstResponder:self.view];
[[self.view window] setAutorecalculatesKeyViewLoop:NO];
[[[self view] window] recalculateKeyViewLoop];
where the nextKeyView for the self.view is set to the first subview I want to appear in key order.
Nothing helps and the the key ordering stays default. How to resolve it?
Thanks

Calling recalculateKeyViewLoop forces the window to automatically calculate the key-view loop. You don't want to do this if you've set the key-view loop manually, as it does exactly what you've just told the window not to do in [[self.view window] setAutorecalculatesKeyViewLoop:NO].
You don't need any of this code. In Interface Builder, make sure your window has the "Auto Recalculates View Loop" checkbox un-checked, and connect the initialFirstResponder outlet of your window to the view that you want to be the initial first responder.
No code required.

following on from Rob's answer, in XCode 7.3 you can find the checkbox in the Interface Builder for the main window under:

Related

how do i stop NSTextField blocking right clicks for NSTableView rows?

As the title suggest, I've enabled right clicks for my tableview with customized rows. Anywhere there is an NSTextField, it blocks the right click.
is there a userInteractionEnabled equivalent for cocoa like on the iphone?
I though I probably needed to subclass everything in my NSTableCellView subclass, but I just needed to override -(NSView*)hitTest:(NSPoint)aPoint method in my NSTableCellView subclass to return self.
I'm able to right click on text fields within my custom view based table view cell. Here is how I configure it:
NSTextField *tf = [[NSTextField alloc] initWithFrame:NSZeroRect];
self.textField = tf;
self.imageView.autoresizingMask=NSViewWidthSizable;
self.textField.editable=NO;
self.textField.selectable=NO;
self.textField.drawsBackground=NO;
self.textField.bordered=NO;
self.textField.bezeled=NO;
self.textField.target=self;
self.textField.action=#selector(textDidEndEditing:);
[self.textField.cell setLineBreakMode:NSLineBreakByTruncatingMiddle];
Also, make sure you are setting the -menu property of NSTableView and not the cell view to enable to menu. (I don't know if that will make a difference to your issue but it is how I do right clicking in a table view.)

Cocoa: NSView drawRect painting over IBOutlets

I have an NSView in IB which sits above the app window. I have a subclass of NSView (AddSource) which I assign to the NSView.
On awakeFromNib I instantiate the view:
//add a new Add Source class
addSourceView = [[AddSource alloc] initWithFrame:NSMakeRect(0.0, 959.0, 307.0, 118.0)];
[[winMain contentView] addSubview:addSourceView];
in addSourceView's drawRect method I am adding a white background to the view:
[[NSColor whiteColor] set];
NSRectFill(rect);
[self setNeedsDisplay:YES];//added this to see if it might solve the problem
In winMain's contentView I have a NSButton that when clicked slides the addSourceView onto the window:
NSRect addSourceViewFrame = [addSourceView frame];
addSourceViewFrame.origin.y = 841.0;
[[addSourceView animator] setFrame:addSourceViewFrame];
But it seems as if the app is painting over the IBOutlets I placed on the NSView in IB. If, in IB, I repoistion the NSView so that it is on screen when the app launches everything works fine, the IBOutlets are there as well as the background color.
I'm not sure why this is happening. I've done this before with no problems. I must be doing something different this time.
Thanks for any help.
*note - on the 3rd screen capture, when I say this is what the app looks like when opened, that's when I hard code the Y position of the NSView. When it is functioning correctly it should open as screen capture 1.
Most likely your buttons and custom view are siblings, i.e. they are both subviews of your window's content view. Since siblings are "Stacked" depending on the order in which they are added, when you add the view in code it is being added on top of the buttons. You should be able to fix it by explicitly specifying where the view should be positioned relative to its new siblings like so:
[[winMain contentView] addSubview:addSourceView positioned:NSWindowBelow relativeTo:nil];
which should place it below any existing subviews of your window's content view. Also, remove the setNeedsDisplay: line in drawRect, that leads to unncessary, possibly infinite, redrawing.
EDIT: OK I see what you're doing.
I would suggest creating a standalove view in the NIB by dragging a "Custom View" object into the left hand side (the vertically-aligned archived objects section) and adding your controls there, that should ensure the controls are actualy subviews of the view, then you can just create a reference to the archived view in code, and add/remove it dynamically as needed.
Honestly though, you should probably be using a sheet for these kinds of modal dialogs. Why reinvent the wheel, and make your app uglier in the process?
You added TWO AddSource views to the window. You added one in IB - this view contains your textFields and buttons that are connected to the IBOutlets and it is positioned outside the window.
Then in -awakeFromNib you create another, blank AddSource view (containing nothing) and animate it into the window.
I can't recommend highly enough the Hillegass as the best introduction to IB and the correct way to build Cocoa Apps.
Also, Assertions can be useful to make sure what you think is happening is actually what is happening.
If you are certain you added a button to your view in IB, assert it is so:-
- (void)awakeFromNib {
NSAssert( myButton, #"did i hook up the outlet?");
}
NSAssert is a macro that has zero overhead in a release build.
Calling [self setNeedsDisplay:YES] from -drawRect just causes the same -drawRect to be called again. This will give you big problems.

beginSheet method not working for me

I have saveWindowController (NSWindowController subclass object). I use initWithWindowNibName: method to init the controller.
I set File's owner in xib to SaveWindowController. I connect delegate (from window) to File's owner and window (from controller) to NSWindow in xib file.
[NSApp beginSheet:[self.saveWindowController window]
modalForWindow:[self window]
modalDelegate:nil didEndSelector:nil contextInfo:nil];
After executing this method I see modal window without titlebar and it appears not like normal sheet. It just appears in left bottom corner of first window.
Could you help me, what I'm doing wrong?
Do you have your SaveWindowController's window set to "Visible at Launch" in the XIB? You must uncheck that option, or your window will try to display as soon as the XIB is loaded, and will not be positioned correctly.
I had a lot of trouble before realizing I declared my sheet window in IB without title bar. Does yours has one? If not, check the "title bar" option.
B.

NSView overlapping

My cocoa app has a "dashboard" style layout.
When the app starts, the main window contains 6 views which display graphs.
When I click on any part of the bottom most view, I have another NSView instance popping up
as an annotation. The problem I run into is that is the pop up NSView is large enough dimension wise,
the other views in the window overlap the pop up view. Currently, I do that with:
[[self superview] addSubview:annotationView ]; where 'superview' is the window.
Im not sure why this would be the case, I have tried removing the the pop up view from the "view stack"
and making it change positions but that didnt work.
[[[self window] contentView] insertView:popupView atIndex:0];
This will insert the view at the top level, if you still can't see it you will need to add a subview to the superview of the NSWindow's contentview.
If all the views are added as subviews of [self superview] then you must make sure that they do not overlap. Cocoa doesn’t guarantee correct behaviour in case there are overlapping sibling views.
If you want a popup view, consider using a child window instead. Since it’s a different window, the popup lies in a different view hierarchy, hence you won’t have the overlapping sibling views problem.
A good example of using child windows for additional information is Matt Gemmell’s MAAttachedWindow.

Set focus on a NSTextField in an NSMenu?

I have an NSMenu in the Mac statusbar, inside it I have a load of NSMenuItems and a custom view. Inside the custom view I have an NSTextField. I want to set the focus on the NSTextField when the menu is opened as in the Spotlight menu so the user can type straight away.
I have tried quite a few methods including:
[myTextField becomeFirstResponder];
and
[myTextField selectText: self];
[[myTextField currentEditor] setSelectedRange:NSMakeRange([[myTextField stringValue] length], 0)];
but none of them work.
Thanks
Alex
You were on the right track with the first one, but -becomeFirstResponder doesn't actually make your view the first responder--you have to call -[NSWindow makeFirstResponder:] for that.
Google suggests that NSMenus actually have an attached window. You have to use it very carefully, but it is safe to call makeFirstResponder: on it.
More information about this and how to take advantage of it here: https://web.archive.org/web/20171113100008/http://www.cocoabuilder.com/archive/cocoa/195835-set-focus-on-nsview-in-an-nsmenuitem.html

Resources