3

我正在尝试使用委托将一个简单的字符串从一个视图控制器发送到另一个视图控制器,但我的委托不起作用。我正在使用情节提要,不想使用 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

视图控制器A.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

视图控制器B.h

#import <UIKit/UIKit.h>
#import "ViewControllerA.h"

@interface ViewControllerB : UIViewController<viewControllerADelegate>

@end

视图控制器B.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未触发委托方法。

4

2 回答 2

4

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

例如,将委托代码移入并ViewControllerA添加ViewControllerBaUIButton和 anIBActionViewControllerB数据发送. ViewControllerA它是这样的:

视图控制器B.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

视图控制器B.m

#import "ViewControllerB.h"

@interface ViewControllerB ()

@end

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

在 中ViewControllerA,您首先需要ViewControllerB通过 segues 获取该推送对象。然后实现委托方法。

ViewControllerA.h

#import <UIKit/UIKit.h>
#import "ViewControllerB.h"

@interface ViewControllerA : UIViewController <viewControllerBDelegate>
- (IBAction)buttonPressed:(id)sender;

@end

视图控制器A.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

希望这将帮助您更好地了解与代表的沟通。

于 2012-06-21T06:53:10.990 回答
1

制作委托变量(强,非原子)而不是(__weak)。

于 2012-06-20T09:50:00.730 回答