深入探索分屏视图与弹出框的使用
立即解锁
发布时间: 2025-08-24 01:21:19 阅读量: 1 订阅数: 7 

# 深入探索分屏视图与弹出框的使用
## 1. 主 - 详细模板应用的工作原理
首先,在 iPad 模拟器上运行应用程序,并将设备旋转至横向模式,此时主视图控制器会显示出来。详细视图控制器中的标签当前显示的是故事板中分配给它的默认文本。我们接下来要探究的是,在主视图控制器中选择一个项目如何导致该文本发生变化。
目前主视图控制器中没有任何项目,可通过按下其导航栏右上角的“+”按钮来添加项目。每次按下该按钮,一个新项目就会被添加到控制器的表格视图中。
主视图控制器表格中的所有项目都是日期。选择其中一个日期,详细视图中的标签会更新以显示相同的日期。实现这一功能的代码是 `DetailViewController.m` 中的 `configureView` 方法,当详细视图控制器的 `detailItem` 属性存储新值时,该方法会被调用。
当在主视图控制器的表格视图中选择一行时,iOS 会执行“Show Detail”转场,转场目标是包装详细视图控制器的导航控制器,这会引发两件事:
- 创建详细视图控制器的新实例,并将其视图添加到视图层次结构中。
- 调用主视图控制器中的 `prepareForSegue:sender:` 方法。
以下是 `MasterViewController.m` 中处理此操作的模板代码:
```objc
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = self.objects[indexPath.row];
DetailViewController *controller =
(DetailViewController *)[[segue destinationViewController] topViewController];
[controller setDetailItem:object];
controller.navigationItem.leftBarButtonItem = self.splitViewController.
displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
}
}
```
这个方法首先检查转场标识符,确保是预期的转场,并获取表格中所选对象的 `NSDate` 对象。然后,主视图控制器从转场目标视图控制器的 `topViewController` 属性中找到 `DetailViewController` 实例。最后,设置详细视图控制器的 `detailItem` 属性以更新详细视图,并在详细视图控制器的导航栏中添加显示模式按钮。
### 1.1 操作步骤总结
1. 在 iPad 模拟器上运行应用并旋转至横向模式。
2. 按下主视图控制器导航栏右上角的“+”按钮添加项目。
3. 选择主视图控制器表格中的日期,观察详细视图标签的更新。
## 2. 引入总统数据
接下来,我们要将模板应用转变为展示美国总统信息的应用。首先,从相关资源中找到 `PresidentList.plist` 文件,将其拖入 Xcode 项目的 `Presidents` 文件夹中,并确保勾选复制文件的复选框。该文件包含了所有美国总统的信息,包括姓名和他们的维基百科页面 URL。
### 2.1 修改主视图控制器
#### 2.1.1 类扩展修改
在 `MasterViewController.m` 的类扩展中,添加以下代码并移除原有的 `objects` 属性:
```objc
@interface MasterViewController ()
@property (copy, nonatomic) NSArray *presidents;
@end
```
这样做是为了使用一个更有意义的不可变数组来存储总统列表。
#### 2.1.2 `viewDidLoad` 方法修改
修改 `viewDidLoad` 方法以加载总统列表,并移除设置编辑和插入按钮的代码:
```objc
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
NSString *path = [[NSBundle mainBundle] pathForResource:@"PresidentList"
ofType:@"plist"];
NSDictionary *presidentInfo = [NSDictionary
dictionaryWithContentsOfFile:path];
self.presidents = [presidentInfo objectForKey:@"presidents"];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)
[[self.splitViewController.viewControllers lastObject] topViewController];
}
```
#### 2.1.3 删除不必要的方法
删除 `insertNewObject:` 方法,因为我们不再使用 `objects` 数组:
```objc
- (void)insertNewObject:(id)sender {
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
```
同时,删除处理表格视图行编辑的数据源方法:
```objc
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array,
and add a new row to the table view.
}
}
```
#### 2.1.4 修改表格视图数据源方法
修改 `tableView:numberOfRowsInSection:` 方法以显示总统列表的行数:
```objc
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.presidents count];
}
```
修改 `tableView:cellForRowAtIndexPath:` 方法,使每个单元格显示总统的姓名:
```objc
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary *president = self.presidents[indexPath.row];
cell.textLabel.text = president[@"name"];
return cell;
}
```
修改 `prepareForSegue:sender:` 方法,将所选总统的数据(一个 `NSDictionary`)传递给详细视图控制器:
```objc
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDictionary *president = self.presidents[indexPath.row];
DetailViewCo
```
0
0
复制全文
相关推荐







