모달 뷰 컨트롤러를 제공하고 모달 VC에서 어떤 단추를 눌렀는지에 따라 문자열을 다시 전송하는 간단한 프로젝트가 있습니다. iTunes U에서 Stanford 수업을 시청하면서이 모든 것을 기반으로했습니다. 모든 것이 정확하지만, 몇 가지 컴파일러 경고가 표시됩니다. 의 각각에, 두 줄을모달 뷰 컨트롤러 데이터 전송을위한 대리자 메서드 구현
먼저 나는 네 개의 경고가 Invalid receiver type 'id <MyModalViewControllerDelegate>*'
전화를받을 TransferViewController.m
두 번째에 passing argument 1 of 'setDelegate:' from incompatible pointer type
라는 하나 얻을 수 있지만 이러한 MyModalViewController.m
오히려 옆에 위반 라인, 빌드 결과 영역에 표시되지 않습니다 버튼 동작. 여기
// TransferViewController.h
#import <UIKit/UIKit.h>
#import "MyModalViewController.h";
@interface TransferViewController : UIViewController <MyModalViewControllerDelegate> {
UILabel *label;
UIButton *button;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) UIButton *button;
- (IBAction)updateText;
@end
// TransferViewController.m
#import "TransferViewController.h"
@implementation TransferViewController
@synthesize label;
@synthesize button;
- (IBAction)updateText {
MyModalViewController *myModalViewController = [[MyModalViewController alloc] init];
myModalViewController.delegate = self; // I get the warning here.
[self presentModalViewController:myModalViewController animated:YES];
[myModalViewController release];
}
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog {
label.text = selectedDog;
[self dismissModalViewControllerAnimated:YES];
}
@end
// MyModalViewController.h
#import <UIKit/UIKit.h>
@protocol MyModalViewControllerDelegate;
@interface MyModalViewController : UIViewController {
UIButton *abby;
UIButton *zion;
id <MyModalViewControllerDelegate> delegate;
}
@property (assign) id <MyModalViewControllerDelegate> delegate;
- (IBAction)selectedAbby;
- (IBAction)selectedZion;
@end
@protocol MyModalViewControllerDelegate <NSObject>
@optional
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog;
@end
// MyModalViewController.m
#import "MyModalViewController.h"
@implementation MyModalViewController
@synthesize delegate;
- (IBAction)selectedAbby {
if ([self.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:@"Abby"];
}
}
- (IBAction)selectedZion {
if ([self.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:@"Zion"];
}
}
감사합니다! 나는 대의원을 사용하는 개념을 이해한다고 말했을 때 다른 질문에 거짓말했다고 생각하지만 지금은 그렇게 생각합니다. 부모 VC에서 대리자 메서드를 정의 할 때 부모에서 구현되므로 부모의 ivars에 액세스 할 수 있습니다. 모달 VC가 그 방법을 사용할 때, 모달로 되돌아가는 도관을 얻습니다. – Steve
잘못된 유형 문제에 관해서는, 이것이 Objective-C가 너무 관대 한 비트 케이스 중 하나라고 생각합니다. 델리게이트를 포인터로 잘못 정의하게되어 기뻤지 만 실제로 그렇게 사용하려고했을 때만 화가났다. 컴파일러의 경고에서 내가 어떻게 정의했는지에 대한 힌트였다. 어쩌면 xCode 4에! – Steve
그래, 당신이 그것을 얻을 것 같습니다 :) 대리인이 여전히 개체를 가리 키므로, SomeDelegateClass * delegate를 말하면 여전히 *가 필요합니다. id는 이미이 'inside' 따라서 명시 적으로 작성할 필요가 없습니다. –