하나의 viewcontroller에서 다른 viewcontroller로 위임자를 사용하여 간단한 문자열을 보내려고하지만 대리자가 작동하지 않습니다. 나는 storyboard를 사용하고 있고 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
이 트리거되지 않습니다.
감사? 같은 두 번째 탭에서 첫 번째 탭으로 데이터를 전송 하시겠습니까? 미리 감사드립니다. –
그게 또 다른 질문이고, 별도의 게시물이 필요합니다. 그럼에도 불구하고, 거의 이것과 거의 같고 이해하기도 쉽습니다. 당신이 여기에 있던 문제는 그것을 받아 들일 자유롭게 :) –
여기에 나는 탭 모음 http : // stackoverflow에 관한 질문을 게시했다.com/questions/11133934/passing-data-between-tab-using-delegate-not-working 문제를 해결해 주시면 감사하겠습니다. 감사 :) –