[英]IOS dev. Tap button, screen does not call a new view
Goal: to switch views when you click the button. 目标:单击按钮时切换视图。 What I did: 我做了什么:
Open a single view application. 打开一个视图应用程序。
File -> New -> File -> Cocoa Touch & Objective -C. 文件->新建->文件->可可触摸和物镜-C。 Create PlayViewController and GMViewController, both subclass UIViewController. 创建PlayViewController和GMViewController,它们都是UIViewController的子类。
Went to mainstoryboard and from the object library, I dragged two View Controllers, with PLayViewController and GMViewController as their STORYBOARD IDs. 进入主板,从对象库中,我拖动了两个视图控制器,并将PLayViewController和GMViewController作为其STORYBOARD ID。
IN ViewController.h I have these codes: 在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
Basically, here I created two buttons. 基本上,我在这里创建了两个按钮。 one will go to PlayView, the other to GMView. 一个进入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.
} }
Problem is when I click the play button the screen goes black. 问题是当我单击播放按钮时,屏幕变黑了。 Please tell me what I missed. 请告诉我我错过了什么。 Thanks 谢谢
As per your code, you are first removing self.view
and then you are trying to insertSubview
on self.view
thats way it is not working for you so change your code with 根据你的代码,你首先去除self.view
,然后你想insertSubview
上self.view
这就是方式,它不为你工作,以便与修改代码
first insret self.playViewController.view
to self.view
and then it remove from super view such like, 第一insret self.playViewController.view
到self.view
,然后将其从例如像超级视图中删除,
[self.view insertSubview: self.playViewController.view atIndex:0];
[self.view removeFromSuperview];
Hope that my suggestion will be work for you. 希望我的建议对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.