1

I'm trying to figure out delegates because I really need them for a project I'm working on, but for the life of me, I can't figure them out. No matter how much I tweak the code, nothing works

ViewController.h:

#import <UIKit/UIKit.h>

@class ViewController;

@protocol testDelegate
-(void)sayHi;
@end

@interface ViewController : UIViewController 

- (IBAction)button:(id)sender;
@property (weak, nonatomic)id <testDelegate> delegate;

@end

ViewController.m:

#import "ViewController.h"
#import "DelegateController.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
DelegateController *dc = [[DelegateController alloc] init];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)button:(id)sender {
[self.delegate sayHi];
}
@end

DelegateController.h:

#import "ViewController.h"

@interface DelegateController : UIViewController <testDelegate>

@end

DelegateController.m:

#import "DelegateController.h"

@interface DelegateController ()

@end

@implementation DelegateController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"init");
        ViewController *vc = [[ViewController alloc] init];
        [vc setDelegate:self];
}
    return self;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)sayHi
{
    NSLog(@"hi");
}


@end

The - (IBAction)button:(id)sender method is connect to a button, but when clicked I get no output. What am I doing wrong?

4

4 回答 4

1

视图控制器.h:

#import <UIKit/UIKit.h>

@protocol testDelegate
    -(void)sayHi;
@end

@interface ViewController : UIViewController 

@end

视图控制器.m:

#import "ViewController.h"
#import "DelegateController.h"

@interface ViewController () <testDelegate>

@end

@implementation ViewController

- (IBAction)pushNewViewController:(id)sender
{
    DelegateController *dc = [[DelegateController alloc] init];
    dc.delegate = self;
    [self.navigationController pushViewController:dc animated:YES];
}

- (void)sayHi
{
    NSLog(@"It works!");
}
@end

委托控制器.h:

#import "ViewController.h"

@interface DelegateController : UIViewController

@property (weak, nonatomic) id<testDelegate> delegate;

@end

委托控制器.m:

#import "DelegateController.h"

@implementation DelegateController

- (IBAction)button:(id)sender
{
    if([_delegate respondsToSelector:@selector(sayHi)]) {
        [_delegate performSelector:@selector(sayHi)];
    }
}

@end
于 2013-04-24T05:53:29.713 回答
0

尝试这个:

视图控制器.h

#import <UIKit/UIKit.h>

@class ViewController;

@protocol testDelegate
-(void)sayHi;
@end

@interface ViewController : UIViewController 

- (IBAction)button:(id)sender;
@property (weak, nonatomic)id <testDelegate> delegate;

@end

视图控制器.m

#import "ViewController.h"
#import "DelegateController.h"


@interface ViewController ()
@property (nonatomic, strong) DelegateController *dc;
@end

@implementation ViewController

- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
  _dc = [[DelegateController alloc] init];
  [self setDelegate:_dc];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)button:(id)sender
{
   [self.delegate sayHi];
}
@end

委托控制器.h

@interface DelegateController : UIViewController <testDelegate>

@end

委托控制器.m

#import "DelegateController.h"

@interface DelegateController ()

@end

@implementation DelegateController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        NSLog(@"init");
    }
    return self;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)sayHi
{
    NSLog(@"hi");
}

@end
于 2013-04-24T05:45:03.257 回答
0

Your problem is that ViewController creates DelegateController, then in DelegateController you're creating a new, different, instance of ViewController, and setting yourself as the delegate of that instance. Normally, the way you set a delegate is that DelegateController would create an instance of ViewController and set itself as the delegate. This assumes that DelegateController is created first, but exactly how to do this depends on your app structure, and how you move from controller to controller.

于 2013-04-24T05:32:25.950 回答
0

将您的按钮单击方法替换为以下

- (IBAction)button:(id)sender {
    // Check whether your Delegate method is implemeted or not
    if([delegate respondsToSelector:@selector(sayHi)])
    {
        // Call Delegate Method
        [delegate performSelector:@selector(sayHi)];
    }
}
于 2013-04-24T05:39:33.227 回答