我有一个完全可操作的网络浏览器应用程序,用于存储书签页面。单击书签按钮时,将显示存储的网站的列表视图。我希望listview显示页面的标题,而不是显示URL。我可以使用下面的代码获取页面标题,但不知道如何或在何处实现它。
NSString * webSiteTitle = [self.webView stringByEvaluatingJavaScriptFromString:@“document.title”];
我已经为下面的两个ViewControllers包含了.m和.h文件。请告诉我该怎么做。
谢谢!
ExplorerViewController.h
@interface ExplorerViewController : UIViewController <UITextFieldDelegate, UIWebViewDelegate, UIActionSheetDelegate>{
UITextField *urlField;
UIBarButtonItem *refreshButton;
UIBarButtonItem *backButton;
UIBarButtonItem *forwardButton;
UIBarButtonItem *bookMarksButton;
UIActivityIndicatorView *loadingActivity;
UIWebView *webView;
UINavigationBar *navigationBar;
}
@property (nonatomic, retain) IBOutlet UITextField *urlField;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *refreshButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *backButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *forwardButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *bookMarksButton;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loadingActivity;
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar;
-(NSString*)repairURL:(NSString*)url;
-(IBAction)refreshWebView;
-(IBAction)goBack;
-(IBAction)goForward;
-(void)actualizeButtons;
-(IBAction)bookmarksButtonTapped;
-(IBAction)addBookmarkButtonTapped;
@end
ExplorerViewController.m
#import "ExplorerViewController.h"
#import "BookmarksViewController.h"
@implementation ExplorerViewController
@synthesize urlField;
@synthesize refreshButton;
@synthesize backButton;
@synthesize forwardButton;
@synthesize bookMarksButton;
@synthesize loadingActivity;
@synthesize webView;
@synthesize navigationBar;
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSMutableArray *bookmarks = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"Bookmarks"] mutableCopy];
if (!bookmarks) {
bookmarks = [[NSMutableArray alloc] init];
}
[bookmarks addObject:[[[[self webView]request] URL] absoluteString]];
[[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:@"Bookmarks"];
[bookmarks release];
}
}
BookmarksViewController.h
@class ExplorerViewController;
@interface BookmarksViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
NSMutableArray *bookmarks;
ExplorerViewController *explorerView;
}
@property (nonatomic, retain) NSMutableArray *bookmarks;
@property (nonatomic, retain) ExplorerViewController *explorerView;
-(IBAction)cancelButtonTapped;
@end
BookmarksViewController.m
#import "BookmarksViewController.h"
#import "ExplorerViewController.h"
@implementation BookmarksViewController
@synthesize bookmarks, explorerView;
-(IBAction)cancelButtonTapped {
[self.parentViewController dismissModalViewControllerAnimated:true];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [bookmarks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = [bookmarks objectAtIndex:indexPath.row];
return cell;
}
答案 0 :(得分:5)
在ExplorerViewController.m中,替换:
[bookmarks addObject:[[[[self webView]request] URL] absoluteString]];
by:
[bookmarks addObject:[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"]];
如果您还需要URL,请添加NSArray而不是NSString来存储URL和标题。