ページ 11

Objective-cでのWebViewControllerの作成

Posted: 2011年4月11日(月) 21:21
by 紅葉
色々なサイトを参考にしてiPhone用のWebブラウザを作成しました。
このブラウザにページ送りと戻りをつけたいのですがどのようにすればいいのでしょうか。
他のアプリではサイトを開くごとにMutableArray等にURLを保存したりしているのでしょうか。
良く分からないのですがアドバイスを頂けないでしょうか。

参考にしたサイト 一例
http://d.hatena.ne.jp/ppmp451z/20081028/1225209322

ソース

コード:

//
//  WebViewController.h
//

#import <UIKit/UIKit.h>

@interface WebViewController : UIViewController {
	UIWebView *webView;
	NSString *link;
}
- (void)setURL:(NSString*)URL;
@property(nonatomic, retain)NSString *link;
@end


//
//  WebViewController.m
//

#import "WebViewController.h"

@implementation WebViewController
@synthesize link;

- (void)setURL:(NSString*)URL{
	link = [[NSString alloc] initWithFormat:@"%@",URL];
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	[webView loadRequest:[[NSMutableURLRequest requestWithURL:
						   [NSURL URLWithString:link] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0] retain]];
	webView.scalesPageToFit = YES;
	self.view = webView;
}

- (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.
}

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


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


@end


Re: Objective-cでのWebViewControllerの作成

Posted: 2011年4月11日(月) 22:00
by 紅葉
先ほどのソースの修正です。
下記の様にしてナビゲーションバーにボタンの追加はしてあります。
片方のボタンを押すとgoogleに飛ぶ様になっておりますが
実際には前に見ていたページを表示したいです。

しかし前見ていたページのURLの保存方法や管理方法が
分からないため質問させて頂きました。
恐らくNSMutableArrayにURLが変わるごとにURLを追加していくのかと思うのですが
URLの取得する方法がわかりません。

コード:


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	[webView loadRequest:[[NSMutableURLRequest requestWithURL:
						   [NSURL URLWithString:link] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0] retain]];
	webView.scalesPageToFit = YES;
	self.view = webView;
	self.title = @"テスト";

	// ナビゲーションバーに追加
	NSArray *array = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"up.png"],[UIImage imageNamed:@"down.png"],nil];
	UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:array];
	segmentControl.segmentedControlStyle= UISegmentedControlStyleBar;

	// これをYESにすると、ボタンを押したときに選択されっぱなしにならない
	segmentControl.momentary = YES;
	[segmentControl addTarget:self action:@selector(changeItem:) forControlEvents:UIControlEventValueChanged];
	UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
	self.navigationItem.rightBarButtonItem = button;
	[segmentControl release];
	[button release];
}

// ナビゲーションバーを押した時の処理
- (IBAction)changeItem:(id)sender {
    UISegmentedControl *segmentControl = (UISegmentedControl *)sender;
    NSInteger selectedIndex = segmentControl.selectedSegmentIndex;
	
	// ボタンが押された時に処理
    if (selectedIndex == 0) {
		[webView loadRequest:[[NSMutableURLRequest requestWithURL:
							   [NSURL URLWithString:@"http://www.google.co.jp/"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0] retain]];
		
	} else if (selectedIndex == 1) {
    }
}

Re: Objective-cでのWebViewControllerの作成

Posted: 2011年4月11日(月) 23:47
by めるぽん
canGoBack や canGoForward で戻りや送りがあることを確認してボタンを有効にして、ボタンが押されたら goBack や goForward を呼び出せばいいのではないでしょうか。

Re: Objective-cでのWebViewControllerの作成

Posted: 2011年4月12日(火) 18:41
by 紅葉
回答ありがとうございました。
WebViewにはそんな機能が用意されているのですね・・・。
勉強になりました。ありがとうございます。

ただしボタンを有効、無効にする方法(どこのメソッドでするか等)が理解できておりません。
button.enabledをYES,NOどちらにしても有効のままです。
ソースは下記の様になっております。

コード:

- (void)loadView {
	webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	[webView loadRequest:[[NSMutableURLRequest requestWithURL:
						   [NSURL URLWithString:link] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0] retain]];
	webView.scalesPageToFit = YES;
	self.view = webView;
	self.title = @"テスト";

	// ナビゲーションバーに追加
	NSArray *array = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"up.png"],[UIImage imageNamed:@"down.png"],nil];
	UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:array];
	segmentControl.segmentedControlStyle= UISegmentedControlStyleBar;

	// これをYESにすると、ボタンを押したときに選択されっぱなしにならない
	segmentControl.momentary = YES;
	[segmentControl addTarget:self action:@selector(changeItem:) forControlEvents:UIControlEventValueChanged];
	UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
	self.navigationItem.rightBarButtonItem = button;
	[segmentControl release];
	[button release];
}

// ナビゲーションバーを押した時の処理
- (IBAction)changeItem:(id)sender {
    UISegmentedControl *segmentControl = (UISegmentedControl *)sender;
    NSInteger selectedIndex = segmentControl.selectedSegmentIndex;
	
	// ボタンが押された時に処理
    if (selectedIndex == 0) {
		if( [webView canGoBack] ) [webView goBack];
	} else if (selectedIndex == 1) {
		if( [webView canGoForward] ) [webView goForward];
	}
}


Re: Objective-cでのWebViewControllerの作成

Posted: 2011年4月13日(水) 02:23
by めるぽん
UIWebViewDelegate プロトコルの webViewDidStartLoad: か webViewDidFinishLoad: あたりを実装して(どっちがいいのかは試してないので分かりません)、その中で UIButton の enabled プロパティを弄ればいいと思います。

Re: Objective-cでのWebViewControllerの作成

Posted: 2011年4月13日(水) 02:46
by めるぽん
と思ったら、UISegmentedControl にページ遷移機能を持たせてるんですね。あまりいい方法とは思えませんが・・・。
とりあえずは setEnabled:forSegmentAtIndex あたりでそれぞれのボタンの有効、無効を操作できそうです。

Re: Objective-cでのWebViewControllerの作成

Posted: 2011年4月15日(金) 17:06
by 紅葉
遅くなってしまいすみませんでした。

ご回答ありがとうございます。

これ以外にいい方法が思いつかなくて・・・申し訳ないです><
手元にMacがないので今しばらくお待ちいただけますと助かります。