【问题标题】:Pushing UIWebView onto UIViewController将 UIWebView 推送到 UIViewController
【发布时间】:2011-05-10 00:22:57
【问题描述】:

我看到的几乎所有示例都是用 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”子类。

【问题讨论】:

    标签: uiwebview push


    【解决方案1】:

    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 设为属性并在您将 WebViewController 推送到 Threads 之前或之后调用 load 方法,我不能 100% 确定这种方式是否可行)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      相关资源
      最近更新 更多