简体   繁体   English

呈现一个视图控制器

[英]presenting a view controller

I want to go to a view controller when the button "Cancel" is pressed.我想在按下“取消”按钮时转到视图控制器。 I tried using this method but when I click 'Cancel", it just displays a black screen. I am a beginner at IOS.我尝试使用这种方法,但是当我单击“取消”时,它只显示黑屏。我是 IOS 的初学者。

 -(void) cancelButtonPressed {

    homeView = (HomeViewController *)[[UIViewController alloc]init];
    [self presentViewController:homeView animated:YES completion:nil];
}

EDITED FOR COMMENTS:编辑评论:

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonPressed)];
 self.navigationItem.leftBarButtonItem = cancelButton;

Try doing this:尝试这样做:

     HomeViewController *homeView = [[HomeViewController alloc]init];
     [self presentViewController:homeView animated:YES completion:nil];

Also note that black is the default colour of the window.另请注意,黑色是窗口的默认颜色。 If you don't have any view controller named HomeViewController, and you present it, by default it will be visible as black colour to the user.如果你没有任何名为 HomeViewController 的视图控制器,并且你展示了它,默认情况下它会以黑色显示给用户。

To Avoid this, you need to set the View for the newly presented view controller, which you can access by homeView.view为了避免这种情况,您需要为新呈现的视图控制器设置视图,您可以通过homeView.view访问

If you are using storyboard, then use storyboard ID:如果您使用的是故事板,则使用故事板 ID:

     HomeViewController *homeView = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
      [self presentViewController:homeView animated:YES completion:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM