简体   繁体   English

在viewDidLoad之后如何更新UIWebView?

[英]How Do I Update UIWebView After viewDidLoad?

This is my first iOS app, so I am probably missing something very simple. 这是我的第一个iOS应用程序,因此我可能缺少一些非常简单的东西。 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. 基本上,这是一个仅加载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. 我有一个要连接的外部附件(蓝牙条形码扫描仪),基本上我想做的是当应用收到扫描后,我想在ViewController中调用一个方法并相应地更新UIWebView。

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. 我似乎无法弄清楚如何从控制器中的方法更新UIWebView。 It logs the url string to my debugger area, but never actually updates the webview. 它将url字符串记录到我的调试器区域,但从未真正更新Webview。 I am pretty sure I have some delegation wrong or something with my webview instance. 我敢肯定我的Webview实例存在某些委派错误或某些错误。 There must be some glue code here that I am missing. 这里一定有一些我缺少的胶水代码。

My Code HelloWorldViewController.h 我的代码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 我的代码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. 基本上,我只是在扫描条形码时尝试将google.com加载到我的网页网页视图中。 My log statements are being logged with the correct URL, but this line of code doesn't work. 我的日志语句使用正确的URL进行了记录,但是这一行代码不起作用。

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

I am not getting any errors and my xCode debugging skills are not the greatest. 我没有收到任何错误,我的xCode调试技能也不是最好的。

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. 您可能正在与一个nil实例交谈。

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: 除非您的Web视图来自XIB文件(我怀疑这是因为您的heder文件中未将其声明为IBOutlet),否则请尝试将以下内容添加到viewDidLoad中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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