2011-12-02 2 views
0

시나리오는 주소록을 호출하려는보기가 두 개 이상 있습니다. 따라서 각보기에서 위임자 코드를 복제하지 않기 위해 App Delegate의 머리글 및 .m 파일에 코드를 배치했지만 2 개의 해당 응용 프로그램의 맨 아래에 "@interface AddressBookDelegate"및 "@implementation AddressBookDelegate"를 사용했습니다peoplePickerDelegate가 대리인의 주소를받지 못하는 이유는 무엇입니까?

addressBookDelegate = (AddressBookDelegate *) [[UIApplication sharedApplication] delegate]; 

ABPeoplePickerNavigationController *abPicker = [[ABPeoplePickerNavigationController alloc]init];  
abPicker.peoplePickerDelegate = self.addressBookDelegate; 
[self presentModalViewController:abPicker animated:YES]; 
[abPicker release]; 

주소록은 모든보기에 잘 표시 위임 나는 다음과 같은 코드가 그럼 내보기에

@implementation AddressBookDelegate 
@synthesize addressBookDelegate; 

- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{ 
    [super dismissModalViewControllerAnimated:YES]; 

    ...get stuff from the Address Book... 

    return NO; 
} 

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
          property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier 
{ 
    return NO; 
} 

@interface AddressBookDelegate : UIViewController <ABPeoplePickerNavigationControllerDelegate> { 
AddressBookDelegate *addressBookDelegate; 
} 
@property (nonatomic, retain) AddressBookDelegate *addressBookDelegate; 
@end 

을 fiies-. 인식 할 수없는 선택기 인스턴스에 전송

깨끗 컴파일 : [MyprogAppDelegate peoplePickerNavigationControllerDidCancel이 :] - 나는 주소와 같은 예약의 취소 버튼을 대리자를 호출하는 어떤 사용자 조치를 취할 때, 나는

가 crash- , 경고.

peoplePickerDelegate가 물리적으로보기 자체와 동일한 파일에 있지 않을 때 주소 대리인 코드에 연결하려면 어떻게 연결해야합니까? 고마워.

ADDED 참고 : 나는 디버거를 사용하고보기 코드의 라인

abPicker.peoplePickerDelegate = addressBookDelegate; 

에 중지 할 때, 나는 addressBookDelegate의 주소가 MyprogAppDelegate의 주소가 아닌 AddressBookDelegate 등으로 언급되는 것을 볼 수 나는 예상 했었을 것이다. 따라서 App Delegate 파일 내에서 주소록 위임 코드의 위치가 바뀌 었다고 생각하게됩니다.

AddressBookDelegate Cancel Delegate 코드가 AddressBookDelegate에 1000 바이트라고 말하면 내 응용 프로그램은 실제로 MyprogAppDelegate에 코드 1000 바이트를 "입력"하므로 충돌이 발생합니다. 그래서 어떻게 든 AddressBookDelegate의 주소 지정을 올바르게 설정하지 않습니다. 그것은 어쨌든 그것을 받아들입니다 ...

답변

0

결국 : 이제

는 기능을 구현합니다. 시간을 절약하고 계속 움직여서 각보기에서 코드를 복제해야했습니다. 나는 그것이 그것을 끝내는 것보다 더 많은 객체 기반 방식으로 행해질 수 있다고 확신하기 때문에이 또 다른 시간을 다시 방문 할 것이다.

1

귀하의 코드는 귀하의 appdelegate (MyprogAppDelegate)가 peoplePickerNavigationControllerDidCancel 메소드를 구현한다고 가정합니다.

그래서, MyprogAppDelegate에있는 당신의 코드는 다음과 같이해야한다 :

@implementation MyprogAppDelegate 
@synthesize ...; 

#pragma mark - 
#pragma mark Application lifecycle 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  


    return YES; 
} 

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{ 

} 
+0

미안하다. 질문에 추가했습니다. 따라서 App Delegate IS에있는 사람들 선택기 위임 코드가 있습니다. 내 주소를 지정하는 데 문제가 있습니다. Pls는 질문의 맨 끝에 추가 된 텍스트를 적어 두었습니다. – Ric

0

편집 좋아, 전체 첫 번째 대답은 밖으로 던져되고있다. 이것은 약간의 경고와 함께, 어둠 속에서 여전히 약간의 발사이지만, 나는 이것이 도움에 더 가까워 질 것이라고 생각한다. 아이디어 중 일부는 이월합니다.

  1. 당신 매우 아마 당신의 ABPeoplePickerNavigationControllerDelegate 역할을하는 별도의 클래스가 필요하지 않습니다. 아마도 그 코드는 하단에 코드가있는 클래스와 같아야합니다. (그 코드는 presentModalViewController:animated:입니다. 컨트롤러가 무엇인지 알 수 없으므로 참조 용으로 MyViewController이라고합니다. 원하는 이유는 무엇입니까? 보기 컨트롤러를 대리자로 사용하는 것은 대리자 메서드에서 주소록이있는 모달보기 컨트롤러를 닫을 수 있어야하기 때문입니다.

  2. 사용자가 인 경우 프로그램의 UIApplicationDelegateABPeoplePickerNavigationControllerDelegate이 아니길 바란다. 자신이 말했듯이 peoplePickerDelegateUIViewController이어야합니다.

따라서, MyViewController. 첫째, 인터페이스 :

/* MyViewController.h */ 

@interface MyViewController : UIViewController<ABPeoplePickerNavigationControllerDelegate> 
... 
@end 

(테이블 뷰 컨트롤러 또는 그런 일 등)의 UIViewController의 후손에서 상속 할 수 컨트롤러 -는 ABPeoplePickerNavigationControllerDelegate에 추가되고 변경해야하는 유일한 일을 변경하지 마십시오 그 구현 된 프로토콜 목록. 내가 기대로 위의 제안 중 하나를 수행 할 수 없었다

/* MyViewController.m */ 

@implementation MyViewController 

... 
- (void) whateverMethodIsDisplayingTheAddressBook 
{ 
    ABPeoplePickerNavigationController *abPicker = [[ABPeoplePickerNavigationController alloc]init];  
    abPicker.peoplePickerDelegate = self; // This view controller is the delegate 
    [self presentModalViewController:abPicker animated:YES]; 
    [abPicker release]; 
} 

... 

- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
     shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{ 
    [super dismissModalViewControllerAnimated:YES]; 

    ...get stuff from the Address Book... 

    return NO; 
} 

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
     shouldContinueAfterSelectingPerson:(ABRecordRef)person 
           property:(ABPropertyID)property 
           identifier:(ABMultiValueIdentifier)identifier 
{ 
    return NO; 
} 

@end 
+0

내가 보여주는 개념을 구현하려고하기 때문에 응답이 지연되었습니다. 나는 큰 그림을 이해하지만 당신이 의미하는 바를 코드화하면 깨끗한 컴파일에서 10 개의 오류로 바뀝니다. 그런 다음 MyprogAppDelegate를 MyprogAppDelegate에 통합하여 MyprogAppDelegate가 NSObject에서 파생되었으며 dismissModalViewControllerAnimated 메서드가 UIViewController 개체에서 파생되어야한다는 것을 알았습니다. 다시 구조로 돌아가서 내가 육체가 될 수 있는지 확인합니다. 나는 당신의 "잘못된 행동"에 대한 예를 들어 본 적이 없다는 것을 이해합니다. 당신이 @interfact AddressBookDelegate의 기능을 복제 어디 당신의 abouve struction에서 – Ric

+0

표시되지 않는 :의 UIViewController를 ABPeoplePickerNavigationControllerDelegate 프로토콜이있는 UIViewController에서 파생되어야하기 때문이다. 나에게 좀 더 자세히 설명해 주겠다. 나는 그에게 시간을주지 않고, 경박하지 않습니다. – Ric

+0

더 많은 시간을 문서를보고 나서 다시 작성했습니다. 가장 큰 변화는 AppDelegate를 전혀 사용하면 안되며 사람들 선택 관리자가 주소록을 생성하는보기 컨트롤러를 만드는 것입니다. – Matt