这是我的主要视图控制器:
#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
这是网络视图控制器:
#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.
}
谷歌页面没有加载,我得到的警告是
这警告:尝试在 其视图不在窗口中 等级制度!
到底是怎么回事?
我建议仔细检查。按照您的代码,我刚刚确认了这一点:
#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