IOS开发。点击按钮,屏幕不会调用新视图

时间:2014-05-06 04:36:49

标签: ios button view

目标:单击按钮时切换视图。 我做了什么:

  1. 打开单个视图应用程序。

  2. 档案 - >新 - >档案 - > Cocoa Touch&目标-C。创建 PlayViewController和GMViewController,都是子类 的UIViewController。

  3. 去了mainstoryboard,从对象库中拖了两个 查看控制器,使用PLayViewController和GMViewController作为 他们的STORYBOARD ID。

  4. 在ViewController.h中我有以下代码:

  5. #import <UIKit/UIKit.h>
    
        @class PlayViewController;
        @class GMViewController;
    
    @interface ViewController : UIViewController
    
    @property (strong, nonatomic) PlayViewController *playViewController;
    @property (strong, nonatomic) GMViewController *gmViewController;
    
    
    - (IBAction)toPlay:(id)sender;
    - (IBAction)toGM:(id)sender;
    
    @end
    

    基本上,我在这里创建了两个按钮。一个将转到PlayView,另一个转到GMView。

     5. In my ViewCotroller.m, I have these:
    
     #import "ViewController.h"
     #import "PLayViewController.h"
     #import "GMViewController.h"
    
     - (IBAction)toPlay:(id)sender {
    
    UIStoryboard *storyboard =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PlayViewController *playController =[storyboard instantiateViewControllerWithIdentifier:@"PlayViewController"];
    self.playViewController = playController;
    
    
    [self.view removeFromSuperview];
    [self.view insertSubview: self.playViewController.view atIndex:0];
    

    }

    - (IBAction)toGM:(id)sender {
    //still blank, waiting to fix the play button.
    

    }

    问题是当我点击播放按钮时屏幕变黑。请告诉我我错过了什么。感谢

1 个答案:

答案 0 :(得分:1)

根据您的代码,您首先删除self.view,然后尝试insertSubview self.view self.playViewController.view这样做不适合您,因此请使用

首先对self.view进行了[self.view insertSubview: self.playViewController.view atIndex:0]; [self.view removeFromSuperview]; ,然后将其从超级视图中删除,例如,

{{1}}

希望我的建议适合你。