简体   繁体   中英

Need Help With Delegates in iOS

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

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.

你忘了将委托设置为符合ClassADelegate协议的类,这里应该是ClassB的一个对象

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM