[英]IOS dev. Tap button, screen does not call a new view
目标:单击按钮时切换视图。 我做了什么:
打开一个视图应用程序。
文件->新建->文件->可可触摸和物镜-C。 创建PlayViewController和GMViewController,它们都是UIViewController的子类。
进入主板,从对象库中,我拖动了两个视图控制器,并将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.
}
问题是当我单击播放按钮时,屏幕变黑了。 请告诉我我错过了什么。 谢谢
根据你的代码,你首先去除self.view
,然后你想insertSubview
上self.view
这就是方式,它不为你工作,以便与修改代码
第一insret self.playViewController.view
到self.view
,然后将其从例如像超级视图中删除,
[self.view insertSubview: self.playViewController.view atIndex:0];
[self.view removeFromSuperview];
希望我的建议对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.