« Back
in ios read.

Whose view is not in the window hierarchy!.

This time, when I was doing a ship battle game for iOS in XCode with objective-c, it shows a very interesting warning.

2015-10-15 16:05:59.278 Lyu-Lab5[4505:1418041] Warning: Attempt to present  on  whose view is not in the window hierarchy!

I tried to find solutions towards this warning.

This is my original code: The function is showup a UIActionSheet to let users choose gamemode that they want:


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view setBackgroundColor:[UIColor whiteColor]];




    [self gamemodechoose];
}


-(void)gamemodechoose{
    modechoose = [[UIActionSheet alloc]
                  initWithTitle:@"Choose game mode"
                  delegate:self
                  cancelButtonTitle:nil
                  destructiveButtonTitle:nil
                  otherButtonTitles:@"one player", @"multiplayer", nil];
    modechoose.tag=1;
    [modechoose showInView:self.view];
}

The problem happened when I want to add the UIActionSheet into the view. When I put the function in - (void)viewDidLoad do not mean the window already pop into current view.

So the solution is move that method into -(void) viewDidAppear. Then it is safe to add subview into current view.

The reason why previous project I didn't encounter this warning was I used storyboard, which will load first.

So, if you do not want to use storyboard, always use -(void) viewDidAppear to be secure.

comments powered by Disqus