尝试创建表单以向设备添加联系人 - 如何处理空字段?

时间:2013-12-07 05:29:30

标签: ios iphone objective-c null abaddressbook

我有一个简单的表单,现在只占用四个字段(我稍后会添加)

  • 名字
  • 姓氏
  • 家庭电子邮件
  • 工作电子邮件

我只想保存具有值作为新联系人的项目。现在,如果我没有输入值,那么在联系页面中,值为“NULL”。处理这个问题的合适方法是什么?

这是我的代码

#pragma mark - Add New Contacts Methods

- (IBAction)savePublicContact:(UIBarButtonItem *)sender {        
    CFErrorRef  anError = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &anError);
    ABRecordRef person = ABPersonCreate();
    Person *personUserDefined = [self populatePersonToSave];

    ABRecordSetValue(person,kABPersonFirstNameProperty,(__bridge CFTypeRef)(personUserDefined.firstName),&anError);
    ABRecordSetValue(person,kABPersonLastNameProperty,(__bridge CFTypeRef)(personUserDefined.lastName),&anError);

    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.homeEmail), (CFStringRef)@"Home Email", NULL);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.workEmail), (CFStringRef)@"Work Email", NULL);
    ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);

    ABAddressBookAddRecord(addressBook, person, &anError);

    if(ABAddressBookSave(addressBook, &anError)){
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Added Contact"
                                                               message:@"You successfully added a contact"
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];

    }else{
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Contact Error"
                                                               message:(@"Contact was not able to be added")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
    }


}

- (Person *)populatePersonToSave
{
    Person *person = [[Person alloc] init];

    if([self.firstNameToSaveTextField.text length] > 0){
        person.firstName = self.firstNameToSaveTextField.text;
    }

    if([self.lastNameToSaveTextField.text length] > 0) {
        person.lastName = self.lastNameToSaveTextField.text;
    }

    if([self.workEmailToSaveTextField.text length] > 0){
        person.workEmail = self.workEmailToSaveTextField.text;
    }

    if([self.homeEmailToSaveTextField.text length] > 0){
        person.homeEmail = self.homeEmailToSaveTextField.text;
    }

    return person;
}

2 个答案:

答案 0 :(得分:3)

#pragma mark - Add New Contacts Methods

- (IBAction)savePublicContact:(UIBarButtonItem *)sender {

    CFErrorRef  anError = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &anError);
    ABRecordRef person = ABPersonCreate();

    if([self isvalidDetails]){ //New method to validate details - this will return yes/no based on validation
        Person *personUserDefined = [self populatePersonToSave];
        ABRecordSetValue(person,kABPersonFirstNameProperty,(__bridge CFTypeRef)(personUserDefined.firstName),&anError);
        ABRecordSetValue(person,kABPersonLastNameProperty,(__bridge CFTypeRef)(personUserDefined.lastName),&anError);

        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.homeEmail), (CFStringRef)@"Home Email", NULL);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.workEmail), (CFStringRef)@"Work Email", NULL);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);

        ABAddressBookAddRecord(addressBook, person, &anError);

        if(ABAddressBookSave(addressBook, &anError)){
            UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Added Contact"
                                                                   message:@"You successfully added a contact"
                                                                  delegate:self
                                                         cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertsuccess show];

        }else{
            UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Contact Error"
                                                                   message:(@"Contact was not able to be added")
                                                                  delegate:self
                                                         cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertsuccess show];
        }
    }

}

-(BOOL)isValidDetails{
    BOOL isValidated = TRUE;

    if([self.firstNameToSaveTextField.text length] <= 0){ //Trim and check length
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Required field"
                                                               message:(@"Please enter firstname")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        isValidated = FALSE;
        return isValidated;
    }

    if([self.lastNameToSaveTextField.text length] > 0) {
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Required field"
                                                               message:(@"Please enter lastname")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        isValidated = FALSE;
        return isValidated;
    }

    if([self.workEmailToSaveTextField.text length] > 0){
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Required field"
                                                               message:(@"Please enter work email")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        isValidated = FALSE;
        return isValidated;
    }

    if([self.homeEmailToSaveTextField.text length] > 0){
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Required field"
                                                               message:(@"Please enter home email")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        isValidated = FALSE;
        return isValidated;
    }
    return isValidated;
}

- (Person *)populatePersonToSave
{
    Person *person = [[Person alloc] init];

    if([self.firstNameToSaveTextField.text length] > 0){
        person.firstName = self.firstNameToSaveTextField.text;
    }

    if([self.lastNameToSaveTextField.text length] > 0) {
        person.lastName = self.lastNameToSaveTextField.text;
    }

    if([self.workEmailToSaveTextField.text length] > 0){
        person.workEmail = self.workEmailToSaveTextField.text;
    }

    if([self.homeEmailToSaveTextField.text length] > 0){
        person.homeEmail = self.homeEmailToSaveTextField.text;
    }

    return person;
}

答案 1 :(得分:3)

最好将检查空状态放在要添加联系人地址簿的位置,这是因为每次启动联系人列表中的必填字段时。如果您在必填字段中没有数据输入,我们为什么要启动它或参考它。

最好使用修改后的代码:

if (personUserDefined.firstName) {
    ABRecordSetValue(person,kABPersonFirstNameProperty,(__bridge CFTypeRef)(personUserDefined.firstName),&anError);
}


if (personUserDefined.lastName) {
    ABRecordSetValue(person,kABPersonLastNameProperty,(__bridge CFTypeRef)(personUserDefined.lastName),&anError);
}

ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
if (personUserDefined.homeEmail) {
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.homeEmail), (CFStringRef)@"Home Email", NULL);
}

if (personUserDefined.workEmail) {
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.workEmail), (CFStringRef)@"Work Email", NULL);
}
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);
ABAddressBookAddRecord(addressBook, person, &anError);

上面的代码为您提供了如何防止在地址簿中插入空值。请检查条件是否满足条件(我没有在系统上执行)。