Закладка iPhone UIWebView - отображение заголовка страницы вместо URL - PullRequest
1 голос
/ 22 июня 2011

У меня есть полнофункциональное приложение для веб-браузера, в котором хранятся страницы с закладками.При нажатии кнопки закладок отображается список сохраненных веб-сайтов.Вместо того, чтобы показывать URL, я бы хотел, чтобы в списке отображался заголовок страницы.Я могу получить заголовок страницы с кодом ниже, но не знаю, как и где его реализовать.

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

Я включил файлы .m и .h для двух представленных ниже контроллеров ViewController.Пожалуйста, покажи / скажи мне, что делать.

Спасибо!

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;
}

1 Ответ

5 голосов
/ 22 июня 2011

В ExplorerViewController.m заменить:

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

по:

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

Если вам нужен также URL-адрес, добавьте NSArray вместо NSString для хранения URL-адреса и заголовка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...