ページ 11

UIWebViewに戻る進むリロードボタンが表示されません。

Posted: 2012年4月23日(月) 12:24
by bluedog
UIWebViewに戻る進むリロードボタンが表示されません。

ModalViewでUIWebViewを表示しています。
xibは使っていません。


下記のコードで作ってみましたが「Done(戻る)」ボタンは左側に表示されるのですが「戻る進むリロード」は表示されませんでした。

形としてはナビゲーションバーのセンターに矢印の「戻る進む」右側に「リロード」
ボタンを実装したいのですがどうしたらそのように表示されるでしょうか。

コードは下記となります。

■ModalViewController.h

コード:

 
#import

@interface ModalViewController : UIViewController {

UIWebView *webView;
}

- (NSString *)getPageUrl;
- (NSString *)getPageTitle;

@property (retain, nonatomic) IBOutlet UIWebView *web;
@property (copy, nonatomic) NSString *urlString;

- (id)initWithUrlString:(NSString *)url;

@end



■ModalViewController.m

コード:

#import “ModalViewController.h”

@implementation ModalViewController
@synthesize urlString;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren’t in use.
}

- (id)initWithUrlString:(NSString *)url
{
self = [super init];
if (self) {
// Custom initialization
self.urlString = url;
}
return self;
}

#pragma mark – View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

UIWebView *web = [[UIWebView alloc] init];
web.frame = self.view.bounds;
web.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
web.scalesPageToFit = YES;
[self.view addSubview:web];

NSURL *url;
if ([self.urlString length] > 0) {
url = [NSURL URLWithString:self.urlString];
} else {
// URLが指定されていないときのデフォルト
url = [NSURL URLWithString:@"https://www.google.co.jp/"];
}
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[web loadRequest:request];
// Do any additional setup after loading the view from its nib.

UIBarButtonItem *bbDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closeView)];
self.navigationItem.leftBarButtonItem = bbDone;

// Webページのサイズに合わせて表示領域を調整する
webView.scalesPageToFit = YES;
webView.delegate = self;

// 次ページ・前ページボタンのアクション登録
UIButton *goNextButton = (UIButton *)[self.view viewWithTag:2001];
UIButton *goBackButton = (UIButton *)[self.view viewWithTag:2002];
[goNextButton addTarget:self action:@selector(goNextPage) forControlEvents:UIControlEventTouchUpInside];
[goBackButton addTarget:self action:@selector(goBackPage) forControlEvents:UIControlEventTouchUpInside];

// ページのタイトルとURLを取得
UILabel *urlLabel = (UILabel *)[self.view viewWithTag:3001];
UILabel *titleLabel = (UILabel *)[self.view viewWithTag:3002];
[urlLabel setText:[self getPageUrl]];
[titleLabel setText:[self getPageTitle]];

return self;

}

#pragma mark 次の画面に進む
- (void)goNextPage {

[webView goForward];
}

#pragma mark 前の画面に戻る
- (void)goBackPage {

[webView goBack];
}

#pragma mark 開いているWebページのURLを取得する
- (NSString *)getPageUrl {

NSString *pageURL = [webView stringByEvaluatingJavaScriptFromString:@"document.URL"];
return pageURL;
}

#pragma mark 開いているWebページのtitleを取得する
- (NSString *)getPageTitle {

NSString *pageTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
return pageTitle;
}

- (void)dealloc {
[urlString release];
[super dealloc];
}

//modalViewを閉じる
- (void)closeView{
[self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
[self setWeb:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)webViewDidStartLoad:(UIWebView*)webView{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

// ページ読込完了時にインジケータを非表示にする
-(void)webViewDidFinishLoad:(UIWebView*)webView{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

#pragma mark 初期処理

@end


ご教授いただけるとたすかります。
宜しくお願いします。

Re: UIWebViewに戻る進むリロードボタンが表示されません。

Posted: 2012年4月23日(月) 16:03
by h2so5
「Done(戻る)」ボタンは57行目でnavigationItemに登録する処理が書かれていますが、
他のボタンにはその処理を書いていないのが原因でしょう。

Re: UIWebViewに戻る進むリロードボタンが表示されません。

Posted: 2012年4月23日(月) 17:27
by bluedog
回答有り難うございます。

登録する処理が書かれていなかったのですね。早速調べてみます。

またWEBを調べていたら、ツールバーから「戻る進むリロード」のやり方が説明されていたので、

実装してみたところ、こちらも全く表示されません。

下記コードなのですが何故表示されないのでしょうか。先ほどの登録する処理は書かれているとおもうのですが。

ModalViewController.h

コード:

 
#import <UIKit/UIKit.h>

@interface ModalViewController : UIViewController{
    UIWebView *web;
}

@property (retain, nonatomic) IBOutlet UIWebView *web;
@property (copy, nonatomic) NSString *urlString;

- (id)initWithUrlString:(NSString *)url;

@end
ModalViewController.m

コード:

 
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    web = [[UIWebView alloc] initWithFrame:self.view.bounds];
    web.frame = self.view.bounds;
    web.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    web.scalesPageToFit = YES;
    [self.view addSubview:web];
    
    NSURL *url;
    if ([self.urlString length] > 0) {
        url = [NSURL URLWithString:self.urlString];
    } else {
        // URLが指定されていないときのデフォルト
        url = [NSURL URLWithString:@"https://www.google.co.jp/"];
    }
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [web loadRequest:request];
    // Do any additional setup after loading the view from its nib.
    
    UIBarButtonItem *bbDone = [[UIBarButtonItem alloc]
                               initWithTitle:@"Back"
                               style:UIBarButtonItemStyleDone
                               target:self
                               action:@selector(closeView)];
    self.navigationItem.leftBarButtonItem = bbDone;
        
    
    //UIToolbarの生成
    UIToolbar *toolBar = [[[UIToolbar alloc]initWithFrame:CGRectMake(0, 414, 320, 46)]autorelease];
    
    //ツールバーをビューに追加
    [self.view addSubview:toolBar];
    
    //ホームボタンの生成
    UIBarButtonItem *homeButton = [[UIBarButtonItem alloc]init];
    homeButton.title = @"Home";
    homeButton.style = UIBarButtonItemStyleBordered;
    homeButton.target = self;
    homeButton.action = @selector(goHome);
    
    //戻るボタンの生成
    UIBarButtonItem *goBackButton = [[UIBarButtonItem alloc]init];
    goBackButton.title = @"戻る";
    goBackButton.style = UIBarButtonItemStyleBordered;
    goBackButton.target = self;
    goBackButton.action = @selector(goBack);
    
    //進むボタンの生成
    UIBarButtonItem *goFowardButton = [[UIBarButtonItem alloc]init];
    goFowardButton.title = @"進む";
    goFowardButton.style = UIBarButtonItemStyleBordered;
    goFowardButton.target = self;
    goFowardButton.action = @selector(goFoward);
    
    //更新ボタンの生成
    UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc]init];
    reloadButton.title = @"更新";
    reloadButton.style = UIBarButtonItemStyleBordered;
    reloadButton.target = self;
    reloadButton.action = @selector(reloadWebView);
    
    //インジケーターの表示
    [UIApplication sharedApplication].
    networkActivityIndicatorVisible=YES;
    
    
return self;
    
}


- (void)goHome {
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: @"http://www.google.co.jp"]]];
}

- (void)goBack {
    [web goBack];
    
}

- (void)goFoward {
    [web goForward];
}

- (void)reloadWebView {
    [web reload];
}

- (void)dealloc {
    [web release];
    [super dealloc];
    [urlString release];
}

Re: UIWebViewに戻る進むリロードボタンが表示されません。

Posted: 2012年4月23日(月) 18:08
by h2so5
どこに書かれていると考えているのですか?

Re: UIWebViewに戻る進むリロードボタンが表示されません。

Posted: 2012年4月24日(火) 11:48
by bluedog
返答送れて申し訳ありません。

勘違いしておりました。やはり登録する処理が書かれていませんでした。

ツールバー表示に関しては下記コードを追加することにより表示されました。

コード:

 
self.navigationController.toolbarHidden = NO;
    
    self.navigationController.toolbar.barStyle = UIBarStyleBlack;
    self.navigationController.toolbar.translucent = YES;
 


しかしツールバーに「ホーム・戻る・進む・更新」のボタンを表示するための処理が調べてもどうしてもわかりません…。

ご教授お願いします。

現在コード

コード:

 
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    web = [[UIWebView alloc] initWithFrame:self.view.bounds];
    web.frame = self.view.bounds;
    web.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    web.scalesPageToFit = YES;
    [self.view addSubview:web];
    
    NSURL *url;
    if ([self.urlString length] > 0) {
        url = [NSURL URLWithString:self.urlString];
    } else {
        // URLが指定されていないときのデフォルト
        url = [NSURL URLWithString:@"https://www.google.co.jp/"];
    }
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [web loadRequest:request];
    // Do any additional setup after loading the view from its nib.
    
    UIBarButtonItem *bbDone = [[UIBarButtonItem alloc]
                               initWithTitle:@"Back"
                               style:UIBarButtonItemStyleDone
                               target:self
                               action:@selector(closeView)];
    self.navigationItem.leftBarButtonItem = bbDone;
    
    self.title = @"Rady";
    
    
    
    //UIToolbarの生成
    UIToolbar *toolBar = [[[UIToolbar alloc]initWithFrame:CGRectMake(0, 414, 320, 46)]autorelease];
    
    
    //ツールバーをビューに追加
    [self.view addSubview:toolBar];
    self.navigationController.toolbarHidden = NO;
    
    self.navigationController.toolbar.barStyle = UIBarStyleBlack;
    self.navigationController.toolbar.translucent = YES;
    
        
    
    //ホームボタンの生成
    UIBarButtonItem *homeButton = [[UIBarButtonItem alloc]init];
    homeButton.title = @"Home";
    homeButton.style = UIBarButtonItemStyleBordered;
    homeButton.target = self;
    homeButton.action = @selector(goHome);
    
    //戻るボタンの生成
    UIBarButtonItem *goBackButton = [[UIBarButtonItem alloc]init];
    goBackButton.title = @"戻る";
    goBackButton.style = UIBarButtonItemStyleBordered;
    goBackButton.target = self;
    goBackButton.action = @selector(action1);
    
    
    //進むボタンの生成
    UIBarButtonItem *goFowardButton = [[UIBarButtonItem alloc]init];
    goFowardButton.title = @"進む";
    goFowardButton.style = UIBarButtonItemStyleBordered;
    goFowardButton.target = self;
    goFowardButton.action = @selector(goFoward);
    
    //更新ボタンの生成
    UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc]init];
    reloadButton.title = @"更新";
    reloadButton.style = UIBarButtonItemStyleBordered;
    reloadButton.target = self;
    reloadButton.action = @selector(reloadWebView);
    
    //インジケーターの表示
    [UIApplication sharedApplication].
    networkActivityIndicatorVisible=YES;
    
    
    return self;
    
}


- (void)goHome {
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: @"http://www.google.co.jp"]]];
}

- (void)goBack {
    [web goBack];
    
}

- (void)goFoward {
    [web goForward];
}

- (void)reloadWebView {
    [web reload];
}

- (void)dealloc {
    [web release];
    [super dealloc];
    [urlString release];
}

Re: UIWebViewに戻る進むリロードボタンが表示されません。

Posted: 2012年4月24日(火) 13:44
by bluedog
h2so5さんアドバイスありがとうございました。
無事ツールバーにボタンを設置して実装出来ました。
これで解決とさせていただきます。