导航控制器与表格视图中的详细视图控制器实现
立即解锁
发布时间: 2025-08-24 01:05:39 阅读量: 1 订阅数: 7 

### 导航控制器与表格视图中的详细视图控制器实现
在开发过程中,导航控制器和表格视图是常见的组件。本文将详细介绍如何实现一个用于编辑总统信息的详细视图控制器。
#### 1. 视图即将出现时的操作
在视图即将出现时,我们需要重新加载表格数据,以确保显示的是最新信息。代码如下:
```objc
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
```
#### 2. 创建详细视图控制器
当用户点击 `BIDPresidentsViewController` 表格中的某一行时,会将一个新的控制器推送到导航栈上,以便为该总统进行数据录入。下面是创建详细视图控制器的步骤。
##### 2.1 修改头文件 `BIDPresidentDetailController.h`
```objc
#import <UIKit/UIKit.h>
@class BIDPresident;
#define kNumberOfEditableRows 4
#define kNameRowIndex 0
#define kFromYearRowIndex 1
#define kToYearRowIndex 2
#define kPartyIndex 3
#define kLabelTag 4096
@interface BIDPresidentDetailController : UITableViewController <UITextFieldDelegate>
@property (strong, nonatomic) BIDPresident *president;
@property (strong, nonatomic) NSArray *fieldLabels;
@property (strong, nonatomic) NSMutableDictionary *tempValues;
@property (strong, nonatomic) UITextField *currentTextField;
- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;
- (IBAction)textFieldDone:(id)sender;
@end
```
这里定义了一些常量,用于表示可编辑的行数、每行对应的索引以及标签的 `tag` 值。该类遵循了 `UITextFieldDelegate` 协议,以便在用户对文本字段进行更改时得到通知并保存字段的值。
各属性的作用如下:
- `president`:指向要编辑的 `BIDPresident` 对象。
- `fieldLabels`:包含与各索引对应的标签数组。
- `tempValues`:用于临时存储用户更改的值,避免直接修改 `president` 对象,方便处理取消操作。
- `currentTextField`:指向当前正在编辑的文本字段,解决保存和取消操作时的数据保存问题。
##### 2.2 修改实现文件 `BIDPresidentDetailController.m`
```objc
#import "BIDPresidentDetailController.h"
#import "BIDPresident.h"
@implementation BIDPresidentDetailController
@synthesize president;
@synthesize fieldLabels;
@synthesize tempValues;
@synthesize currentTextField;
- (IBAction)cancel:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)save:(id)sender {
if (currentTextField != nil) {
NSNumber *tagAsNum = [NSNumber numberWithInt:currentTextField.tag];
[tempValues setObject:currentTextField.text forKey:tagAsNum];
}
for (NSNumber *key in [tempValues allKeys]) {
switch ([key intValue]) {
case kNameRowIndex:
president.name = [tempValues objectForKey:key];
break;
case kFromYearRowIndex:
president.fromYear = [tempValues objectForKey:key];
break;
case kToYearRowIndex:
president.toYear = [tempValues objectForKey:key];
break;
case kPartyIndex:
president.party = [tempValues objectForKey:key];
default:
break;
}
}
[self.navigationController popViewControllerAnimated:YES];
NSArray *allControllers = self.navigationController.viewControllers;
UITableViewController *parent = [allControllers lastObject];
[parent.tableView reloadData];
}
- (IBAction)textFieldDone:(id)sender {
[sender resignFirstResponder];
}
#pragma mark -
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"Name:", @"From:", @"To:", @"Party:", nil];
self.fieldLabels = array;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithTitle:@"Cancel"
style:UIBarButtonItemStylePlain
target:self
action:@selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelButton;
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithTitle:@"Save"
style:UIBarButtonItemStyleDone
target:self
action:@selector(save:)];
self.navigationItem.rightBarButtonItem = saveButton;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
self.tempValues = dict;
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return kNumberOfEditableRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *PresidentCellIdentifier = @"PresidentCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: PresidentCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PresidentCellIdentifier];
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(10, 10, 75, 25)];
label.textAlignment = UITextAlignmentRight;
label.tag = kLabelTag;
label.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:label];
UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(90, 12, 200, 25)];
textField.clearsOnBeginEditing = NO;
[textField setDelegate:self];
textField.returnKeyType = UIReturnKeyDone;
[textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.contentView addSubview:textField];
}
NSUInteger row = [indexPath row];
UILabel *label = (UILabel *)[cell viewWithTag:kLabelTag];
UITextField *textField = nil;
for (UIView *oneView in cell.contentView.subviews) {
if ([oneView isMemberOfClass:[UITextField class]])
textField = (UIText
```
0
0
复制全文
相关推荐










