I created an iOS app in Xcode. There are four ViewControllers in my app. You can navigate through the app by tapping the buttons in the bottom toolbar.
How can I prevent a ViewController from reloading when visiting it again?
I created an iOS app in Xcode. There are four ViewControllers in my app. You can navigate through the app by tapping the buttons in the bottom toolbar.
How can I prevent a ViewController from reloading when visiting it again?
This largely depends on what your view controllers contain so it's hard to answer without seeing your code. That said, rather than 'presenting' the view controllers another option would be to hide/unhide the views (or animating them on and off the screen). You could do this with four view controllers or with four views managed by a single controller.
There are a couple of ways of going about this. Personally I would have one viewController that controls all four views. If you are using a storyboard then I would have the toolbar at the bottom and then you can either 1) add the three webviews and the textview to the storyboard - or 2) you can one of the views and then create the other three views programatically. If you went the later route you could simply place the one web view (we'll call it webView1) on the storyboard and then override the viewDidLayoutSubviews and add the lines
-(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;
}
Then on your toolBar you can have your buttons unhide the view you want to show by changing the view property, for example, if you want to show the second webView you would simply say:
webView2.hidden=NO;
Here's the code of one ViewController.h/ViewController.m file:
//
// 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
So there are a couple of ways to keep track of whether the view has been loaded or not. One way is to create singleton and add several boolean properties to monitor whether the view has been loaded. Another way is to us NSUserDefaults to store a property once it has been loaded the first time. If you go that route then here is what the code would look like in your viewDidLoad method:
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"];}
This place a wrapper around your call to load the webView and will only make the call when it's loaded the first time.