2012-02-23 71 views
0

我對iOS開發非常新穎。所以,請儘可能準確和基本地做出迴應。如何告訴地址簿選擇器填寫哪些字段?

我有一個有兩個(此時 - 可能會有更多)的字段,每個字段旁邊都會有一個按鈕,允許用戶從iPad地址簿中選擇一個聯繫人並填寫相關字段和地址簿中的姓氏。

Basic Form Mockup

The example code I have拉去,我可以在聯繫人姓名填點。不過,我希望能夠點擊推薦人旁邊的「瀏覽聯繫人」按鈕,並使用相同的功能填寫名稱。我看到getContactName函數具有sender參數。所以,我可以很容易地判斷兩個按鈕(或其他人)中的哪一個被點擊。

但是,當我從地址簿中選擇,我怎麼知道在PeoplePickerNavigationController或fillContactName函數中點擊了哪個按鈕?

- (IBAction)getContactName:(id)sender { 

    ABPeoplePickerNavigationController *picker = 
    [[ABPeoplePickerNavigationController alloc] init]; 
    picker.peoplePickerDelegate = self; 

    [self presentModalViewController:picker animated:YES]; 
} 


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

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker 
     shouldContinueAfterSelectingPerson:(ABRecordRef)person { 

    [self fillContactName:person]; 

    [self dismissModalViewControllerAnimated:YES]; 

    return NO; 
} 

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


- (void)fillContactName:(ABRecordRef)person 
{ 
    NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, 
                    kABPersonFirstNameProperty); 

    NSString *test = [firstName stringByAppendingString:@" "]; 

    NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, 
                     kABPersonLastNameProperty); 

    NSString *fullName = [test stringByAppendingString:lastName]; 

    self.contactName.text = fullName; 

} 
+1

在附註中,您確定不想使用「ABRecordCopyCompositeName()」函數而不是手動將姓和名放在一起嗎? – 2012-06-20 19:15:16

回答

3

這裏有一種方法可以做到這一點。

首先,分配你的「瀏覽聯繫人」按鈕,獨特的INT值的tag財產在創建時(或在故事板,如果你使用的是故事板),這樣就可以區分它們在你的getContactName:方法。我建議爲第一個按鈕設置tag = 0,爲第二個按鈕設置tag = 1。

然後爲您的ViewController類添加一個新屬性,該按鈕被單擊後將存儲指向目標文本字段的指針。 (請務必在您的m文件@synthesize)

@property (nonatomic, strong) UITextField *targetTextField; 

getContactName:,檢查發件人對象,並使用其tag值來設置適當的指針。 (請注意,發件人將是用戶單擊的UIButton對象)。

- (IBAction)getContactName:(id)sender { 

    ... 

    switch(sender.tag) { 
     case 0: 
      self.targetTextField = self.contactName; 
      break; 
     case 1: 
      self.targetTextField = self.referredBy; 
      break; 
     default: 
      self.targetTextField = nil; 
    }   

    [self presentModalViewController:picker animated:YES]; 
} 

然後在fillContactName:,在您先前設置的targetTextField設置文本價值的文本字段。

- (void)fillContactName:(ABRecordRef)person 
{ 
    NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty); 
    NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty); 

    self.targetTextField.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 
} 

請注意,我已經使用了NSString類方法stringWithFormat:讓你的fillContactName代碼更簡潔。在這種情況下,這是一種常用的方法。

+0

工程就像一個魅力。謝謝。 – 2012-02-23 18:55:43

相關問題