保留ViewController个内容;防止 ViewController 重新加载 – 怎么做?

Keep ViewController Content; Prevent ViewController From Reloading – How?

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

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

这在很大程度上取决于您的视图控制器包含的内容,因此如果不查看您的代码就很难回答。也就是说,除了 'presenting' 视图控制器之外,另一种选择是 hide/unhide 视图(或在屏幕上和屏幕外设置动画)。您可以使用四个视图控制器或单个控制器管理的四个视图来执行此操作。

有几种方法可以解决这个问题。就我个人而言,我会有一个 viewController 来控制所有四个视图。如果您使用的是情节提要,那么我会将工具栏放在底部,然后您可以 1) 将三个 webview 和文本视图添加到情节提要中 - 或者 2) 您可以使用其中一个视图,然后以编程方式创建其他三个视图.如果你走后面的路线,你可以简单地将一个网络视图(我们称之为 webView1)放在故事板上,然后覆盖 viewDidLayoutSubviews 并添加行

-(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;

}

然后在您的工具栏上,您可以通过更改视图 属性 让您的按钮取消隐藏您想要显示的视图,例如,如果您想要显示第二个 webView,您只需说:

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

所以有几种方法可以跟踪视图是否已加载。一种方法是创建单例并添加几个布尔属性来监视视图是否已加载。另一种方法是我们 NSUserDefaults 在第一次加载后存储 属性 。如果你走那条路,那么代码在你的 viewDidLoad 方法中应该是这样的:

if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"homeLoadFlag"] isEqualToString:@"YES"]) {
    NSURL *url=[NSURL URLWithString: @"http://google.com"]; 
    NSURLRequest * requestURL=[NSURLRequest requestWithURL:url]; 
   [_homewebview loadRequest:requestURL];
    [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"homeLoadFlag"];}

这会在您加载 webView 的调用周围放置一个包装器,并且只会在第一次加载时进行调用。