比如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
should be
A *av = [A alloc]init]; [av setDelegate:self]
Well, A in the question is a class name, how can I setDelegate
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中實現(xiàn),通過UITableView的實例b的屬性delegate聯(lián)系起來。 - (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.
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
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.