搜尋

首頁  >  問答  >  主體

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
巴扎黑巴扎黑2865 天前722

全部回覆(3)我來回復

  • 伊谢尔伦

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

    應該是

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

    吧,問題裡A是個類名,怎麼會setDelegate呢

    回覆
    0
  • 大家讲道理

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

    沒太看懂問題什麼意思。 。 。建議你把完整的需求和程式碼貼一下。你提供的兩個程式碼片段,在不同情況下我都用過。 。 。都是可以的
    先只能按照我的理解普及一下delegate的知識。
    先解釋delgate是什麼東西,delgate直譯過來叫代理。
    作用舉例:
    在類別A中實作控制類別B的程式碼,把類別B的實例屬性delegate設成類別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

    了解了delegate的原理,再看你問題裡的程式碼

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

    是合理的,而另一種寫法,把Delegate設成某個初始化的實例就比較詭異了(雖然我曾經有這麼用過,但是是很特殊的情況)
    再說viewDidLoad的問題,這個函數是在整個View初始化後,載入完成,呼叫的。一般就在這裡賦值,初始化subviews。

    補充

    有這麼多問題
    1,delegate的類別名稱盡量不要用保留字,看起來太詭異了
    2,delegate的用法是,在LabelViewController裡定義一個delegate實例,然後呼叫delegate的方法
    3,實作delegate的應該是ButtonViewController

    程式碼如下:

    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

    回覆
    0
  • 巴扎黑

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

    如果理解了delegate的意思,就知道如何使用delegate了。

    簡單來說,就是某件事情發生了,我處理不了,需要藉由外在力量才行。好比我要出門,走肯定不切實際,這時就要藉助交通工具,如:汽車、火車、飛機。只要這些交通工具都實現了某個協議,確保在呼叫該交通工具的某個方法時不會出錯。

    如果我主動去設定delegate,如:我.delegate = 火車。沒錯,但沒什麼意義,delegate的彈性就不存在了,我被某個交通工具綁死了。所以 我.delegate 需要在外部設定。對我來說,只要在適當的時候執行 我.delegate.go 就行了。要是delegate是飛機就爽了,是一輛臨客就認了吧。

    回覆
    0
  • 取消回覆