简体   繁体   中英

How Do I Update UIWebView After viewDidLoad?

This is my first iOS app, so I am probably missing something very simple. Please be kind. I have been tearing my hair out and I could really use some help.

Overview Of App

Basically, this is a single page application that just loads a UIWebView. I have an external accessory (bluetooth barcode scanner) that I connect and basically what I want to do is when the the app receives a scan, I want to call a method in my ViewController and update the UIWebView accordingly.

What Is Working

I am able to connect the scanner, load the first view, which loads the initial webpage, scan a barcode and call the method in my controller.

My Problem

I can't seem to figure out how to update the UIWebView from the method in my controller. It logs the url string to my debugger area, but never actually updates the webview. I am pretty sure I have some delegation wrong or something with my webview instance. There must be some glue code here that I am missing.

My Code HelloWorldViewController.h

#import <UIKit/UIKit.h>
#import "KScan.h"

@interface HelloWorldViewController : UIViewController <UIWebViewDelegate> { 

  IBOutlet UIWebView *page;
  IBOutlet UILabel *myLabel;

  Boolean IsFirstTime;

  KScan *kscan;

}

- (void)setFirstTime;
- (void)DisplayConnectionStatus; 
- (void)DisplayMessage:(char *)Message; 
- (void)newBarcodeScanned:(NSString *)barcode;
- (void)loadBarcodePage:(NSString *)barcode;

@property (nonatomic, retain) KScan *kscan;
@property (nonatomic, retain) UIWebView *page;
@property (nonatomic, retain) UILabel *myLabel;

@end 

My Code HelloWorldViewController.m

#import "HelloWorldViewController.h"
#import "common.h"

@implementation HelloWorldViewController

@synthesize myLabel;
@synthesize page;
@synthesize kscan;


- (void)setFirstTime
{
    IsFirstTime = true;
}


- (void)viewDidLoad
{
    self.kscan = [[KScan alloc] init];

    [super viewDidLoad];
    page.scrollView.bounces = NO;

    //page.delegate = self;

   [page loadRequest:[NSURLRequest requestWithURL:[NSURL   URLWithString:@"http://192.168.0.187:3000"]]];

}


- (void) newBarcodeScanned:(NSString *)barcode
{
NSLog(@"%s[%@]",__FUNCTION__, barcode);

[self loadBarcodePage:barcode];
} 

- (void)loadBarcodePage:(NSString *)barcode
{
NSLog(@"%s",__FUNCTION__);
NSString *url = [[NSString alloc] initWithFormat:@"http://www.google.com/%@", barcode];
NSLog(@"%@", url);
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; 

}

- (void)viewDidUnload
{
    [myLabel release];
    myLabel = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:   (UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
       return YES;
    }
}

- (void)dealloc {
   [page release];
   [kscan release];
   [myLabel release];
   [super dealloc];
}
@end

Basically, I am just trying to load google.com into my page webview when scanning a barcode. My log statements are being logged with the correct URL, but this line of code doesn't work.

[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

I am not getting any errors and my xCode debugging skills are not the greatest.

Any help would be greatly appreciated!

It looks like your webview is never allocated, or added to your main view. You are probably talking to a nil instance.

Unless your web view comes from a XIB file (which I doubt since it is not declared as an IBOutlet in your heder file) try adding something like this to your viewDidLoad:

self.page = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.page];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM