我正在尝试使用委托(delegate)将一个简单的字符串从一个 View Controller 发送到另一个 View Controller ,但我的委托(delegate)不起作用。我正在使用 Storyboard,不想使用 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
我是不是错过了什么傻事?委托(delegate)方法 -(void) addItem: (ViewControllerA *)controller data:(NSString *)item
没有被触发。
最佳答案
您设计中的缺陷是尝试使用委托(delegate)将数据发送到您自己创建的对象。您当然不需要委托(delegate),因为您可以使用简单的属性来完成。 myViewControllerBObject.dataProperty = @"some data"];
就足够了。委托(delegate)通常用于将数据发送回创建当前对象的 Controller 或您应用中某处的任何其他 Controller 。
例如将ViewControllerA
中的delegate代码移到ViewControllerB
中,并添加一个UIButton
和一个IBAction
ViewControllerB
会将数据返回到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
对象。然后实现委托(delegate)方法。
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
希望这能帮助您更好地理解与委托(delegate)的沟通。
关于iphone - 我的自定义 View Controller 委托(delegate)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11115913/