代表们如何在目标c中工作?

时间:2020-12-18 15:02:49

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:

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:

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:

DelegateController.h:

#import "ViewController.h"

@interface DelegateController : UIViewController <testDelegate>

@end

DelegateController.m:

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?

- (IBAction)按钮:(id)发送方法连接到一个按钮,但单击时我没有输出。我究竟做错了什么?

4 个解决方案

#1


0  

Try this:

尝试这个:

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 ()
@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

DelegateController.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");
    }
    return self;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
}

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

@end

#2


1  

ViewController.h:

ViewController.h:

#import <UIKit/UIKit.h>

@protocol testDelegate
    -(void)sayHi;
@end

@interface ViewController : UIViewController 

@end

ViewController.m:

ViewController.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

DelegateController.h:

DelegateController.h:

#import "ViewController.h"

@interface DelegateController : UIViewController

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

@end

DelegateController.m:

DelegateController.m:

#import "DelegateController.h"

@implementation DelegateController

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

@end

#3


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.

您的问题是ViewController创建了DelegateController,然后在DelegateController中创建一个新的,不同的ViewController实例,并将自己设置为该实例的委托。通常,您设置委托的方式是DelegateController将创建ViewController的实例并将其自身设置为委托。这假定先创建DelegateController,但具体如何执行此操作取决于您的应用程序结构,以及您如何从控制器移动到控制器。

#4


0  

Replace you button Click method with following

替换您按钮单击以下方法

- (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)];
    }
}

#1


0  

Try this:

尝试这个:

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 ()
@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

DelegateController.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");
    }
    return self;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
}

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

@end

#2


1  

ViewController.h:

ViewController.h:

#import <UIKit/UIKit.h>

@protocol testDelegate
    -(void)sayHi;
@end

@interface ViewController : UIViewController 

@end

ViewController.m:

ViewController.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

DelegateController.h:

DelegateController.h:

#import "ViewController.h"

@interface DelegateController : UIViewController

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

@end

DelegateController.m:

DelegateController.m:

#import "DelegateController.h"

@implementation DelegateController

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

@end

#3


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.

您的问题是ViewController创建了DelegateController,然后在DelegateController中创建一个新的,不同的ViewController实例,并将自己设置为该实例的委托。通常,您设置委托的方式是DelegateController将创建ViewController的实例并将其自身设置为委托。这假定先创建DelegateController,但具体如何执行此操作取决于您的应用程序结构,以及您如何从控制器移动到控制器。

#4


0  

Replace you button Click method with following

替换您按钮单击以下方法

- (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)];
    }
}