我想發送一個簡單的字符串從一個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
未被觸發。
感謝您的explanation.But我很困惑如何發送使用委託標籤欄之間的數據,他們沒有塞格斯?如發送數據從第二個選項卡到第一個選項卡向後? 在此先感謝 –
這是另一個問題,我相信它需要一個單獨的帖子。但是,這幾乎是相同的,這也容易理解。你在這裏的問題,隨時可以接受:) –
在這裏我已經發布了關於標籤欄http:// stackoverflow的問題。com/questions/11133934 /傳遞數據之間選項卡使用委託不工作 如果解決問題,我將不勝感激。謝謝 :) –