这可能很容易,但我今天坚持下去。这个想法是,在我的浏览器中,我创建了 uiwebview 并且我想在弹出窗口中使用它自己的类来暗示地址栏。我可以从 UItextfield 获取从 popover 类到 webview 类的 url,但是当我得到它时,uiwebview 变得懒惰并且它不会加载它。当我检查它时,调试器说 webview 为空。这是 ViewController.h
#import <UIKit/UIKit.h>
#import "AdressBar.h"
#import "mypopoverController.h"
@interface ViewController : UIViewController<AddressbarDelegate>
{
UIWebView* mWebView;
mypopoverController *popoverController;
}
@property (nonatomic, retain) IBOutlet UIWebView* webPage;
@end
这是 ViewController.m:
#import "mypopoverController.h"
#import "MyOwnPopover.h"
#import "ViewController.h"
#import "AdressBar.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize webPage = mWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
addressBar = [[AdressBar alloc] init];
addressBar.delegate = self;
[edittext addTarget:self action:@selector(showPopoverAdressBar:forEvent:) forControlEvents:UIControlEventTouchUpInside];
NSURL *url = [NSURL URLWithString:@"http://www.google.lv"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[mWebView setScalesPageToFit:YES];
[mWebView setDelegate:self];
[mWebView loadRequest:request];
}
-(void)loadReceivedAddress:(NSURLRequest *)url{
NSLog(@"url= %@", url);//there url always is not null and mWebView should load it
if(mWebView != nil){
[mWebView loadRequest:url];
}else{
NSLog(@"mWebView is null");//...but there it say's that it's null
}}
-(void)showPopoverAdressBar:(id)sender forEvent:(UIEvent*)event
{
AdressBar *popoverControllesr = [[AdressBar alloc]init];
popoverControllesr.view.frame = CGRectMake(0,0, 600, 45);
popoverControllesr.view.backgroundColor = [UIColor whiteColor];
popoverController = [[mypopoverController alloc] initWithContentViewController:popoverControllesr];
popoverController.cornerRadius = 20;
if(_titles!=NULL){
popoverController.titleText = _titles;}else{
popoverController.titleText = @"Loading...";
}
popoverControllesr.address.text = absoluteString;
popoverController.popoverBaseColor = [UIColor orangeColor];
popoverController.popoverGradient= YES;
popoverController.arrowPosition = TSPopoverArrowPositionHorizontal;
[popoverController showPopoverWithTouch:event];
}
@end
这是 AdressBar.h
#import <UIKit/UIKit.h>
@protocol AddressbarDelegate <NSObject>
@required
-(void)loadSomethingFromAddressBar:(NSURLRequest*)request;
@end
@interface AdressBar : UIViewController{
IBOutlet UIButton *cancel;
}
@property (nonatomic, retain) IBOutlet UITextField *address;
@property (nonatomic, retain) NSURLRequest *request;
@property(nonatomic, weak) id <AddressbarDelegate> delegate;
@end
这是 AdressBar.m:
#import "AdressBar.h"
#import "ViewController.h"
@interface AdressBar ()
@end
@implementation AdressBar
@synthesize delegate = delegate;
- (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 from its nib.
[_address setDelegate:self];
_address.clearButtonMode =
UITextFieldViewModeWhileEditing;
_address.keyboardType = UIKeyboardTypeURL;
[_address addTarget:self
action:@selector(loadAddresss)
forControlEvents:UIControlEventEditingDidEndOnExit];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadAddresss {
NSString* urlString = _address.text;
NSURL* url = [NSURL URLWithString:urlString];
if(!url.scheme)
{
NSString* modifiedURLString = [NSString stringWithFormat:@"http://%@", urlString];
url = [NSURL URLWithString:modifiedURLString];
}
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSLog(@"request= %@", request);
NSLog(@"text= %@", urlString);
if (request!=nil) {
if(delegate!=nil)
{NSLog(@"delegate not nil");
[delegate loadSomethingFromAddressBar:request];
}else{
NSLog(@"delegate is nil");//There delegate always is nil
}
}
}
@end