需要 iOS 中委托的帮助
我很难理解代表们的情况。我尝试编写一个简单的示例来自学,但它不起作用。
我的简单测试有两个类。 A 类和 B 类。 ClassA 有委托定义。以及调用包含委托方法调用 (-(void)buttonPressed
) 的方法 (-(void)buttonHasBeenPressed:(id)sender
) 的按钮。 ClassB 设置为(我认为)侦听 ClassA 委托,并且当调用 -(void)buttonHasBeenPressed:(id)sender
时,它会将其写入控制台。
这是 AppDelegate:
.h:
#import <UIKit/UIKit.h>
@interface DelegateTestAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
.m:
#import "DelegateTestAppDelegate.h"
#import "ClassA.h"
@implementation DelegateTestAppDelegate
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ClassA *classA = [[ClassA alloc] init];
[self.window addSubview:[classA view]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
@end
这里是 ClassA:
.h:
#import <UIKit/UIKit.h>
@protocol ClassADelegate <NSObject>;
-(void)buttonPressed;
@end
@interface ClassA : UIViewController {
id <ClassADelegate> delegate;
}
@property (retain) id <ClassADelegate> delegate;
-(void)buttonHasBeenPressed:(id)sender;
@end
.m:
#import "ClassA.h"
@implementation ClassA
@synthesize delegate;
-(id)init {
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
return [self init];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonHasBeenPressed:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"hola" forState:UIControlStateNormal];
button.frame =CGRectMake(50, 200, 100, 20);
[self.view addSubview:button];
}
-(void)buttonHasBeenPressed:(id)sender {
[self.delegate buttonPressed];
NSLog(@"button has been pressed");
}
@end
这里是 ClassB:
.h:
#import <UIKit/UIKit.h>
#import "ClassA.h"
@interface ClassB : UIViewController <ClassADelegate> {
}
@end
.m:
#import "ClassB.h"
@implementation ClassB
-(id)init {
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
return [self init];
}
-(void)buttonPressed {
NSLog(@"delegating!");
}
@end
预期结果是“按钮已按下”和“委派!”单击“hola”按钮时写入控制台。
感谢您的帮助... 肖恩
I'm having a hard time getting my head around delegates. I tried putting together a simple example to teach myself, but it's just not working.
My simple test has two classes. ClassA and ClassB. ClassA has the delegate definition. As well as a button that calls a method (-(void)buttonHasBeenPressed:(id)sender
) that contains the delegate method call (-(void)buttonPressed
). ClassB is set (I think) to listen to the ClassA delegate and when the -(void)buttonHasBeenPressed:(id)sender
is called it writes out to the console.
Here's the AppDelegate:
.h:
#import <UIKit/UIKit.h>
@interface DelegateTestAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
.m:
#import "DelegateTestAppDelegate.h"
#import "ClassA.h"
@implementation DelegateTestAppDelegate
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ClassA *classA = [[ClassA alloc] init];
[self.window addSubview:[classA view]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
@end
Here's ClassA:
.h:
#import <UIKit/UIKit.h>
@protocol ClassADelegate <NSObject>;
-(void)buttonPressed;
@end
@interface ClassA : UIViewController {
id <ClassADelegate> delegate;
}
@property (retain) id <ClassADelegate> delegate;
-(void)buttonHasBeenPressed:(id)sender;
@end
.m:
#import "ClassA.h"
@implementation ClassA
@synthesize delegate;
-(id)init {
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
return [self init];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonHasBeenPressed:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"hola" forState:UIControlStateNormal];
button.frame =CGRectMake(50, 200, 100, 20);
[self.view addSubview:button];
}
-(void)buttonHasBeenPressed:(id)sender {
[self.delegate buttonPressed];
NSLog(@"button has been pressed");
}
@end
Here's ClassB:
.h:
#import <UIKit/UIKit.h>
#import "ClassA.h"
@interface ClassB : UIViewController <ClassADelegate> {
}
@end
.m:
#import "ClassB.h"
@implementation ClassB
-(id)init {
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
return [self init];
}
-(void)buttonPressed {
NSLog(@"delegating!");
}
@end
The expected result is to have both "button has been pressed" and "delegating!" written to the console when clicking on the "hola" button.
Thanks for your help...
Shawn
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 ClassB 实例在哪里?我在您发布的代码中找不到它。
另外,您在哪里将实例设置为 ClassA 作为其委托?我也找不到。
如果您不执行这两件事,您的委托将永远不会被调用。
Where is your ClassB's instance? I can't find it in your posted code.
Also, where do you set the instance into ClassA as its delegate at? I can't find it neither.
If you are not doing this two things, your delegate will never be invoked.
你忘记将delegate设置为符合ClassADelegate协议的类,这里应该是ClassB的对象
you forgot to set delegate to class which conforms to ClassADelegate protocol, here which should be an object of ClassB