I have a view controller that I want to present from my root view controller when the view is load.
I have set the presenting view controller as the delegate for the presented view.
I have a UIBarbutton that when I tap, the view is presented with no problem using a method that does the exact same thing as the viewDidLoad, in the viewDidLoad method I try to present the view:
modalViewController.delegate = self;
modalViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:modalViewController animated:YES];
it’s not working.
Is it a problem to try to present the view in the viewDidLoad method?
The whole viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navigationBar.tintColor = [UIColor blackColor];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@" "
style:UIBarButtonItemStyleDone
target:self
action:@selector(changePlayersNames)];//Check how to highlight the button
//Label for UIBarButton text
UILabel *barButtonLabel = [[UILabel alloc] initWithFrame:CGRectMake(3, 5, 70, 30)];
barButtonLabel.text = @"Players";
[barButtonLabel setFont:[UIFont fontWithName:@"ChalkboardSE-Bold" size:15.0]];
barButtonLabel.textAlignment = UITextAlignmentCenter;
barButtonLabel.backgroundColor = [UIColor clearColor];
barButtonLabel.textColor = [UIColor whiteColor];
//Set UINavigation item
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@""];
item.leftBarButtonItem = leftBarButton;
item.hidesBackButton = YES;
[navigationBar pushNavigationItem:item animated:YES];
//Label for the navigation title
UILabel *barTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 30)];
barTitleLabel.center = navigationBar.center;
barTitleLabel.text = @"Tic-Tac-Toe";
[barTitleLabel setFont:[UIFont fontWithName:@"ChalkboardSE-Bold" size:20.0]];
barTitleLabel.textAlignment = UITextAlignmentCenter;
barTitleLabel.backgroundColor = [UIColor clearColor];
barTitleLabel.textColor = [UIColor whiteColor];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:navigationBar];
[self.view addSubview:barTitleLabel];
[self.view addSubview:barButtonLabel];
self.player1 = [[ORDPlayer alloc] init];
self.player2 = [[ORDPlayer alloc] init];
[self setNamesLabels:@"Player 1" :@"Player 2"];
//TapRecognizer
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapRecognized:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
singleTap.delegate = self;
[self.view addGestureRecognizer:singleTap];
//Present modal view
self.playerChangeController = [[ORDPlayerChangeController alloc]
initWithNibName:@"ORDPlayerChangeController" bundle:nil];
playerChangeController.delegate = self;
playerChangeController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:playerChangeController animated:YES];
}
You cannot present your view controller from
viewDidLoad, especially notanimated. At that point you might still be creating subviews, so the view is not even rendered yet.Instead, you should try presenting your modal view controller in
viewDidAppear.