我在我的应用程序中使用 SWRevealController。我有一个 MainController 作为应用程序启动时显示的“HOME”。现在我在该屏幕上有 textfield 和其他控件。当我切换到另一个屏幕并返回“主页”时,“主页”屏幕上的所有数据都消失了。它在那里不显示任何数据。我想保存它的状态。现在我想将该 View Controller 保留在堆栈中,这样当我返回此 viewcontroller 时,它仍保留在堆栈中,而其他 View Controller 只是弹出。请解释我该怎么做?
我为此使用以下代码。
-(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];
};
}
}
编辑:
它在下面的代码中进行了尝试,如果我在“主页”和其他屏幕之间切换两次,它可以正常工作。
-(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];
};
}
}
最佳答案
每次执行 segue 时,都会在 destinationViewController 中为您创建一个新的 UIViewController 实例,您需要在创建后保留对此的引用(首先时间)。
保留对所有 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 在你的情况下。
关于ios - 在 iOS 的 SWRevealController 中保持 View Controller 的保存状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37940017/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我想为我的Task模型创建一个status属性,该属性将按以下顺序指示它在三部分进度中的位置:打开=>进行中=>完成。它的工作方式类似于亚马逊包裹的交付方式:已订购=>已发货=>已交付。我想知道设置此属性的最佳方法是什么。我可能是错的,但创建三个独立的bool属性似乎有点多余。实现此目标的最佳方法是什么? 最佳答案 Rails4有一个内置的enummacro.它使用单个整数列并映射到键列表。classOrderenumstatus:[:ordered,:shipped,:delivered]end状态映射如下:{ordered:0,
s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成