18

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:)];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
hitheredude
  • 929
  • 5
  • 18
  • 32

3 Answers3

39

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; 
makboney
  • 1,808
  • 1
  • 17
  • 34
  • makboney got it in one. You dont even need to add a back button, just push the view controller from whatever class you were in and if that class contains a nav controller there'll be a back button by default – Elmo Nov 29 '11 at 11:48
  • yup...dont break the protocol.you shouldn't use that Back Button in that way. – makboney Nov 29 '11 at 11:50
  • Are you talking to me or lav? I thought goBack only worked within a webview opened within a webview – Elmo Nov 29 '11 at 12:22
2

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.

Srinivas
  • 983
  • 9
  • 21
1

Add the below code into your viewcontroller.m file viewDidLoad method.

UIWebView *webView = [[UIWebView alloc]init];
NSString *urlString = @"http://www.sourcefreeze.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[self.view addSubview:webView];

for complete uiwebview example please refer the below link.

https://sourcefreeze.com/ios-webview-uiwebview-example/

Bharathi Devarasu
  • 7,759
  • 2
  • 18
  • 23