Application Menu Items Xcode - xcode

I am building a simple app in xcode 3, but don't know how to code the appmenu items. I have a few simple items that need codes - open and print. Close and minimise are done automatically. Can anybody tell me how to programme these items so that they can be actioned in the app?

basically you need to declare you actions/functions as IBActions so you can link them in the Interface Builder.
for example, you declare you functions like:
-(IBAction) doSomething:(id)sender{
//do something code here
}
The IBAction is really just a nil, but it tells the Interface builder that this is available for linking. The (id)sender tells XCode which object triggered the action.
Hard to explain without screenshots, but here is a quick tutorial for you:
http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/002-MenuApp.pl

Related

Make "Close All" (cmd+option+W) apply only to one type of document windows

I have an application that manages different types of NSDocument subclasses (along with matching NSWindow subclasses).
For instance, it's possible that the app has one window of type A open, and two windows of type B.
Now, if a window of type B is active, and the user chooses "Close All" or hits cmd+option+W, all my app's windows are sent the close message.
But I only want all of the active window type's windows closed instead, i.e. only the two type B, not the type A window. How do I accomplish this?
I currently have no explicit menu entry for "Close All". Instead, macOS provides that automagically. If there perhaps a way to intercept a "closeAll" message? Can't find one, though.
AppKit will add the Close All menu item if there isn't one. Add an alternate menu item item with key equivalent cmd+option+W below the Close menu and connect it to your own action method.
You might succeed with overriding your document's canClose(withDelegate:,shouldClose:,contextInfo:) to return whether a document should be closed.
If this doesn't behave the way you want, you can create a subclass of NSDocumentController (if you don't have one already). Details on how to do that vary, but usually you have main (menu) XIB or main Storyboard, which has a "Document Controller" object: set its class to your custom class.
Then override closeAllDocuments(withDelegate:,didCloseAllSelector:,contextInfo:) and implement your custom logic.
Note that you should detect whether your app is about to quit and then really do close all you documents (unless you really want to prevent the app quit, e.g. because a document is dirty).
After some digging I figured out where the auto-generated "Close All" menu item sends its action to: To the closeAll: selector of the application target:
Thus my solution is to subclass NSApplication and implement the handler there, which then simply closes all windows that are of the same type (this assumes that I use specific subclasses for my different types of windows):
- (IBAction)closeAll:(id)sender {
Class windowClass = self.keyWindow.class;
for (NSWindow *w in self.windows) {
if (windowClass == w.class) {
[w performClose:sender];
}
}
}
Caution: If you adopt this pattern be aware that:
The closeAll: selector is not documented nor mentioned in the header files, meaning that Apple might feel free to change in a future SDK, though I find that unlikely. It will probably not break anything if that happens, but instead your custom handler won't be called any more.
The code simply tells all windows to close, ignoring the fact that one might reject to be closed, e.g. by user interaction. In that case you may want to stop the loop instead of continuing to close more windows (though I know of no easy way to accomplish that).

How to see Print Debugging While Interface Builder Previews? IOS / XCode

When Interface Builder shows a preview of your ViewController, it must execute the code belonging to the views contained in that VC in order to draw them, right?
So... let's say you have some print debugging in your custom view...
print("draw() was executed")
... How can one see this output while Interace Builder is processing the preview?
Thanks!
As far as I have seen, there is no way of printing to the debug console when Interface Builder is executing your code. However, I did find it helpful to temporarily add messages to a label or textview instead.
If necessary, you can do conditional code that only execute when Interface Builder is running (or not) like below. That way, you can show a small message view that is only visible at design time, and otherwise hidden.
#if TARGET_INTERFACE_BUILDER
// IB only
#endif

How to create multiple windows using "command + n" in non document based application

Is there a way to create/enable having multiple windows using "command + n" in a non document based application? I want to have unlimited instance of that window (not actually unlimited, but might be 6-7 instances) using command + n
Or I have to create a document based app and port all my code in new project template is the only solution?
I can see the menu button for "New" is disabled right now.
A few ways to do this.
First connect the New menu item to an IBAction method.
Name the method whatever makes sense to you.
Next, you will want to add some kind of property to your controller ( app delegate for simplicity ) that is basically a window stack only storing a reference to each window or window controller. NSMutableArray should do nicely.
Now you can do the next part a few ways, but I would recommend creating an NSWindowController subclass with a nib/xib (especially if these windows will have the same basic things in them).
Do what you want in the nib file.
Now in your IBAction method, create a new instance of your window controller class, add it to your mutable array. Tell it to load its window.
You only have to decide if the controller should be removed from the stack and set to nil if its window is closed.
Many ways to handle that, and up to your design to know what is correct.
Try this :-
NSWindowController *yourWindow=[[[[yourWindowController alloc]init]retain]autorelease];
[yourWindow loadWindow];

Define the class of the sender when making an IBAction connection in Xcode 4,2.1

In Xcode 4 or above, it has a handy function allowing us to CTRL + drag an object from the interface to the .h file to quickly connect the object with an event method (assume the Assistant Editor is enabled).
Say we have an UIButton in the interface, and we want to add an IBAction for its "touch up inside", we enable the assistant window and press/hold CTRL + Drag the button to the .h file to quickly generate the necessary codes.
In the popup prompt box, say we set "connection" as "Action".
In the "Type" drop-down, we can select "id" or "UIButton". <--- this is where my problem is.
The strange thing in Xcode 4.2.1 is: no matter what I select, it always generates code: "(id)sender" as the argument.
I know it is easy to manually change it to "(UIButton *) sender", but what is the point of this drop-down when it always generates "(id)"?
Is this is a bug of Xcode or am I missing something to make it directly generate the code "(UIButton *) sender" when I select "UIButton" in this drop-down?
Edited on 27/Feb/2012: This is confirmed solved in Xcode 4.3
- (void)action:(id)sender is just the way actions are defined. you can, in theory, connect different UI elements to the same action. after you've created the connection, you can manually change id to whatever class you want, or just do a cast inside the method.

What is the name of this Mac OS X control?

Does this control have a name? Or is it just a bunch of simple controls merged together? If so, what controls are they?
http://img8.imageshack.us/img8/3002/picture2xrb.png
It looks like an NSTableView with an a custom cell type and no column header. Have a look at the documentation for NSTableView's tableView:dataCellForTableColumn:row:. For columns which have the same type for all rows you may also set the cell class in interface builder.
I doubt the search box is part of the same control.
You could open the Application's Nib file to see what is in there. Look inside the application bundle. If the application is called Example then you should be able to find the Nib at Example.app/Contents/Resources/English.lproj/MainMenu.nib.
The best tool for investigating this is fscript, specifically FScriptAnywhere which will let you determine the class and much other information about any visual element of any Cocoa program (and do a lot of other interesting things with Cocoa programs).
In addition to what toholio said, an easy way to get the look and feel of the bottom button bar is with BWToolkit.

Resources