我的自定义viewcontroller委托不工作

我想发送一个简单的string从一个viewcontroller到另一个viewcontroller使用委托,但我的代理不起作用。 我正在使用故事板,并不想使用segue来传递数据。 这是我简单的代码

ViewControllerA.h

#import <UIKit/UIKit.h> @class ViewControllerA; @protocol viewControllerADelegate <NSObject> -(void) addItem: (ViewControllerA *)controller data:(NSString *)item; @end @interface ViewControllerA : UIViewController{ __weak id <viewControllerADelegate> delegate; } - (IBAction)buttonPressed:(id)sender; @property (weak) id<viewControllerADelegate> delegate; @end 

ViewControllerA.m

 #import "ViewControllerA.h" @interface ViewControllerA () @end @implementation ViewControllerA @synthesize delegate; - (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)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (IBAction)buttonPressed:(id)sender { [self.delegate addItem:self data:@"this is data"]; } @end 

ViewControllerB.h

 #import <UIKit/UIKit.h> #import "ViewControllerA.h" @interface ViewControllerB : UIViewController<viewControllerADelegate> @end 

ViewControllerB.m

 #import "ViewControllerB.h" #import "ViewControllerA.h" @interface ViewControllerB () @end @implementation ViewControllerB - (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. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; ViewControllerA *vca = [storyboard instantiateViewControllerWithIdentifier:@"A"]; [vca setDelegate:self]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(void) addItem: (ViewControllerA *)controller data:(NSString *)item{ NSLog(@"delegate function called"); } @end 

我错过了一些愚蠢的东西吗? 委托方法-(void) addItem: (ViewControllerA *)controller data:(NSString *)item不被触发。

你的devise中的缺陷是尝试发送与代表你自己创build的对象的数据。 你当然不需要委托,因为你可以用简单的属性来做到这一点。 myViewControllerBObject.dataProperty = @"some data"]; 简直就够了。 通常使用的委派是将数据发送回创build当前对象的控制器,或者发送到应用程序中任何其他控制器的控制器。

例如,将ViewControllerA的委托代码移动到ViewControllerB然后向ViewControllerB添加一个UIButton和一个IBAction ,将数据发送 ViewControllerA 。 这里是:

ViewControllerB.h

 #import <UIKit/UIKit.h> @class ViewControllerB; @protocol viewControllerBDelegate <NSObject> -(void) sendDataBack: (ViewControllerB *)controller data:(NSString *)item; @end @interface ViewControllerB : UIViewController { __unsafe_unretained id <viewControllerBDelegate> delegate; } @property (nonatomic, assign) id<viewControllerBDelegate> delegate; - (IBAction)buttonTouch:(id)sender; @end 

ViewControllerB.m

 #import "ViewControllerB.h" @interface ViewControllerB () @end @implementation ViewControllerB @synthesize delegate; .... - (IBAction)buttonTouch:(id)sender { [self.delegate sendDataBack:self data:@"my data"]; } @end 

而在ViewControllerA ,您首先需要通过segues获取推送的ViewControllerB对象。 然后执行委托方法。

ViewControllerA.h

 #import <UIKit/UIKit.h> #import "ViewControllerB.h" @interface ViewControllerA : UIViewController <viewControllerBDelegate> - (IBAction)buttonPressed:(id)sender; @end 

ViewControllerA.m

 #import "ViewControllerA.h" @interface ViewControllerA () @end @implementation ViewControllerA ...... - (IBAction)buttonPressed:(id)sender { } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSLog(@"id %@", [segue destinationViewController]); ViewControllerB *vcb = (ViewControllerB *)[segue destinationViewController]; vcb.delegate = self; //key point } - (void)sendDataBack:(ViewControllerB *)controller data:(NSString *)item { NSLog(@"Here is the data %@ and the controller it comes from %@", item, controller); } @end 

希望这能帮助你更好地理解与代表的沟通。

使委托variables(强,非primefaces)而不是(__weak)。