我正在玩iOS 6状态保存/恢复,我遇到了一个奇怪的情况。如果我在故事板应用程序中的导航控制器中嵌入一个表视图控制器,表格视图(即使在我向下滚动之后)在我测试状态恢复后将其滚动位置重置为顶部(使用“按下主页按钮后停止并且在Xcode中运行“例行程序”。这是在iOS 6.0的Xcode 4.5.1版本中。
我已经执行了支持恢复所需的步骤:我检查了在我的表视图控制器子类中调用encodeRestorableStateWithCoder和e encodeRestorableStateWithCoder(是的,我在运行该检查时调用super)。
如果我从故事板中删除导航控制器,而是将我的表视图控制器嵌入到选项卡栏控制器中,则滚动位置 保留。
知道我可能做错了什么,如果有的话? (我倾向于认为这是一个错误。)
以下是重现此问题的确切步骤(适用于Xcode 4.5.1,iOS 6.0)
使用启用了ARC的空模板创建一个新的iPhone项目。
创建一个名为Storyboard的故事板文件,并在项目编辑器中将其设置为应用程序的“主要故事板”>目标>摘要窗格
将导航控制器拖到故事板场景
(4)在Appdelegate.m中,将@implementation部分中的代码替换为
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
(5)创建一个没有XIB的UITableViewController子类,将其命名为TableViewController,并将.m文件中的代码替换为:
#import "TableViewController.h"
@implementation TableViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1000;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
return cell;
}
(6)在Storyboard中,在实用程序窗格的Identity Inspector中将表视图控制器的类从UITableViewController更改为TableViewController。
(7)在Storyboard中,通过Attributes Inspector将单元格重用标识符“Cell”分配给表格视图单元格。
(8)在Storyboard中,将Restoration ID分配给Identity Inspector中的导航控制器,表视图控制器和表视图。我将Storyboard ID分别设置为“Nav Controller”,“Table View Controller”和“Table View”,并选中“Use Storyboard ID”作为每个的Restoration ID。
构建应用程序,在表格视图中向下滚动,单击主页按钮,在Xcode中停止应用程序,然后通过Xcode再次运行。
表格视图应滚动到按下主页按钮之前的点,但不是。