目标:单击按钮时切换视图。 我做了什么:
打开单个视图应用程序。
档案 - >新 - >档案 - > Cocoa Touch&目标-C。创建 PlayViewController和GMViewController,都是子类 的UIViewController。
去了mainstoryboard,从对象库中拖了两个 查看控制器,使用PLayViewController和GMViewController作为 他们的STORYBOARD ID。
在ViewController.h中我有以下代码:
#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.
}
问题是当我点击播放按钮时屏幕变黑。请告诉我我错过了什么。感谢
答案 0 :(得分:1)
根据您的代码,您首先删除self.view
,然后尝试insertSubview
self.view
self.playViewController.view
这样做不适合您,因此请使用
首先对self.view
进行了[self.view insertSubview: self.playViewController.view atIndex:0];
[self.view removeFromSuperview];
,然后将其从超级视图中删除,例如,
{{1}}
希望我的建议适合你。