将UIWebView推送到UIViewController上

时间:2010-11-15 02:06:07

标签: uiwebview push

我看到的几乎所有例子都是用IB完成的,但我不想使用IB。

我想要做的是,当用户选择表格中的一行时,UIWebView将被推入堆栈并加载该特定页面,同时保留标签栏和导航栏。我不希望浏览器的所有功能都只能滚动浏览页面,因为我的应用程序的其余部分控制着人们如何通过表格浏览网站。

所以我已经能够推送其他视图控制器,但使用相同的方法推送UIWebView不起作用。

这是我到目前为止所拥有的......

这是Threads.h文件

#import "ThreadContent.h"
#import <UIKit/UIKit.h>


@interface Threads : UITableViewController {
NSMutableArray *threadName;
NSMutableArray *threadTitle; 
UIActivityIndicatorView *spinner;
NSOperationQueue *operationQueue;
UILabel *loadingLabel;
NSMutableDictionary *cachedForum;
NSMutableArray *forumID;
NSInteger *indexPathRowNumber;
NSMutableArray *threadID;

ThreadContent *threadContent;
}

@property (nonatomic, assign) NSMutableArray *forumID;
@property (nonatomic, assign) NSInteger *indexPathRowNumber;

@end

我正在尝试推送UIWebView

的Threads.m文件的一部分
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%i",indexPath.row);//get row number
NSLog(@"%@", [threadID objectAtIndex:indexPath.row]);//thread id
                                                     //forums.whirlpool.net.au/forum-replies.cfm?t=
                                                     //NSString *urlString = [NSString stringWithFormat:@"forums.whirlpool.net.au/forum-replies.cfm?t=%@",  [threadID objectAtIndex:indexPath.row]];
threadContent = [[ThreadContent alloc] init];
[self.navigationController pushViewController:threadContent animated:YES];
}

我的WebView文件..好吧,我不知道该怎么做?我确实把它变成了一个“UIWebView”的子类,但是如果我试着把它推到堆栈上,我就会发现它需要它成为一个“UIViewController”子类的崩溃。

1 个答案:

答案 0 :(得分:5)

UIWebView是UIView的子类,而不是UIViewController。

您需要继承UIViewController(将其称为WebViewController)

viewDidLoad方法中创建一个UIWebView并使用addSubview:将其添加到视图中 您可以将URL作为WebViewController的属性

传递到webView中
-(void)viewDidLoad {
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubView:webView];
    [webView loadRequest:[NSURLRequest requestWithURL:self.urlToLoad]];
    [webView release];
}

并在Threads

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%i",indexPath.row);//get row number
    NSLog(@"%@", [threadID objectAtIndex:indexPath.row]);//thread id
                                                     //forums.whirlpool.net.au/forum-replies.cfm?t=
                                                     //NSString *urlString = [NSString stringWithFormat:@"forums.whirlpool.net.au/forum-replies.cfm?t=%@",  [threadID objectAtIndex:indexPath.row]];
    threadContent = [[ThreadContent alloc] init];
    threadContent.urlToLoad = [NSURL URLWithString:urlString];
    [self.navigationController pushViewController:threadContent animated:YES];
}

(或者将webView作为属性并在Threads中推送WebViewController之前或之后调用load方法,我不能100%确定这种方式是否有效)