问题描述
比如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
问题解答
回答1:应该是
A *av = [A alloc]init];[av setDelegate:self]
吧,问题里A是个类名,怎么能setDelegate呢
回答2:没太看明白问题什么意思。。。建议你把完整的需求和代码贴一下。你提供的两个代码片段,在不同情况下我都用过。。。都是可以的先只能按照我的理解普及一下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回答3:
如果理解了delegate的意思,就知道如何使用delegate了。
简单来说,就是某件事情发生了,我处理不了,需要借助外部力量才行。好比我要出远门,走肯定不现实,这时就要借助交通工具,如:汽车、火车、飞机。只要这些交通工具都实现了某个协议,确保调用该交通工具的某个方法时不会出错。
如果我主动去设置delegate,如:我.delegate = 火车。没有错,但没什么意义,delegate的灵活性就不存在了,我被某个交通工具绑死了。所以 我.delegate 需要在外部设置。对我来说,只要在适当的时候执行 我.delegate.go 就行了。要是delegate是飞机就爽了,是一辆临客就认了吧。