File: node3.html

package info (click to toggle)
gnustep-tutorial 1.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 772 kB
  • ctags: 194
  • sloc: makefile: 46; perl: 6
file content (115 lines) | stat: -rw-r--r-- 3,754 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<!--Converted with LaTeX2HTML 99.2beta6 (1.42)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>Setting a delegate for the shared NSApplication instance</TITLE>
<META NAME="description" CONTENT="Setting a delegate for the shared NSApplication instance">
<META NAME="keywords" CONTENT="FirstGUIApplication">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="LaTeX2HTML v99.2beta6">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">

<LINK REL="STYLESHEET" HREF="FirstGUIApplication.css">

<LINK REL="next" HREF="node4.html">
<LINK REL="previous" HREF="node2.html">
<LINK REL="up" HREF="node1.html">
<LINK REL="next" HREF="node4.html">
</HEAD>

<BODY >
<!--Navigation Panel-->
<B> Next:</B> <A NAME="tex2html53"
  HREF="node4.html">The run loop of</A>
<B> Up:</B> <A NAME="tex2html51"
  HREF="node1.html">The shared application object</A>
<B> Previous:</B> <A NAME="tex2html45"
  HREF="node2.html">Creating the shared NSApplication</A>
<BR>
<BR>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION00012000000000000000">
Setting a delegate for the shared NSApplication instance</A>
</H2>
Once you have learnt how to create your application's shared instance,
the fastest way to develop an application is to set a
<I>delegate</I> for your shared application object.  This is done by using 
the method <TT>-setDelegate:</TT>, as follows:
<PRE>
id myObject;

// &lt;missing: create myObject&gt;
[NSApp setDelegate: myObject];
</PRE>
A delegate is an object of your choice which can customize the
behaviour of your application by implementing some (predefined)
methods.  For example, if you want your application to display a menu
(all apps should), you just need to implement in the delegate a method
called
<PRE>
- (void) applicationWillFinishLaunching: (NSNotification *)not;
</PRE>
(ignore the argument for now) and put the code to create the menu
inside this method.  Your shared application will check if the
delegate of your choice has implemented this method, and if so it will
run the method just before entering the main run loop.  The
documentation of <TT>NSApplication</TT> lists all the other methods
the delegate can implement to customize your application's behaviour;
we'll learn about some of them in other tutorials.

<P>
After you set the delegate of your application object, you need to run
your application; to do this, just invoke the function
<TT>NSApplicationMain ()</TT>, which does all for you.

<P>
To sum up, here is the code we are going to use: 
<PRE>
#include &lt;Foundation/Foundation.h&gt;
#include &lt;AppKit/AppKit.h&gt;

@interface MyDelegate : NSObject
- (void) applicationWillFinishLaunching: (NSNotification *)not;
@end

@implementation MyDelegate
- (void) applicationWillFinishLaunching: (NSNotification *)not
{
  // TODO - Create the menu here.
}
@end

int main (int argc, const char **argv)
{ 
  [NSApplication sharedApplication];
  [NSApp setDelegate: [MyDelegate new]];

  return NSApplicationMain (argc, argv);
}
</PRE>

<P>
<HR>
<!--Navigation Panel-->
<B> Next:</B> <A NAME="tex2html53"
  HREF="node4.html">The run loop of</A>
<B> Up:</B> <A NAME="tex2html51"
  HREF="node1.html">The shared application object</A>
<B> Previous:</B> <A NAME="tex2html45"
  HREF="node2.html">Creating the shared NSApplication</A>
<!--End of Navigation Panel-->
<ADDRESS>
Nicola Pero
2000-07-21
</ADDRESS>
</BODY>
</HTML>