我在我的应用中使用SWRevealController
。我有一个MainController
作为“HOME”,在应用启动时显示。现在我有textfield
&该屏幕上的其他控件。当我切换到另一个屏幕&回到“Home”然后“Home”屏幕上的所有数据都消失了。它没有显示任何数据。我想保持它的状态。现在我想将该视图控制器保持在堆栈中,这样当我回到这个viewcontroller
时,它仍然保持在堆栈中。其他视图控制器刚刚弹出。请解释我该怎么做?
我正在使用以下代码。
-(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];
};
}
}
编辑:
尝试下面的代码&如果我在“Home”和“Home”之间切换,它正在工作其他屏幕两次。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)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];
}
}
在prepareforsegue
- (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:@"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];
};
}
}
答案 0 :(得分:1)
每次执行segue时,都会在UIViewController
内为您创建一个新的destinationViewController
实例,并且一旦创建(第一次),您需要保留对它的引用。
保留对保存状态所需的所有UIViewController
个实例的引用 -
SettingsViewController* objSettingsVC;
如果您之前创建了需要导航到的屏幕的实例,请不要执行segue -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)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
。