【问题标题】:iPhone Bookmark UIWebView - Displaying Page Title Instead of URLiPhone书签UIWebView - 显示页面标题而不是URL
【发布时间】:2011-06-22 19:45:47
【问题描述】:

我有一个完全可操作的 Web 浏览器应用程序,用于存储书签页面。单击书签按钮时,将显示已存储网站的列表视图。我希望列表视图显示页面标题,而不是显示 URL。我可以使用下面的代码获取页面的标题,但不知道如何或在哪里实现它。

NSString *webSiteTitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];

我已经包含了下面两个 ViewController 的 .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;
}

【问题讨论】:

    标签: iphone objective-c xcode cocoa-touch uiwebview


    【解决方案1】:

    在 ExplorerViewController.m 中,替换:

    [bookmarks addObject:[[[[self webView]request] URL] absoluteString]];
    

    作者:

    [bookmarks addObject:[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"]];
    

    如果你还需要 URL,添加一个 NSArray 而不是 NSString 来存储 URL 和标题。

    【讨论】:

    • 谢谢!我需要存储 URL 和标题,以便在单击标题时 UIWebView 转到 URL。我对 iPhone 编程很陌生,有什么方法可以在上面的代码中告诉我如何做到这一点?
    • 所以改用:NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"kURL", [[[self.webView request] URL] absoluteString], @"kTitle", [self.webView stringByEvaluatingJavaScriptFromString:@ “document.title”],无]; [书签添加对象:字典];并在 - tableView:cellForRowAtIndexPath: (在 BookmarksViewController.m 中),替换: cell.textLabel.text = [bookmarks objectAtIndex:indexPath.row];通过:cell.textLabel.text = [[书签 objectAtIndex:indexPath.row] objectForKey:@"kTitle"];并实现 tableView:didSelectRowAtIndexPath: 以在选择单元格时执行操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 2017-04-01
    • 2014-04-21
    • 2020-04-25
    • 1970-01-01
    相关资源
    最近更新 更多