-1

Here is my main view controller:

#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

Here is 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.
}

The google page does not get loaded and the warning I get is

Warning: Attempt to present on whose view is not in the window hierarchy!

What the hell is going on?

dan
  • 9,695
  • 1
  • 42
  • 40
Ackman
  • 1,562
  • 6
  • 31
  • 54
  • Already reported in Radar - https://stackoverflow.com/questions/39520499/class-plbuildversion-is-implemented-in-both-frameworks – Subramanian P Jun 15 '17 at 16:58
  • 1
    You can't present a view controller in `viewDidLoad`, do it in `viewDidAppear` – dan Jun 15 '17 at 17:06
  • Did that gives "Warning: Attempt to present on whose view is not in the window hierarchy!" – Ackman Jun 15 '17 at 17:18

1 Answers1

1

I'd suggest double-checking. Following your code, I just confirmed this:

#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
DonMag
  • 69,424
  • 5
  • 50
  • 86