2012-06-20 2 views
3

하나의 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이 트리거되지 않습니다.

답변

4

디자인의 결함은 사용자가 직접 만든 개체에 대리인과 함께 데이터를 보내 보는 것입니다. 간단한 속성으로 할 수 있으므로 위임자는 필요하지 않습니다. myViewControllerBObject.dataProperty = @"some data"];이면 충분합니다. 위임은 대개 현재 객체를 만든 컨트롤러 나 앱의 다른 컨트롤러로 데이터를 다시 보냅니다.

예를 들어, ViewControllerBViewControllerA에 위임 코드를 이동하고 UIButtonIBAction다시ViewControllerA에 데이터를 보낼 것이다 ViewControllerB에 추가합니다. 여기 간다 :

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 

#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 객체를 밀어 얻을 필요가 ViewControllerB.m

을. 그런 다음 대리자 메서드를 구현하십시오. 이 도움이 될 것입니다

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

감사? 같은 두 번째 탭에서 첫 번째 탭으로 데이터를 전송 하시겠습니까? 미리 감사드립니다. –

+0

그게 또 다른 질문이고, 별도의 게시물이 필요합니다. 그럼에도 불구하고, 거의 이것과 거의 같고 이해하기도 쉽습니다. 당신이 여기에 있던 문제는 그것을 받아 들일 자유롭게 :) –

+0

여기에 나는 탭 모음 http : // stackoverflow에 관한 질문을 게시했다.com/questions/11133934/passing-data-between-tab-using-delegate-not-working 문제를 해결해 주시면 감사하겠습니다. 감사 :) –

1

(__weak) 대신 대리자 변수 (강하고 비 구조)를 만듭니다.

+0

그것을 @property했다 (강한 비 원자) ID 대표 하지만 결과 :(그들이 segues을 해달라고으로 내가 위임을 사용하여 탭 막대 사이에 데이터를 전송하는 방법 혼란 스러워요 당신의 explanation.But에 대한 –

관련 문제