search

Home  >  Q&A  >  body text

objective-c - 这两个delegate的用法效果是一样的吗?

比如A,B两个view,值从A传到B。

在B里面写

A *av = [[A alloc]init];
[av setDelegate:self]

和在A里面写

B *bv = [[B alloc]init];
[self setDelegate:bv];

这句setDelegate要放哪里呢?viewDidLoad?没有报错,但是就是传值不成功。能给我一点提示吗?

代码

#import <Foundation/Foundation.h>

@protocol delegate <NSObject>

-(void)passString:(NSString *)string;

@end
#import <UIKit/UIKit.h>
#import "labelViewController.h"
#import "delegate.h"

@interface buttonViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak,nonatomic) id <delegate> delegate;

- (IBAction)buttonPress:(UIButton *)sender;

@end
#import "buttonViewController.h"

@interface buttonViewController ()

@end

@implementation buttonViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
	labelViewController *lv = [[labelViewController alloc]init];
	[self setDelegate:lv];
        [super viewDidLoad];

	// Do any additional setup after loading the view.
}

- (IBAction)buttonPress:(UIButton *)sender {

	[self.delegate passString:sender.currentTitle];
	[self performSegueWithIdentifier:@"push" sender:self];
}

@end
#import <UIKit/UIKit.h>
#import "delegate.h"


@interface labelViewController : UIViewController <delegate>

@property (weak, nonatomic) IBOutlet UILabel *label;

@end
#import "labelViewController.h"

@interface labelViewController ()

@end

@implementation labelViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}

- (void)passString:(NSString *)string{
	self.label.text = string;
		NSLog(@"%@",self.label.text);
}


@end
巴扎黑巴扎黑2769 days ago691

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-21 10:59:26

    should be

    A *av = [A alloc]init];
    [av setDelegate:self]

    Well, A in the question is a class name, how can I setDelegate

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-21 10:59:26

    I don’t quite understand what the question means. . . I suggest you post the complete requirements and code. I've used both of the code snippets you provided in different situations. . . Everything is possible
    First, I can only popularize the knowledge of delegate according to my understanding.
    Let me first explain what delgate is. The literal translation of delgate is called agency.
    An example of its function:
    Implement the code that controls class B in class A, and set the instance attribute delegate of class B to an instance of class A.

    @@implementation A
    - (void)viewDidLoad
    {
        UITableView *b;
        b.delegate = self; // 这个self就是A的实例
    }
    
    // 这就是一个Delgate方法,本来是控制UITableView的,但是在A中实现,通过UITableView的实例b的属性delegate联系起来。
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 44.f;
    }
    
    @end

    After understanding the principle of delegate, let’s look at the code in your question

    A *av = [[A alloc]init];
    [av setDelegate:self]

    is reasonable, but another way of writing, setting the Delegate to an initialized instance is more weird (although I have used it before, but it is a very special situation)
    Let’s talk about the issue of viewDidLoad. This function is called after the entire View is initialized and the loading is completed. Generally, you assign values ​​here and initialize subviews.

    Supplement

    There are a few questions
    1. Try not to use reserved words in the delegate class name, it looks too weird
    2. The usage of delegate is to define a delegate instance in LabelViewController and then call the delegate method
    3. The implementation of delegate should be ButtonViewController

    The code is as follows:

    LabelViewController.h

    #import <Foundation/Foundation.h>
    
    @protocol LabelDelegate <NSObject>
    
    -(NSString *)passString;
    
    @end
    
    @interface labelViewController : UIViewController <delegate>
    
    - (void)pass;
    
    @property (weak, nonatomic) IBOutlet UILabel *label;
    @property (assign, nonatomic) id<LabelDelegate> delegate;
    
    @end

    LabelViewController.m

    #import "LabelViewController.h"
    
    @interface LabelViewController ()
    
    @end
    
    @implementation LabelViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // Do any additional setup after loading the view.
    }
    
    - (void)pass {
        if ([self.delegate respondsToSelector:@selector(passString)]) {
            self.label.text = [self.delegate passString];
        }
    }
    
    @end

    ButtonViewController.h

    #import <UIKit/UIKit.h>
    #import "LabelViewController.h"
    
    @interface ButtonViewController : UIViewController <LabelDelegate>
    
    @property (weak, nonatomic) IBOutlet UIButton *button;
    @property (strong, nonatomic) LabelViewController *lv;
    
    - (IBAction)buttonPress:(UIButton *)sender;
    
    @end

    ButtonViewController.m

    #import "ButtonViewController.h"
    
    @interface ButtonViewController ()
    
    @end
    
    @implementation ButtonViewController
    
    @synthesize lv;
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
            [super viewDidLoad];
    
            self.lv = [[LabelViewController alloc] init];
            [lv setDelegate:self];
    
            // Do any additional setup after loading the view.
    }
    
    - (NSString *)passString
    {
        return self.button.currentTitle;
    }
    
    - (IBAction)buttonPress:(UIButton *)sender {
        [self.lv pass];
    }
    
    @end

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-21 10:59:26

    If you understand the meaning of delegate, you will know how to use delegate.

    To put it simply, something happened and I couldn’t handle it, so I needed to rely on external forces. For example, if I want to travel far away, it is definitely not practical to go. In this case, I need to use transportation, such as cars, trains, and airplanes. As long as these vehicles implement a certain protocol, it is ensured that there will be no errors when calling a method of the vehicle.

    If I take the initiative to set the delegate, such as: I.delegate = train. It's not wrong, but it makes no sense. The flexibility of delegate does not exist, and I am tied to a certain vehicle. So my .delegate needs to be set externally. For me, just executing my.delegate.go at the appropriate time works. It would be cool if the delegate was an airplane. If it was a Linke, just accept it.

    reply
    0
  • Cancelreply