我的自定义viewcontroller委托无法正常工作

时间:2012-06-20 08:48:23

标签: iphone objective-c ios delegates storyboard

我正在尝试使用委托将一个简单的字符串从一个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

2 个答案:

答案 0 :(得分:4)

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

例如,将ViewControllerA中的委托代码移至ViewControllerB,然后将UIButtonIBAction添加到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对象。然后实现委托方法。

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

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

答案 1 :(得分:1)

使委托变量(强,非原子)而不是(__ weak)。