Objective-cでのWebViewControllerの作成

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
紅葉

Objective-cでのWebViewControllerの作成

#1

投稿記事 by 紅葉 » 14年前

色々なサイトを参考にして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の作成

#2

投稿記事 by 紅葉 » 14年前

先ほどのソースの修正です。
下記の様にしてナビゲーションバーにボタンの追加はしてあります。
片方のボタンを押すと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の作成

#3

投稿記事 by めるぽん » 14年前

canGoBack や canGoForward で戻りや送りがあることを確認してボタンを有効にして、ボタンが押されたら goBack や goForward を呼び出せばいいのではないでしょうか。

紅葉

Re: Objective-cでのWebViewControllerの作成

#4

投稿記事 by 紅葉 » 14年前

回答ありがとうございました。
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の作成

#5

投稿記事 by めるぽん » 14年前

UIWebViewDelegate プロトコルの webViewDidStartLoad: か webViewDidFinishLoad: あたりを実装して(どっちがいいのかは試してないので分かりません)、その中で UIButton の enabled プロパティを弄ればいいと思います。

めるぽん

Re: Objective-cでのWebViewControllerの作成

#6

投稿記事 by めるぽん » 14年前

と思ったら、UISegmentedControl にページ遷移機能を持たせてるんですね。あまりいい方法とは思えませんが・・・。
とりあえずは setEnabled:forSegmentAtIndex あたりでそれぞれのボタンの有効、無効を操作できそうです。

紅葉

Re: Objective-cでのWebViewControllerの作成

#7

投稿記事 by 紅葉 » 14年前

遅くなってしまいすみませんでした。

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

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

閉鎖

“C言語何でも質問掲示板” へ戻る