0

I have a UIViewController inside of which I put a UIWebView

Then I added this to the .h file:

#import <UIKit/UIKit.h>

@interface BuildBusinessController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *theWebView;

@end

and have this as the .m file

#import "BuildBusinessController.h"

@interface BuildBusinessController ()

@end

@implementation BuildBusinessController
@synthesize theWebView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    theWebView.delegate = self;
}

- (void)viewDidUnload
{
    [self setTheWebView:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)viewDidAppear:(BOOL)animated
{  
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"build_business" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:htmlFile];
    NSURLRequest *rq = [NSURLRequest requestWithURL:url];
    [theWebView loadRequest:rq];
}

@end

But the build_business.html file is not rendering inside the view, and also I get a warning on this line: theWebView.delegate = self; which says:

Assigning to id 'UIWebViewDelegate from incompatible type BuildBusinessController *const_string

Would anyone know what I am doing wrong here? It feels I am forgetting some one small thing :)

2 Answers 2

1

In addition to F.X. answer, I think you forgot to add this in viewDidAppear (after loadRequest):

[self.view addSubview:theWebView];

Update: The issue is caused by OP failing to configure delegate for UIWebView in Storyboard. The code snippet provided here will only work if you are coding it manually. Otherwise, as stated by @F.X. in the comments below, Storyboard will implement this automatically.

6
  • sorry, where exactly should that line be? I am not noticing the loadRequest - you mean as the last line in viewDidAppear - that didn't work.
    – GeekedOut
    Oct 5, 2012 at 19:01
  • Just to check, did you create the web view using storyboard?
    – Alan
    Oct 5, 2012 at 19:13
  • yes I created the view using the storyboard, but I wrote the code manually, which might be the problem.
    – GeekedOut
    Oct 5, 2012 at 19:22
  • I wasn't sure how to align the windows of xcode so I can drag the arrow from the storyboard to the .h file
    – GeekedOut
    Oct 5, 2012 at 19:23
  • @AlanYeo: There's no need for that, the Storyboard automatically creates and adds the view :)
    – F.X.
    Oct 5, 2012 at 19:53
1

You're just forgetting UIWebViewDelegate on your class definition :

#import <UIKit/UIKit.h>

@interface BuildBusinessController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *theWebView;

@end
10
  • although I just tried it and it still didn't fully work :( although it did get rid of the error
    – GeekedOut
    Oct 5, 2012 at 18:45
  • Could the problem be that I use an underscore in my html file build_business ?
    – GeekedOut
    Oct 5, 2012 at 18:46
  • I don't think so. Have you verified that the outlet is set? I also suppose the UIWebView object isn't null... Have you tried looking directly inside the bundle in the Simulator to see if the HTML files are actually there?
    – F.X.
    Oct 5, 2012 at 18:49
  • I did not add that html file specifically to the bundle - should I do that?
    – GeekedOut
    Oct 5, 2012 at 18:53
  • Yes. You should do that. But just to make sure you have everything setup, you should test this out by doing (instead of using a file): NSURL *url = [NSURL URLWithString:@"stackoverflow.com"];
    – Alan
    Oct 5, 2012 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.