【问题标题】:iPhone Bookmark UIWebView - Displaying Page Title and Storing URLiPhone Bookmark UIWebView - 显示页面标题和存储 URL
【发布时间】:2011-06-23 00:24:46
【问题描述】:

我有一个完全可操作的 Web 浏览器应用程序,用于存储书签页面。单击书签按钮时,将显示已存储网站的列表视图。我希望列表视图显示页面标题,而不是显示 URL,但我希望 UIWebView 在单击标题时转到 URL。

我已包含以下代码。我也将属性放在两个头文件中,但无法使其正常工作。请帮忙!

ExplorerViewController.h

#import <UIKit/UIKit.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

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSMutableArray *bookmarks = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"Bookmarks"] mutableCopy];
        NSMutableArray *websitetitle = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"Websitetitle"] mutableCopy];
        if (!bookmarks) {
            bookmarks = [[NSMutableArray alloc] init];
        }
        [bookmarks addObject:[[[[self webView]request] URL] absoluteString]];
        [websitetitle addObject:[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"]];
        [[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:@"Bookmarks"];
        [[NSUserDefaults standardUserDefaults] setObject:websitetitle forKey:@"Websitetitle"];
        [bookmarks release];
        [websitetitle release];
    }
}

BookmarksViewController.h

#import <UIKit/UIKit.h>

@class ExplorerViewController;
@interface BookmarksViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    NSMutableArray *bookmarks;
    NSMutableArray *websitetitle;
    ExplorerViewController *explorerView;
}

@property (nonatomic, retain) NSMutableArray *bookmarks;
@property (nonatomic, retain) NSMutableArray *websitetitle;
@property (nonatomic, retain) ExplorerViewController *explorerView;

-(IBAction)cancelButtonTapped;

@end

BookmarksViewController.m

#import "BookmarksViewController.h"
#import "ExplorerViewController.h"

@implementation BookmarksViewController
@synthesize bookmarks, websitetitle, 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 = [websitetitle objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    explorerView.urlField.text = [bookmarks objectAtIndex:indexPath.row];
    [explorerView textFieldShouldReturn:explorerView.urlField];
    [self.parentViewController dismissModalViewControllerAnimated:true];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [bookmarks removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
        [[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:@"Bookmarks"];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
    // e.g. self.myOutlet = nil;
    [explorerView release];
    explorerView = nil;
    [bookmarks release];
    bookmarks = nil;
}

【问题讨论】:

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


    【解决方案1】:

    你要实现tableView:didSelectRowAtIndexPath:,大致如下:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
      NSString *url=[bookmarks objectAtIndex:indexPath.row];
    
      // open url here
    
    }
    

    关于您的代码的一些注意事项:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
      explorerView.urlField.text = [bookmarks objectAtIndex:indexPath.row];
      [explorerView textFieldShouldReturn:explorerView.urlField];
      [self.parentViewController dismissModalViewControllerAnimated:true]; 
    
    } 
    
    • 您是否尝试复制 Mobile Safari?
    • 您应该避免将书签存储在 NSUserDefaults 存储中,并使用适当的存储。
    • 您不应尝试通过模拟交互事件来触发导航操作;使 UIWebView(您的底层浏览器对象)加载网页的正确方法是使用 loadRequest:

    以下代码可能对你有用(我在这里依赖很多假设):

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
      // Get your url from the bookmarks object
      NSString *urlString = [bookmarks objectAtIndex:indexPath.row];
    
      // Convert to a URL object.
      NSURL *url = [NSURL URLWithString:urlString];
    
     // Create request
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    
     //Load the request in the UIWebView.
     [explorerView.webView loadRequest:requestObj];
    
     // dismiss
     [self.parentViewController dismissModalViewControllerAnimated:true]; 
    
    } 
    

    【讨论】:

    • 这是我拥有的类似于您的答案的代码:请让我知道我需要做什么。 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { explorerView.urlField.text = [bookmarks objectAtIndex:indexPath.row]; [explorerView textFieldShouldReturn:explorerView.urlField]; [self.parentViewControllerdismissModalViewControllerAnimated:true]; }
    • @brandon 将其粘贴到您的原始问题中,请不要在这里。
    • 好的,编辑原始问题以包含所有代码,包括 2 个头文件。我已经坚持了72小时。感谢您的帮助!
    • 有没有办法用 NSUserDefaults 存储来完成这个?我能够成功地让它存储 URL,但不是 html 页面的标题
    • 谢谢!看起来我的信息是否已成功存储在 ExplorerViewController.m 中,并已成功在 BookmarksViewController.m 中调用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多