比如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