我用Xcode创建了一个iOS应用。在我的应用程序中有四个viewcontroller。你可以通过点击底部工具栏中的按钮来浏览应用程序。
如何防止ViewController重新加载时再次访问它?
有几种方法可以跟踪视图是否已加载。一种方法是创建singleton并添加几个布尔属性来监视视图是否已加载。另一种方法是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的调用周围放置了一个包装器,并且只会在第一次加载时进行调用。
这在很大程度上取决于你的视图控制器包含什么,所以如果没有看到你的代码,很难回答这个问题。也就是说,而不是"呈现"视图控制器的另一个选择是隐藏/取消隐藏视图(或动画他们在屏幕上和离开)。你可以用四个视图控制器或由单个控制器管理的四个视图来做到这一点。
有几种方法可以做到这一点。我个人会用一个viewController来控制所有四个视图。如果你使用的是storyboard,那么我会在底部设置工具栏,然后你可以1)将三个webview和textview添加到storyboard中,或者2)你可以使用其中一个视图,然后通过编程方式创建其他三个视图。如果你选择后一种方式你可以简单地将一个web视图(我们称之为webView1)放在storyboard上然后重写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