-3

这可能很容易,但我今天坚持下去。这个想法是,在我的浏览器中,我创建了 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
4

2 回答 2

0

如果这是您的视图层次结构

   ---- In ViewController 
       ---- Showing AddressBar View in POPOver
   ---- Remove POPover and display ViewController's web view

我建议在 AddressBar 中创建一个自定义委托方法,并在您删除 popOver 时触发委托方法。在您的 ViewContrller 中实现委托并在该实现的委托方法中调用 loadSomethingFromAddressBar

注意:确保您已将网页 IBOutlet 连接到您的 nib 文件。

// 在地址栏.h

@protocol AddressbarDelegate <NSObject>
@required
   -(void)loadYourWebViewNow:(NSURLRequest*)request;
@end

@interface Addressbar : UIViewController
{
}

@property(nonatomic, weak) id <AddressbarDelegate>

// 在地址栏.m

    - (void)loadAddresss {
      NSString* urlString = _address.text; //geting text from UItextField
      NSURL* url = [NSURL URLWithString:urlString];
      if(!url.scheme)
      {
          NSString* modifiedURLString = [NSString stringWithFormat:@"http://%@", urlString];
          url = [NSURL URLWithString:modifiedURLString];
      }
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
     if (request!=nil) {
     NSLog(@"request is good");
     if(_delegate!=nil)
     {
       [_delegate loadYourWebViewNow:request];
     }

 }

// 在你的 ViewController.h

@interface ViewController : UIViewController<AddressbarDelegate>
{
    //AdressBar *addressBar;
}

// 在你的 ViewController.m 中实现委托方法并设置委托

@implementation ViewController

-(void)viewDidLoad
{
    Remove these two below lines  on viewDidLoad
     //addressBar = [[Adressbar alloc] init];
     //addressbar.delegate = self;
}

-(void)loadYourWebViewNow:(NSURLRequest*)request
{
    [self loadSomethingFromAddressBar:request];
}


-(void)showPopoverAdressBar:(id)sender forEvent:(UIEvent*)event
{
AdressBar *popoverControllesr = [[AdressBar alloc]init];
popoverControllesr.delegate = self; // set the delegate here to this object. 
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];

}
于 2013-10-21T11:37:00.837 回答
0

Make sure that your web view Outlet & Delegate is correctly conncted.

URL 地址之间没有空格,因为我也面临同样的问题。尝试在网络浏览器中打开该网址。

于 2013-10-21T12:54:52.680 回答