Disable NSToolbar customisation via window's toolbar button? - objective-c

I am looking to disable the Command + Click combination on a toolbar button (located top-right) in a Cocoa window. I would still like to let the user show and hide the toolbar, but I do not want them to be able to select a different display mode (e.g. small icons, no icons, etc).
Has anyone found a way to do this? Thanks in advance.

You don't need to subclass NSToolbar to do this. In your NSWindowController subclass, put the following in your awakeFromNib:
- (void) awakeFromNib
{
NSToolbar *tb = [[self window] toolbar];
[tb setAllowsUserCustomization:NO];
}
You also have the added benefit of avoiding private API use.

Have you tried using a custom NSToolbar subclass that overrides setDisplayMode: and setSizeMode: to do nothing? That won't remove the menu items of course, or the UI in the customization sheet (assuming you aren't disabling that as well with setAllowsUserCustomization:), but it might prevent them from doing anything.

Related

NSTextView sometimes get grayed out and unable to interact with

I have a NSView inside of my "menubar". I have a button where which i click on and add a new NSMenuItem to the menu. However when i run this code which is inside my custom view in the init method sometimes the view will get grayed out and I'm unable to select it. Any ideas what this could be the cause of this. The problem seems to affect all created NSMenuItems after this problem occur.
-(id)initWithFrame:(NSRect)frameRect andTag:(int)tagz{
textfeild = [[NSTextView alloc]initWithFrame:CGRectMake(19,1 , 110, 18)];
[textfeild setFont:[NSFont fontWithName:#"Helvetica" size:15]];
[textfeild setString:[NSString stringWithFormat:#"Notespace %d",tagz]];
[textfeild selectAll:self];
[self addSubview:textfeild];
}
I don't think you are supposed to use [self addSubview:] on a menu item. Instead, you are supposed to create an NSView and place your custom view inside it. Documentation is here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MenuList/Articles/ViewsInMenuItems.html
However, according to the documentation views inside a menu item may not receive keyboard events. It may work sometimes, but since the documentation specifically mentions it you should not place any view that needs keyboard events inside a menu.
What you should do instead, is avoid NSMenu altogether and simply create an NSWindow where the menu would normally appear. You can make your window look/behave like a menu.

UIBarButtonItem created in Interface Builder not working - confused

I am trying to tidy up my UI by consolidating various things in a Tool Bar, and am utterly confused. I am using Interface Builder rather than constructing the controls programmatically, because my UI is fairly simple and not particularly dynamic.
What I did did so far:
Added an empty tool bar.
Dragged two previously existing and working buttons onto the tool bar. They changed their class from UIButton
to UIBarButtonItem, and the inspector now shows them as having no
Sent Actions or Referencing Outlet, but the the previous action &
outlet in the View Controller - responding to taps, setting the
label of the button - still work.
Created a new Button directly
in the tool bar. Wired up its action & outlet by ctrl-drag in the
normal way. The inspector shows the Action and Outlet for this
button as connected, which is nice, but sadly neither of them works.
Clicking the button does not invoke the action; setting the label of
the button does not cause anything to happen on the screen, even
after I tried prodding the tool bar with a setNeedsDisplay.
I'm not sure what to try next. Googling has shown me that I'm not the only person to find using UIToolBar via Interface Bulder difficult and confusing, but I haven't found a solution to my exact problem.
I don't particularly want to resort to creating my entire GUI programmatically just to tidy up a few buttons. Creating all the controls in Interface Builder outside the tool bar, getting them wired up and working, then moving them into the tool bar would presumably also work - but it would be a kludge, and would leave me still none the wiser if anything went wrong later.
Should you try using UIBarButtonItem instead of UIButton? It works for me.
i had a similar issue.
Did you created an extra UITapGestureRecognizer for root view ?
Maybe for something like > When elsewhere than UITextView clicked, resignFirstResponder for all UITextViews !
In my case, on 7.1, that extra UITapGestureRecognizer prevented transfer of event to IBAction of UIBarButtonItem which is inside an UIToolBar.
IBAction was made on Storyboard by Ctrl+Drag.
on 8.1 it was working. Not on 7.1
Read some suggestions to create an extra UIView and putting all visual elements into that extra UIView except UIToolBar

MAAttachedWindow for NSStatusItem without a custom view

I have a simple app that has a NSStatusItem, which only displays an icon.
I would now like to add functionality that would make a MAAttachedWindow appear under the NSStatusItem.
I saw the demo code Matt Gemmel provided; the code he uses to make the MAAttachedWindow appear under the NSStatusItem is:
NSRect frame = [[self window] frame];
NSPoint pt = NSMakePoint(NSMidX(frame), NSMinY(frame));
[controller toggleAttachedWindowAtPoint:pt];
The above is done in the custom view of the NSStatusItem. However, my NSStatusItem has no custom view. How can I add the MAAttachedWindow in my case?
You can't afaik. You have to have a way to get coordinates to attach the window to, and the only way i've been able to get that to work is to use a custom view so you can get the coordinates on mouse down and the only way i've seen is to use your own view. Anything else would probably be a little hacky unless there is some way to get the view for a status item without a custom view and it wouldn't be good to display the MSAttachedWindow and a menu.
I just settled on doing a NSStatusitem with a custom view and faking selection by drawing a background gradient when its selected.
Have you considered using NSPopover ?

Selecting text programmatically in menu view

I write an "Agent" Cocoa app where I have TextField in status icon's menu, it looks like this:
(source: ifotos.pl)
And in couple of places I select its contents programmatically (e.g. in -(BOOL)becomeFirstResponder of my NSTextField subclass)
And it doesn't work. It surely has something to do with the fact that it's in menu, not in window. But how do I fix that?
Because your view is in a menu, it's possible that the textfield isn't responding because the run loop is not in its default mode. Try calling selectText: like this:
[textField performSelector:#selector(selectText:) withObject:nil
afterDelay:0.0 inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
Why don't you just use a window instead? Menus are implemented as windows under the hood: you can do the same thing, just position and style your window appropriately.
Edit: answer largely rewritten

Accessing a TabBarItem from within one of the ViewControllers

i'm searching for a way to disable a TabBarItem from within the ViewController of one of the TabBars Tabs. I'm even not sure if this is about the View Hierarchy or what to really search for. I tried a lot but didn't come up with a solution.
Right now I worked arround it by saving a reference to the tabbar in a singelton object when instanciating the tabbar (inside the apps delegate). But I don't think this is the best way to do it.
Thanks and Cheers,
Nils
I had the same problem and I have found a solution (but I find it a little bit dirty)
In your ViewController where you want to disable the TabBarItem, create a variable UITabBarController *myTabBarController; with property.
In the class where you manage your Tabbarcontroller put this code in the viewDidLoad:
MyViewController * vc = (MyViewController *)[navigationController.viewControllers objectAtIndex:0];// O the index of the first view controller replace it with the index of the needed viewController
vc.myTabBarController = myTabBarController; // pass your TabBarController to your ViewController
Now back to your ViewController class (MyViewController) and use this code to disable the tabBarItem:
UITabBarItem *mapTabBarItem= [[myTabBarController.tabBar items] objectAtIndex:1]; // I want to desable the second tab for example (index 1)
[mapTabBarItem setEnabled:NO];
Note that this is not the best way to do it, but it works :-)
Maybe I don't understand what you are wanting, but I have dissabled a tab bar button by going into interface builder, then selecting the tab button i want to work on (small blue highlight color only on the place holder for the picture, the small box with a ? on it) then -> Tools Menu -> Inspector -> Tab Bar Item Properties (first tab on left in the Inspector) -> then a small radio button on the bottom says "Enabled" -> deselect this. Works for me, as I use this as a simple load screen on the first tab. Hope that helps.
Kirk