[Cocoa] Target & Action, another method for responding to events

Source: Internet
Author: User

[Cocoa] Target & Action, another method for responding to events

We know that in cocoaProgramIf you want to process a window event or application event, you can use the delegate method to implement the RESPONSE event processing function, but if you want to handle a button control or many buttons-like controls in a window with a default action, do you want to implement the delegate for each of them? The answer is no, because for these controls with specified default behaviors, nscontrol has helped us to process them (this control must be inherited from nsactioncell, for example, if you add a button to a window, nscontrol has its own processing of the button click event, which is the target-action mode, when the Click Event of the button is triggered, nscontrol checks whether there is a corresponding target in the control. What is the action that the target processes for this event, if both target and action exist, the click event will be processed directly by nscontrol.CodeAs follows (implementation of false settings)
-(Void) mouseclicked ()
{
If (target! = Nil & Action! = Nil & [target respondstoselector: Action])
{
[Target employee mselector: Action withobject: Self];
}
}
The following code demonstrates this technology.
# Import <Cocoa/cocoa. h>

@ Interface mycontroller: nsobject
{
}
-(Void) onbuttonclicked :( ID) sender;
@ End

@ Implementation mycontroller
-(Void) onbuttonclicked :( ID) sender
{
[Nsapp terminate: nsapp];
}
@ End

Int main (INT argc, char * argv [])
{
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
[Nsapplication sharedapplication];

// Create Main Window
Nsrect rc = nsmakerect (0, 0,800,600 );
Nsuinteger uistyle = nstitledwindowmask | nsresizablewindowmask | nsclosablewindowmask;
Nsbackingstoretype backingstorestyle = nsbackingstorebuffered;
Nswindow * win = [[nswindow alloc] initwithcontentrect: RC stylemask: uistyle backing: backingstorestyle defer: No];
[Win settitle: @ "hellowin test"];
// Create a button and set it as content View
Nsbutton * button = [[nsbutton alloc] initwithframe: nsmakerect (200,200, 60, 40)];
[Button settitle: @ "Quit application"];
[Win setcontentview: button];
// Set Target and Action
Mycontroller * controller = [[mycontroller alloc] init];
[Button settarget: controller];
[Button setaction: @ selector (onbuttonclicked :)];
[Win center]; // Center Main Window
[Win makekeyandorderfront: Win];
[Win makemainwindow];

[Nsapp run];
[Button release];
[Win release];
[Pool drain];

Return 0;
}

The above code first creates a window, and then creates a button in the window. This button is displayed as the contentview of the window, because nsbutton is inherited from nsview, so it is also an instance of nsview. After the button object is created, we set the target object for the button object to process the default event, and then set the method to process the event through setaction, note that the action method must be responded by the target, that is, the call that complies with the respondstoselector: method.

The implementation of this target object is put in a class called mycontroller, which implements a method called onbuttonclicked:. This method is used as an action method, so it has a parameter (ID) sender. This is because when the action method is called, the caller will pass the object that triggers this action to the action method through this parameter. The implementation in this example is very simple. You can directly call the terminate: Method of the nsapp object to exit the application.

The end of this example is different from the previous one. I called the release method of the button and window respectively, because the window and button objects are allocated by reference, therefore, when you no longer need to use it, you need to release the resources they occupy. OBJ-C 2.0 provides the garbage colletion function. The objects you allocate will be automatically released when they are not used. However, if you can understand the allocation and release modes of cocoa objects, it is of great benefit to understand the implementation of the entire cocoa framework.

In the above example, we also need to note the size of the button. You can see that in the Code, the size of the rect allocated to the button is 60x40, however, when running, you will find that the button is full of the content area of windows, because this button serves as the contentview of windows, the Size Policy of window contentview changes the height and width of the parent window. If we

[Win setcontentview: button];

Comment out the code and replace it with the following

[[Win contentview] addsubview: button];

The button is correctly located in the window, but the position and size of the button are not changed when you change the window size, this is determined by its sizing policy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.