2011-06-22 104 views
1

我有一個完全可操作的Web瀏覽器應用程序,用於存儲書籤頁。當點擊書籤按鈕時,顯示存儲網站的列表視圖。我不希望顯示URL,而是希望列表視圖顯示頁面的標題。我能夠用下面的代碼獲得頁面的標題,但不知道如何或在哪裏實現它。iPhone書籤UIWebView - 顯示頁面標題而不是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; 
} 

回答

5

在ExplorerViewController.m,替換:

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

由:

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

如果你也需要有URL,添加一個NSArray代替的NSString存儲網址和標題。

+0

謝謝!我需要存儲網址和標題,這樣當標題被點擊時,UIWebView會轉到URL。我對iPhone編程非常陌生,有什麼方法可以告訴我如何在上面的代碼中執行此操作? – Brandon

+0

因此改爲: NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:@「kURL」,[[self.webView request] URL] absoluteString],@「kTitle」,[self.webView stringByEvaluatingJavaScriptFromString:@「document.title」 ],無]; [書籤addObject:dictionary]; 並在 - tableView:cellForRowAtIndexPath :(在BookmarksViewController.m中),替換: cell.textLabel.text = [bookmarks objectAtIndex:indexPath.row]; 作者: cell.textLabel.text = [[書籤objectAtIndex:indexPath.row] objectForKey:@「kTitle」]; 並實現tableView:didSelectRowAtIndexPath:在選中單元格時執行動作。 – Johnmph