2011-12-02 35 views
0

這種情況是我有多個想要調用地址簿的視圖。爲了不在每個視圖中重複委託的代碼,我在App Delegate的頭文件和.m文件中找到了代碼,但是在2個相應的應用程序的底部使用了「@interface AddressBookDelegate」和「@implementation AddressBookDelegate」代表fiies-爲什麼peoplePickerDelegate沒有獲得代表的地址?

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

@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; 
} 

然後在我的意見,我有以下代碼:

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

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

通訊簿顯示在所有視圖罰款。但是,當我採取會調用代表用戶進行任何操作,如地址簿中的取消按鈕,我crash-

- [MyprogAppDelegate peoplePickerNavigationControllerDidCancel:]:無法識別的選擇發送到實例

它編譯乾淨,沒有警告。

如何連接PeoplePickerDelegate以連接到地址委託代碼,當它與物理視圖本身不在同一個文件中時?謝謝。

補充說明:當我使用調試器和停止在視圖代碼行

abPicker.peoplePickerDelegate = addressBookDelegate; 

,我看到地址addressBookDelegate據稱是MyprogAppDelegate的地址,而不是AddressBookDelegate作爲我可能會預料到的。這使我認爲在App Delegate文件中關閉了地址簿委託代碼的位移。

如果AddressBookDelegate取消委託代碼被稱爲AddressBookDelegate 1000字節,我的應用程序實際上是「輸入」1000字節代碼到MyprogAppDelegate,並因此崩潰。所以不知何故,我沒有正確設置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中的代碼。我在解決它時遇到了一些問題,並與之連接。請注意在問題底部添加的文字,以便澄清任何事情。 – Ric

0

編輯好了,整個第一個答案已經拋出了。這有一些警告,在黑暗中仍然有點不足,但我認爲這將會更有幫助。一些想法確實繼續下去。

  1. 很可能不需要單獨的類作爲您ABPeoplePickerNavigationControllerDelegate。很可能,它應該是同一個類,它的代碼位於底部(調用presentModalViewController:animated:。因爲我不知道那個控制器是什麼,所以我將其稱爲MyViewController以供參考。視圖控制器成爲委託是因爲,在您的委託方法中,您需要能夠關閉具有地址簿的模式視圖控制器。

  2. 肯定不希望你的程序的UIApplicationDelegateABPeoplePickerNavigationControllerDelegate。正如你自己所說,peoplePickerDelegate必須是UIViewController

所以,到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個錯誤。 然後,我試圖將AddressBookDelegate整合到MyprogAppDelegate中,以便到達MyprogAppDelegate從NSObject派生的牆,而dismissModalViewControllerAnimated方法必須從UIViewController對象派生。 我再次回到你的結構,看看我是否可以充實它。我明白你的「不要這樣做」的例子,我走錯了路。 – Ric

+0

在你abouve結構調查我沒有看到你複製 @interfact AddressBookDelegate的功能:的UIViewController 因爲ABPeoplePickerNavigationControllerDelegate協議必須從一個UIViewController派生。請允許我爲你重新編碼你的例子,爲我提供更多的細節嗎?我給它的時間,而不是輕浮。 – Ric

+0

花了一些時間看文檔後,我重寫了它。最大的改變是,你根本不應該使用AppDelegate,而是讓任何視圖控制器產生人員選擇器委派的地址簿。 – Matt

相關問題