xcode Version 6.1.1でiPhoneアプリの開発を行っています。
webViewを表示するコントローラーと
その他のコントローラの画面間を遷移した際に、
webViewで先ほど表示していたページをそのまま表示したいと考えています。(リロードを行わずに)
実装方法の基本概念としては
1.AppDelegateにwebViewをグローバル変数的に保持
2.webViewのControllerが表示された際に、グローバル変数のwebViewに値がなければデフォルトページを表示、値があればグローバル変数のwebViewを表示。
という概念で以下のようにコーディングしたのですが、元のページが表示されずに、白いページが表示されてしまいます。
どなたかどのようにすればいいかご教授いただけませんでしょうか。
よろしくお願いいたします。
//
// AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
//webViewをグローバル変数的に保持
@property (nonatomic) UIWebView *webView;
@end
//
// ViewController.h
#import "AppDelegate.h"
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
//グローバル変数を取得、設定するためのインスタンス
AppDelegate *appDelegate;
}
@end
//
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];
//グローバル変数を呼び出すためのインスタンス作成
appDelegate = [[UIApplication sharedApplication] delegate];//グローバル変数にwebViewが保持されていなければデフォルトページロード
if(appDelegate.webView == nil){
NSString *path = @"http://google.com";
NSURL *url = [NSURL URLWithString:path];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];//グローバル変数にwebViewが保持されていればそれを使う
}else{
webView = appDelegate.webView;
}
}
//ロード開始
- (void)webViewDidStartLoad:(UIWebView *)webView {
self.webViewLoadingCount++;
}
//ロード終了
- (void)webViewDidFinishLoad:(UIWebView *)wv {
self.webViewLoadingCount--;
if (self.webViewLoadingCount > 0) {
// Loading
return;
}
// Load complete
//ロードが終わったところでグローバル変数に格納
appDelegate.webView = wv;
}
//ロードエラー
-
(void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error {
self.webViewLoadingCount--;
if (self.webViewLoadingCount > 0) {
// Loading
return;
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;// Load complete with error
//ロードが終わったところでグローバル変数に格納
appDelegate.webView = wv;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。