Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a UIWebView and load a website programmatically

The following is my code. I simply want to load a website page and put a back button on screen. Don't know why nothing shows on the screen.

in .h

#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController
{
    UIWebView *myWebView;
}
@property (nonatomic, retain) UIWebView *myWebView;
-(IBAction)goBack:(id)sender;
@end

in .m

#import "ThirdViewController.h"
@implementation ThirdViewController
@synthesize myWebView;
-(IBAction)goBack:(id)sender
{
    [myWebView goBack];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *urlAddress = @"http://www.apple.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [myWebView loadRequest:requestObj];
    [self.view addSubview:myWebView];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]     initWithTitle:@"Back"                                                                              style:UIBarButtonItemStylePlain                                                                         target:self action:@selector(goBack:)];
}
like image 386
lavitanien Avatar asked Nov 29 '11 11:11

lavitanien


2 Answers

Initialize your webView.

UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:theFrame];
NSString *urlAddress = @"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.myWebView = tempWebview;
[tempWebview loadRequest:requestObj];
[tempWebview release];
myWebView.delegate=self; 
like image 66
makboney Avatar answered Sep 20 '22 16:09

makboney


First Initialize Web view .Write The Below Code In Between [Super ViewDidLoad]; and Nsstring Inisilization.

myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myWebView.delegate=self;
[self.view addSubview:myWebView]

Then Load Request Your Code Will Work.

like image 26
Srinivas Avatar answered Sep 18 '22 16:09

Srinivas