需要 iOS 中委托的帮助

发布于 2024-12-01 03:17:25 字数 2885 浏览 0 评论 0原文

我很难理解代表们的情况。我尝试编写一个简单的示例来自学,但它不起作用。

我的简单测试有两个类。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

君勿笑 2024-12-08 03:17:25

你的 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.

旧时模样 2024-12-08 03:17:25

你忘记将delegate设置为符合ClassADelegate协议的类,这里应该是ClassB的对象

you forgot to set delegate to class which conforms to ClassADelegate protocol, here which should be an object of ClassB

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文