Ios 保持ViewController内容;防止ViewController重新加载–;怎么用?

Ios 保持ViewController内容;防止ViewController重新加载–;怎么用?,ios,xcode,viewcontroller,reloading,Ios,Xcode,Viewcontroller,Reloading,我在Xcode中创建了一个iOS应用程序。我的应用程序中有四个ViewController。您可以通过点击底部工具栏中的按钮在应用程序中导航 如何防止再次访问ViewController时重新加载它?这在很大程度上取决于视图控制器包含的内容,因此在没有看到代码的情况下很难回答。也就是说,与“显示”视图控制器不同,另一个选项是隐藏/取消隐藏视图(或在屏幕上或屏幕外设置视图动画)。可以使用四个视图控制器或由一个控制器管理的四个视图来执行此操作 有两种方法可以做到这一点。就我个人而言,我会有一个vie

我在Xcode中创建了一个iOS应用程序。我的应用程序中有四个ViewController。您可以通过点击底部工具栏中的按钮在应用程序中导航


如何防止再次访问ViewController时重新加载它?

这在很大程度上取决于视图控制器包含的内容,因此在没有看到代码的情况下很难回答。也就是说,与“显示”视图控制器不同,另一个选项是隐藏/取消隐藏视图(或在屏幕上或屏幕外设置视图动画)。可以使用四个视图控制器或由一个控制器管理的四个视图来执行此操作

有两种方法可以做到这一点。就我个人而言,我会有一个viewController来控制所有四个视图。如果您使用的是故事板,那么我会将工具栏放在底部,然后您可以1)将三个Web视图和文本视图添加到故事板,或者2)您可以选择其中一个视图,然后通过编程创建其他三个视图。如果您选择后面的路线,只需将一个web视图(我们称之为webView1)放置在情节提要上,然后覆盖ViewDidLayoutSubview并添加线条

-(void)viewDidLayoutSubviews {

    CGRect viewFrame=webView1.frame;
    UIWebView *webView2=[[UIWebView alloc] initWithFrame:viewFrame];
    webView2.hidden=YES;
    UIWebView *webView3=[[UIWebView alloc] initWithFrame:viewFrame];
    webView3.hidden=YES;
    UITextView *textView=[[UITextView alloc] initWithFrame:viewFrame];
    textView.hidden=YES;

}
然后,在工具栏上,您可以通过更改“视图”属性来取消隐藏要显示的视图,例如,如果要显示第二个Web视图,只需说:

webView2.hidden=NO;

这在很大程度上取决于视图控制器包含的内容,因此在没有看到代码的情况下很难回答。也就是说,与“显示”视图控制器不同,另一个选项是隐藏/取消隐藏视图(或在屏幕上或屏幕外设置视图动画)。可以使用四个视图控制器或由一个控制器管理的四个视图来执行此操作

有两种方法可以做到这一点。就我个人而言,我会有一个viewController来控制所有四个视图。如果您使用的是故事板,那么我会将工具栏放在底部,然后您可以1)将三个Web视图和文本视图添加到故事板,或者2)您可以选择其中一个视图,然后通过编程创建其他三个视图。如果您选择后面的路线,只需将一个web视图(我们称之为webView1)放置在情节提要上,然后覆盖ViewDidLayoutSubview并添加线条

-(void)viewDidLayoutSubviews {

    CGRect viewFrame=webView1.frame;
    UIWebView *webView2=[[UIWebView alloc] initWithFrame:viewFrame];
    webView2.hidden=YES;
    UIWebView *webView3=[[UIWebView alloc] initWithFrame:viewFrame];
    webView3.hidden=YES;
    UITextView *textView=[[UITextView alloc] initWithFrame:viewFrame];
    textView.hidden=YES;

}
然后,在工具栏上,您可以通过更改“视图”属性来取消隐藏要显示的视图,例如,如果要显示第二个Web视图,只需说:

webView2.hidden=NO;

下面是一个ViewController.h/ViewController.m文件的代码:

//
//  HomeViewController.h
//  App_Single
//
//

#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIWebView *homewebview;
@property (nonatomic, strong) IBOutlet UIWebView *website;

@end


//
//  HomeViewController.m
//  App_Single
//
//

#import "HomeViewController.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"

@interface HomeViewController ()

@end

@implementation HomeViewController
- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}
- (void)viewDidLoad {
    [super viewDidLoad];


    NSURL *url=[NSURL URLWithString: @"http://google.com"]; NSURLRequest * requestURL=[NSURLRequest requestWithURL:url]; [_homewebview loadRequest:requestURL];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    // Setting the swipe direction.
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

    // Adding the swipe gesture on WebView
    [_homewebview addGestureRecognizer:swipeLeft];
    [_homewebview addGestureRecognizer:swipeRight];

    if (![self connected])
    {
        // not connected
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Keine Internetverbindung vorhanden!" message:@"No network connection available!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    } else
    {
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations {
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskLandscape);
}
- (IBAction)goHomepage:(id)sender {
    NSURL *url=[NSURL URLWithString: @"http://google.com"];
    NSURLRequest *requestURL=[NSURLRequest requestWithURL:url];
    [_website loadRequest:requestURL];
}
- (IBAction)openSearch:(id)sender {
    NSURL *url=[NSURL URLWithString: @"http://google.com/search"];
    NSURLRequest *requestURL=[NSURLRequest requestWithURL:url];
    [_website loadRequest:requestURL];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        [_homewebview goForward];
    }

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        [_homewebview goBack];
    }
}
@end
//
//HomeViewController.h
//应用程序单
//
//
#进口
@接口HomeViewController:UIViewController
@属性(非原子,强)IBUIWebView*homewebview;
@属性(非原子、强)IBUIWebView*网站;
@结束
//
//HomeViewController.m
//应用程序单
//
//
#导入“HomeViewController.h”
#进口
#导入“可达性.h”
@接口HomeViewController()
@结束
@HomeViewController的实现
-(BOOL)连接
{
可达性*可达性=[可达性可达性Internet连接];
NetworkStatus NetworkStatus=[reachability currentReachabilityStatus];
return!(networkStatus==NotReachable);
}
-(无效)viewDidLoad{
[超级视图下载];
NSURL*url=[NSURL URLWithString:@”http://google.com“];NSURLRequest*requestURL=[nsurlRequestRequestWithUrl:url];[\u homewebview loadRequest:requestURL];
UISwipeLeft=[[UISwipeGestureRecognitzer alloc]initWithTarget:self action:@selector(handleSwipe:)];
UISweepGestureRecognizer*swipeRight=[[UISweepGestureRecognizer alloc]initWithTarget:self action:@selector(HandleSweep:)];
//设置滑动方向。
[swipeLeft设置方向:UISwipegestureRecognitizerDirectionLeft];
[swipeRight设置方向:UISwipegestureRecognitizerDirectionRight];
//在WebView上添加滑动手势
[\u homewebview AddGestureRecognitor:swipeLeft];
[\u homewebview AddGestureRecognitor:swipeRight];
如果(![自连接])
{
//不相连
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“Keine Internetverbindung vorhanden!”消息:@“没有可用的网络连接!”代表:无取消按钮:@“好”其他按钮:无;
[警报显示];
}否则
{
}
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(整数)支持的接口方向{
返回(UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskLandscape);
}
-(iAction)goHomepage:(id)发件人{
NSURL*url=[NSURL URLWithString:@”http://google.com"];
NSURLRequest*requestURL=[nsurlRequestRequestWithUrl:url];
[_网站加载请求:请求URL];
}
-(iAction)openSearch:(id)发件人{
NSURL*url=[NSURL URLWithString:@”http://google.com/search"];
NSURLRequest*requestURL=[nsurlRequestRequestWithUrl:url];
[_网站加载请求:请求URL];
}
-(无效)无手柄擦拭:(UISWEEPGESTURE识别器*)擦拭{
如果(swipe.direction==UISWIPgestureRecognitizerDirectionLeft){
[_HomeWebViewGoForward];
}
如果(swipe.direction==UISWIPGestureRecognitizerDirectionRight){
[_HomeWebViewGoback];
}
}
@结束

以下是一个ViewController.h/ViewController.m文件的代码:

//
//  HomeViewController.h
//  App_Single
//
//

#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIWebView *homewebview;
@property (nonatomic, strong) IBOutlet UIWebView *website;

@end


//
//  HomeViewController.m
//  App_Single
//
//

#import "HomeViewController.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"

@interface HomeViewController ()

@end

@implementation HomeViewController
- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}
- (void)viewDidLoad {
    [super viewDidLoad];


    NSURL *url=[NSURL URLWithString: @"http://google.com"]; NSURLRequest * requestURL=[NSURLRequest requestWithURL:url]; [_homewebview loadRequest:requestURL];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    // Setting the swipe direction.
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

    // Adding the swipe gesture on WebView
    [_homewebview addGestureRecognizer:swipeLeft];
    [_homewebview addGestureRecognizer:swipeRight];

    if (![self connected])
    {
        // not connected
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Keine Internetverbindung vorhanden!" message:@"No network connection available!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    } else
    {
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations {
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskLandscape);
}
- (IBAction)goHomepage:(id)sender {
    NSURL *url=[NSURL URLWithString: @"http://google.com"];
    NSURLRequest *requestURL=[NSURLRequest requestWithURL:url];
    [_website loadRequest:requestURL];
}
- (IBAction)openSearch:(id)sender {
    NSURL *url=[NSURL URLWithString: @"http://google.com/search"];
    NSURLRequest *requestURL=[NSURLRequest requestWithURL:url];
    [_website loadRequest:requestURL];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        [_homewebview goForward];
    }

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        [_homewebview goBack];
    }
}
@end
//
//HomeViewController.h
//应用程序单
//
//
#进口
@接口HomeViewController:UIViewController
@属性(非原子,强)IBUIWebView*homewebview;
@属性(非原子、强)IBUIWebView*网站;
@结束
//
//HomeViewController.m
//应用程序单
//
//
#导入“HomeViewController.h”
#进口
#导入“可达性.h”
@接口HomeViewController()
@结束
@HomeViewController的实现
-(BOOL)连接
{
可达性*可达性=[可达性可达性Internet连接];
NetworkStatus NetworkStatus=[rea]