如何加载url,什么来自popoverclass?

时间:2013-10-21 11:29:05

标签: ios objective-c uiwebview

这可能很容易,但我今天坚持了。 我的想法是,在我的浏览器中,我创建了uiwebview,并且我希望使用自己的类来修改popover中的地址栏。 我可以从UItextfield获取从popover类到webview类的URL,但是当我得到它时,uiwebview会变得很懒,并且它不会加载它。 当我检查它时,debuger说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

2 个答案:

答案 0 :(得分:0)

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

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

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

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

//在Adressbar.h

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

@interface Addressbar : UIViewController
{
}

@property(nonatomic, weak) id <AddressbarDelegate>

//在Adressbar.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];

}

答案 1 :(得分:0)

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

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