我在我的应用程序中使用 SWRevealController
。我有一个 MainController
作为“HOME”,它会在应用程序启动时显示。现在我在该屏幕上有 textfield
和其他控件。当我切换到另一个屏幕并返回“主页”时,“主页”屏幕上的所有数据都消失了。它没有显示任何数据。我想保存它的状态。现在我想将该 View Controller 保留在堆栈中,以便当我回到此 viewcontroller
时,它仍保留在堆栈中,而其他 View Controller 刚刚弹出。请解释我该怎么做?
我正在使用下面的代码。
-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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)prepareForSegueUIStoryboardSegue *)segue senderid)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];
};
}
}
编辑:
它在下面的代码中尝试过,如果我在“主页”和其他屏幕之间切换两次,它就可以工作。
-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row==0)
{
if(objMainController != nil)
{
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: @[objMainController] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
}
else
{
[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)prepareForSegueUIStoryboardSegue *)segue senderid)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:@"about"])
{
self.revealViewController.navigationItem.title = @"about";
}
else if([segue.identifier isEqualToString:@"help"])
{
self.revealViewController.navigationItem.title = @"help";
}
if([segue isKindOfClass:[SWRevealViewControllerSegue class]])
{
SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;
swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
if([segue.identifier isEqualToString:@"home"])
{
objMainController = (MainViewController*)destViewController;
self.revealViewController.navigationItem.title = @"home";
[navController setViewControllers: @[objMainController] animated: NO ];
}
else
{
[navController setViewControllers: @[dvc] animated: NO ];
}
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
};
}
}
每次执行 segue 时,都会在 destinationViewController
内为您创建一个新的 UIViewController
实例,并且在创建后您需要保留对它的引用(首先时间)。
保留对您需要保存状态的所有 UIViewController
实例的引用-
SettingsViewController* objSettingsVC;
如果您需要导航到的屏幕的实例之前已创建,请不要执行 segue-
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row == 1)
{
if(objSettingsVC != nil)
{
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: @[objSettingsVC] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
}
else
{
[self performSegueWithIdentifier:@"settings" sender:cell];
}
}
}
将 UIViewController
实例保存在变量中,以便稍后在 prepareForSegue:
-
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController* destViewController = (UINavigationController*)segue.destinationViewController;
if([segue.identifier isEqualToString:@"settings"])
{
objSettingsVC = (SettingsViewController*)destViewController;
self.revealViewController.navigationItem.title = @"Settings";
}
}
希望对你有帮助。
正如下面评论中所讨论的,您还需要将 initialViewController
实例保存在一个变量中。以下是你可以做到的。
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *objMainController;
@end
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.objMainController = self;
}
@end
这不是一个很好的方法,但它会让你继续前进。你的 AppDelegate 不应该是所有这些导航逻辑的一部分,你应该把它保存在一个单独的对象中,负责你的应用程序导航。也许你的 SideTableViewController
在你的情况下。
关于ios - 在 iOS 的 SWRevealController 中保持 View Controller 的保存状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37940017/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |