我在iOS中使用SWRevealViewController
导航抽屉。我有一个主屏幕,默认情况下会在应用启动时显示。我还有其他屏幕,如设置,帮助,关于。因此,当用户在屏幕之间切换时,第一个视图控制器的状态会被重置。如果在屏幕上显示一些数据,我想保持它的状态,那么它就不会被删除。
我使用下面的代码在屏幕之间切换。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row==0)
{
[self performSegueWithIdentifier:@"home" sender:cell];
[cell setSelected:YES];
}
else if(indexPath.row==1)
{
[self performSegueWithIdentifier:@"settings" sender:cell];
}
else if(indexPath.row==2)
{
[self performSegueWithIdentifier:@"help" sender:cell];
}
else if(indexPath.row==3)
{
[self performSegueWithIdentifier:@“about” sender:cell];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"Segue");
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[titles objectAtIndex:indexPath.row] capitalizedString];
if([segue.identifier isEqualToString:@"settings"])
{
self.revealViewController.navigationItem.title = @"Settings";
}
else if([segue.identifier isEqualToString:@"help"])
{
self.revealViewController.navigationItem.title = @"help";
}
else if([segue.identifier isEqualToString:@"about"])
{
self.revealViewController.navigationItem.title = @"about";
}
if([segue isKindOfClass:[SWRevealViewControllerSegue class]])
{
SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;
swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: @[dvc] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
};
}
}
我看到一个解决方案是将viewController保留在导航堆栈中以便viewController不被破坏,当用户转到“home”时它将保留在堆栈中,然后从导航堆栈重新启动它,而不是再次初始化视图控制器。
答案 0 :(得分:0)
例如,对于HelpVC,您将其属性存储在侧面mennu视图控制器中:
@property (nonatomic, strong) HelpViewContoller* helpVC;
比在行选择而不是调用你的segue,你实例化视图控制器,但在此之前你检查它是否已经实例化你已经使用它:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row==0) {
if (helpVC == nil) {
helpVC = [self.storyboard instantiateViewControllerWithIdentifier:@"helpVCidentifier"];
[self changeFrontVCto:helpVC];
}
}
}
-(void) changeFrontVCto:(UIViewController*)vc {
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: @[vc] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
}