Pushing UIWebView onto UIViewController

Almost all the examples I see are done with IB but I don't want to use IB.

What I want to do is, when a user selects a row in the table a UIWebView will be pushed onto the stack and load that particular page while keeping the tab bar and navigation bar. I don't want all the features of a browser rather just be able to scroll through a page beacause the rest of my app controls how a person can navigate through the website through tables.

So I've been able to push other viewcontrollers but pushing a UIWebView with the same method doesn't work.

Here is what I have so far..

This is the Threads.h file

#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

Part of my Threads.m file where I am trying to push the UIWebView

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

My WebView files.. well i'm not sure how to do that? I did make it a "UIWebView" subclass but if i try to push it on to the stack i get a crash saying it needs it to be a "UIViewController" subclass.


UIWebView is a subclass of UIView not UIViewController.

You need to subclass UIViewController (call it for example WebViewController)

And in the viewDidLoad method create a UIWebView and add it to the view, using addSubview: You can pass the URL to load in the webView as a property of the WebViewController

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

and in 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];
}

(or make the webView a property and call the load method in just before or after you push the WebViewController in Threads , I'm not 100% sure if this way would work)

链接地址: http://www.djcxy.com/p/34780.html

上一篇: App Browser not loading?

下一篇: 将UIWebView推入UIViewController