【问题标题】:launch a webview programmatically from another view controller error从另一个视图控制器错误以编程方式启动 webview
【发布时间】:2017-06-15 16:50:42
【问题描述】:

这是我的主视图控制器:

#import "ViewController.h"
#import "WebViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    WebViewController *wvc = [[WebViewController alloc]init];
    [self presentViewController:wvc animated:NO completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

这里是 webviewcontroller:

#import "WebViewController.h"

@interface WebViewController ()

@end

@implementation WebViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
    NSString *url=@"https://www.google.com";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];
    [self.view addSubview:webview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

谷歌页面没有加载,我得到的警告是

警告:尝试在 谁的视野不在窗内 等级制度!

这到底是怎么回事?

【问题讨论】:

  • 您不能在viewDidLoad 中显示视图控制器,请在viewDidAppear 中执行
  • 是否给出了“警告:尝试在 上显示 ,其视图不在窗口层次结构中!”

标签: ios objective-c uiwebview


【解决方案1】:

我建议仔细检查。按照您的代码,我刚刚确认了这一点:

#import "LoadPresentViewController.h"
#import "WebViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)doLoadPresent:(id)sender {

    WebViewController *wvc = [[WebViewController alloc]init];
    [self presentViewController:wvc animated:NO completion:nil];

}


- (void)viewDidLoad {
    [super viewDidLoad];

    // this fails in viewDidLoad
//  WebViewController *wvc = [[WebViewController alloc]init];
//  [self presentViewController:wvc animated:NO completion:nil];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // this succeeds in viewDidAppear
    WebViewController *wvc = [[WebViewController alloc]init];
    [self presentViewController:wvc animated:NO completion:nil];

}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多