我有一个视图控制器,我想在视图加载时从我的根视图控制器呈现。
我已将呈现视图控制器设置为呈现视图的委托。
我有一个UIBarbutton
,当我点击时,使用与viewDidLoad
完全相同的方法,在我尝试的viewDidLoad
方法中,视图显示没有问题提出观点:
modalViewController.delegate = self;
modalViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:modalViewController animated:YES];
它不起作用。
尝试在viewDidLoad
方法中显示视图是否有问题?
整个viewDidLoad方法:
- (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];
}
答案 0 :(得分:4)
您无法从viewDidLoad
展示您的视图控制器,尤其不是animated
。此时您可能仍在创建子视图,因此甚至还没有呈现视图。
相反,您应该尝试在viewDidAppear
中展示您的模态视图控制器。