Cocoa : How to make a control invisible? - cocoa

In my application I have some controls like NSButton and NSTextfield I want to turn invisible.
I know I can do it n the Interface Builder but I need to do it in the code.
I still haven't found the right message I need to send to the controls.

[myView setHidden:YES];

Actually, this will also disable the control. If you want the control to still work, such as for an NSButton, [button setTransparent:YES] is the one to use.

Related

How to animate the insert of a UIBarButtonItem

I have a simple app with a Tool Bar at the bottom. Depending upon the context of what the user is doing, I may want various buttons visible or not in the Tool Bar. So, in my code, I remove the weak modifier for the IBOutlet reference to those buttons, and I remove the non-visible buttons in viewDidLoad. Great.
Now, when the context changes, I add/remove buttons as desired using
toolBar.items?.removeAtIndex(0)
toolBar.items?.insert(playButton, atIndex: 0)
Great, works well. But for aesthetics, I would like to have the button removal/insert animated. Apparently the animated:YES argument from Objective-C days is gone. Do you know, does the API support animation of the button shuffling? Or, if not, how best to accomplish this?
Thanks!

Subclass NSMenu

This is a simple question...
Just: How would I go about subclassing NSMenu? As I'm not sure about how to go about doing it.
I'd like to subclass it in order to do custom drawing eg. change background colour etc.
This is normally done with - (void)drawRect:(NSRect)dirtyRect thus I tried subclassing and using that method, to no avail.
You can't do this by subclassing NSMenu. You want to use NSMenuItem's setView: to do custom drawing in a menu. Take a look at the MenuItemView sample code for an example of how it's done and what you can do with it.

Cocoa forControlEvents:WHATGOESHERE

In Cocoa/Objective-C if I have created a button programmatically, what do I put in for my control event?
[btnMakeChar addTarget:self action:#selector(makeChar:) forControlEvents:WHAT GOES HERE?];
In iOS you can write it like so forControlEvents: UIControlEventTouchUpInside
I can't find anything to show what I would use for just cocoa, not cocoa touch
I'm not sure if I understand you correctly, but if you're programming a Mac,
[theHappyButton setTarget:self];
[theHappyButton setAction:#selector(doStuff)];
it is two separate lines, rather than the one combined line of code on an iPhone.
I hope that is what you were after??
To find it in the doco: choose on the 10.6 doco (not iOS) and search on "setAction:". You'll see it in NSControl Class Reference. NSButton is of course a subclass of NSControl.
The method you're asking about does not exist in Cocoa, so nothing goes there. Cocoa controls have a single target with a single action, and either use a different addTarget:-type method for each kind of action or expect a delegate object that will handle all the events they generate.

Switch XIB Views - Cocoa Mac

I am pretty new to coding and would like to know how to switch XIB views on a button click. IS there also anyway to add animation when switching?
Thanks,
Kevin
this is totally possible, but there are a few things you'll need to do. I imagine you are already familiar with connecting outlets to objects in your XIB, so the first thing you need to do is create the custom views in your XIB and connect them to outlets in your appDelegate. I suggest that one of the views be dragged into the window and one one be outside the window. That way, when the window loads, it already has one of your custom views as a subview. This just makes it easier to get started.
Then you're going to write an IBAction in the appDelegate and connect it to your button. Assuming that one of the custom views is already being hosted by the window, the IBAction should send a replaceSubviewWith message to the window's contentView animator like this [[window.contentView animator] replaceSubview:firstView with:secondView]; where firstView and secondView are the pointer/outlets that you declared and connected to the views in your XIB.
This is sending the animator proxy of the window's content view a message which tells it to replace the old subview with the new one. The reason for sending the message to the view's animator proxy (and not the view itself) is that the transition will be carried out with the deafult CATransitionAnimation. Because you want it to be animated, right?
The reason why you shouldn't remove one subview and then add another is because animating the removal of a subview is actually quite tricky and requires the implementation of the delegate method animationDidEnd. This is because executing an animation on a view that has been removed from the view heirarchy does not make sense. I don't know why Apple hasn't changed this, but for now it will be one of the enduring quirks of CoreAnimation.
Let me know if that helps. I am happy to clarify! And welcome to Cocoa!
An easy way to do this is to use a tabless NSTabView- you can lay everything out in IB so the pain is minimal.

Creating a checkbox programmatically using Cocoa

I need to create a checkbox programmatically in Cocoa and when I try and make a button with buttonType set to NSSwitchButton it displays the title, but does not show the button as a checkbox. I think I am missing something but I can't find any resources about making things like checkboxes without using the Xcode GUI.
The question is a little old so you've probably already figured it out, but I found it while searching for this exact thing. Alex danced around the solution without actually providing it. So here, for Google and all mankind: how to programmatically create a checkbox in Cocoa.
NSRect frame;
frame.size.width = frame.size.height = 18;
NSButton *myCheckBox = [[NSButton alloc] initWithFrame:frame];
[myCheckBox setButtonType:NSSwitchButton];
[myCheckBox setBezelStyle:0]; // This is unnecessary. I include it to show that checkboxes don't have a bezel style.
[myView addSubview:myCheckBox];
I don't think buttons are bezeled by default when created programmatically. Check the setBezelStyle: method, as well as setBezeled: and setBordered:. One of those should give you what you want.
I had failed to execute setImagePosition properly and this was causing the checkbox not to display.

Resources