How to use custom delegates in Objective-C
2011-11-22 10:18
387 查看
You will want to declare a delegate protocol for your class. An example of a delegate protocol and interface for class
look like this:
view plain@class Foo;
@protocol FooDelegate <NSObject>
@optional
- (BOOL)foo:(Foo *)foo willDoSomethingAnimated:(BOOL)flag;
- (void)foo:(Foo *)foo didDoSomethingAnimated:(BOOL)flag;
@end
@interface Foo : NSObject {
NSString *bar;
id <FooDelegate> delegate;
}
@property (nonatomic, retain) NSString *bar;
@property (nonatomic, assign) id <FooDelegate> delegate;
- (void)someAction;
@end
[/code]
Don't forget to synthesize your properties in the
What this code did was declare a protocol called FooDelegate; a class that conforms to this protocol would be declared like
it now gets to implement the methods under
require that these be implemented, use
of
The last step is for a
to be instantiated in the class that conforms to
and for this
to have its delegate property set:
view plainFoo *obj = [[Foo alloc] init];
[obj setDelegate:self];
[/code]
Now, your class is prepared to receive messages from
that have their delegates set correctly.
Foomight
look like this:
view plain@class Foo;
@protocol FooDelegate <NSObject>
@optional
- (BOOL)foo:(Foo *)foo willDoSomethingAnimated:(BOOL)flag;
- (void)foo:(Foo *)foo didDoSomethingAnimated:(BOOL)flag;
@end
@interface Foo : NSObject {
NSString *bar;
id <FooDelegate> delegate;
}
@property (nonatomic, retain) NSString *bar;
@property (nonatomic, assign) id <FooDelegate> delegate;
- (void)someAction;
@end
[/code]
Don't forget to synthesize your properties in the
@implementation.
What this code did was declare a protocol called FooDelegate; a class that conforms to this protocol would be declared like
@interface SomeClass : SuperClass <FooDelegate> {}. Because this class conforms to the protocol
FooDelegate,
it now gets to implement the methods under
FooDelegate(to
require that these be implemented, use
@requiredinstead
of
@optional).
The last step is for a
Fooobject
to be instantiated in the class that conforms to
FooDelegate,
and for this
Fooobject
to have its delegate property set:
view plainFoo *obj = [[Foo alloc] init];
[obj setDelegate:self];
[/code]
Now, your class is prepared to receive messages from
Fooobjects
that have their delegates set correctly.
相关文章推荐
- How to use custom delegates in Objective-C
- How to use isInEditMode() to see layout with custom View in the editor
- How to use custom type handler (typeHandler) in ibatis 2.3.4
- How to create custom methods for use in spring security expression language annotations
- How to Use FontAwesome in an Android App(把 icons 变得和 text 一样)
- How to use tinyfck in drupal
- How to use the date format that has been setup as default in Settings?
- How to Use PeekMessage() Correctly in Windows
- How to use ftp in a shell script
- Caffe-----ProtoBuffer ,How to use protobuffer in Ubuntu
- How to use Intel C++ Compiler in Visual Studio 2008
- How to use Azure Function in Microsoft Flow
- Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
- How to use Terminal in Vim
- How to Use HTML in Swing Components
- How To Use Proguard in Android APP
- How to use GCC to build DLL by DEF file in MinGW?
- How to build & install GLFW 3 and use it in a Linux project
- Know How And When To Use System.Message_Level To Control Messages In Oracle Forms
- Example of how to use both JDK 7 and JDK 8 in one build.--reference