iOS开发:用户数据与表格视图处理指南
立即解锁
发布时间: 2025-08-25 01:23:22 阅读量: 2 订阅数: 6 

### iOS开发:用户数据与表格视图处理指南
#### 1. 用户数据处理:设置联系人信息
在iOS开发中,处理用户数据是一项重要任务,尤其是联系人信息的设置。以下是设置联系人信息的具体步骤:
1. **创建新项目**:使用Single View Application模板,创建名为“Chapter8Recipe8”的新项目,类前缀为“Main”。
2. **添加框架**:将AddressBook.framework和AddressBookUI.framework添加到项目中。
3. **设计用户界面**:在视图控制器的XIB文件中添加一个UIButton,并连接到名为`-newContactPressed:`的动作。
4. **导入框架并配置协议**:在头文件中导入框架,并使视图控制器遵循`ABNewPersonViewControllerDelegate`协议。
```objc
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
```
5. **实现动作方法和委托方法**:
```objc
-(void)newContactPressed:(id)sender
{
ABNewPersonViewController *view = [[ABNewPersonViewController alloc] init];
view.newPersonViewDelegate = self;
UINavigationController *newNavigationController = [[UINavigationController alloc]
initWithRootViewController:view];
[self presentModalViewController:newNavigationController animated:YES];
}
-(void)newPersonViewController:(ABNewPersonViewController *)newPersonView
didCompleteWithNewPerson:(ABRecordRef)person
{
if (person == NULL)
{
NSLog(@"User Cancelled Creation");
}
else
NSLog(@"Successfully Created New Person");
[self dismissModalViewControllerAnimated:YES];
}
```
通过上述步骤,用户可以创建新联系人并添加到地址簿中。但这种方式需要用户手动输入所有信息,接下来介绍如何通过编程方式创建记录并设置其值。
1. **修改用户界面**:添加一系列UITextFields,并设置占位符文本描述其用途。
2. **连接属性**:将每个UITextField连接到头文件中的相应属性。
```objc
@property (strong, nonatomic) IBOutlet UITextField *textFieldFirst;
@property (strong, nonatomic) IBOutlet UITextField *textFieldLast;
@property (strong, nonatomic) IBOutlet UITextField *textFieldPhone;
@property (strong, nonatomic) IBOutlet UITextField *textFieldStreet;
@property (strong, nonatomic) IBOutlet UITextField *textFieldCity;
@property (strong, nonatomic) IBOutlet UITextField *textFieldState;
@property (strong, nonatomic) IBOutlet UITextField *textFieldZip;
```
3. **实现创建新联系人的代码**:
```objc
ABMutableMultiValueRef multi =
ABMultiValueCreateMutable(kABMultiStringPropertyType);
CFErrorRef anError = NULL;
ABMultiValueIdentifier multivalueIdentifier;
bool didAdd, didSet;
didAdd = ABMultiValueAddValueAndLabel(multi, (__bridge
CFStringRef)self.textFieldPhone.text,
kABPersonPhoneMobileLabel,
&multivalueIdentifier);
if (!didAdd)
{
NSLog(@"Error Adding Phone Number");
}
ABRecordRef aRecord = ABPersonCreate();
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, (__bridge
CFStringRef)self.textFieldFirst.text, nil);
ABRecordSetValue(aRecord, kABPersonLastNameProperty, (__bridge
CFStringRef)self.textFieldLast.text, nil);
didSet = ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
if (!didSet)
{
NSLog(@"Error Setting Phone Value");
}
CFRelease(multi);
ABMutableMultiValueRef address =
ABMultiValueCreateMutable(kABDictionaryPropertyType);
// Set up keys and values for the dictionary.
CFStringRef keys[5];
CFStringRef values[5];
keys[0] = kABPersonAddressStreetKey;
keys[1] = kABPersonAddressCityKey;
keys[2] = kABPersonAddressStateKey;
keys[3] = kABPersonAddressZIPKey;
keys[4] = kABPersonAddressCountryKey;
values[0] = (__bridge CFStringRef)self.textFieldStreet.text;
values[1] = (__bridge CFStringRef)self.textFieldCity.text;
values[2] = (__bridge CFStringRef)self.textFieldState.text;
values[3] = (__bridge CFStringRef)self.textFieldZip.text;
values[4] = CFSTR("USA");
CFDictionaryRef aDict = CFDictionaryCreate(kCFAllocatorDefault,(void *)keys,
(void *)values,5,
&kCFCopyStringDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
ABMultiValueIdentifier dictionaryIdentifier;
bool didAddAddress;
didAddAddress = ABMultiValueAddValueAndLabel(address, aDict, kABHomeLabel,
&dictionaryIdentifier);
if (!didAddAddress)
{
NSLog(@"Error Adding Address");
}
CFRelease(aDict);
ABRecordSetValue(aRecord, kABPersonAddressProperty, address, nil);
```
此时,有两种方式实现新联系人的创建:
- **方式一**:加载`ABNewPersonViewController`并传递创建的`ABPersonRef`。
```objc
view.displayedPerson = aRecord;
```
- **方式二**:直接通过编程方式创建联系人,无需用户确认。
```objc
ABAddressBookRef addressBook = ABAddressBookCreate();
ABAddressBookAddRecord(addressBook, aRecord, nil);
ABAddressBookSave(addressBook, nil);
```
无论选择哪种方式,都需要在方法结束前释放相关变量。
```objc
CFRelease(aRecord);
CFRelease(address);
```
#### 2. 用户数据处理:查看联系人
查看联系人信息也是常见的需求,以下是实现步骤:
1. **创建新项目**:创建名为“Chapter8Recipe9”的新项目,类前缀为“MainViewController”。
2. **导入框架和添加表格视图**:导入Address Book和Address Book UI框架,并在视图控制器中添加UITableView。
3. **连接属性和遵循协议**:将UITableView连接到头文件中的`tableViewContacts`属性,并使视图控制器遵循`UITableViewDelegate`、`UITableViewDat
0
0
复制全文
相关推荐










