2012-06-20 20 views
3

我想發送一個簡單的字符串從一個viewcontroller到另一個viewcontroller使用委託,但我的代理不工作。我正在使用故事板,並且不想使用segue來傳遞數據。這是我簡單的代碼我的自定義viewcontroller委託不工作

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未被觸發。

回答

4

您的設計中的缺陷是嘗試將代表發送給您自己創建的對象的代表。你當然不需要委託,因爲你可以用簡單的屬性來完成。 myViewControllerBObject.dataProperty = @"some data"];就夠了。通常使用的委派是將數據發送回創建當前對象的控制器或應用程序中任何其他控制器的控制器。

例如,在移動ViewControllerA委託代碼ViewControllerB並添加UIButtonIBActionViewControllerB,將數據發送回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,你首先需要獲得的是保持通過塞格斯推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 

希望這將有助於你更好地瞭解與溝通代表的。

+0

感謝您的explanation.But我很困惑如何發送使用委託標籤欄之間的數據,他們沒有塞格斯?如發送數據從第二個選項卡到第一個選項卡向後? 在此先感謝 –

+0

這是另一個問題,我相信它需要一個單獨的帖子。但是,這幾乎是相同的,這也容易理解。你在這裏的問題,隨時可以接受:) –

+0

在這裏我已經發布了關於標籤欄http:// stackoverflow的問題。com/questions/11133934 /傳遞數據之間選項卡使用委託不工作 如果解決問題,我將不勝感激。謝謝 :) –

1

使委託變量(強,非原子)而不是(__weak)。

+0

使其@property(強,非原子)ID 委託 但沒有結果:( –