【问题标题】:How do delegates work in objective c?代表如何在目标 c 中工作?
【发布时间】:2013-04-24 05:26:14
【问题描述】:

我正在尝试找出代表,因为我确实需要他们来完成我正在从事的项目,但对于我的一生,我无法弄清楚他们。无论我如何调整代码,都没有任何效果

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

- (IBAction)button:(id)sender 方法是连接到一个按钮,但是当我点击时没有输出。我做错了什么?

【问题讨论】:

标签: ios objective-c xcode delegates


【解决方案1】:

ViewController.h:

#import <UIKit/UIKit.h>

@protocol testDelegate
    -(void)sayHi;
@end

@interface ViewController : UIViewController 

@end

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:

#import "ViewController.h"

@interface DelegateController : UIViewController

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

@end

DelegateController.m:

#import "DelegateController.h"

@implementation DelegateController

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

@end

【讨论】:

    【解决方案2】:

    试试这个:

    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
    

    【讨论】:

    • 谢谢!所以基本上我做错的是在 ViewController.m 中,dc 需要是一个 ivar?
    • @user2314004:因为您正在使用来自另一个方法的委托调用,并且您正在将其分配给弱指针。所以我更喜欢 dc 应该是私有的 ivar
    • 如果 ViewController 正在创建 DelegateViewController,那么使用委托有什么意义?你可以在 DelegateViewController 中调用一个方法,因为你有一个指向它的指针。委托类不应该知道它的委托是什么类,这就是委托被声明为 id 的原因。在我看来,这种方法与通常的委托模式相反,在委托模式中,委托创建(或以其他方式获取对)委托对象的实例。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      用下面的方法替换你的按钮点击方法

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多