简体   繁体   English

iOS开发人员 点击按钮,屏幕不会调用新视图

[英]IOS dev. Tap button, screen does not call a new view

Goal: to switch views when you click the button. 目标:单击按钮时切换视图。 What I did: 我做了什么:

  1. Open a single view application. 打开一个视图应用程序。

  2. File -> New -> File -> Cocoa Touch & Objective -C. 文件->新建->文件->可可触摸和物镜-C。 Create PlayViewController and GMViewController, both subclass UIViewController. 创建PlayViewController和GMViewController,它们都是UIViewController的子类。

  3. 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。

  4. 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 ,然后你想insertSubviewself.view这就是方式,它不为你工作,以便与修改代码

first insret self.playViewController.view to self.view and then it remove from super view such like, 第一insret self.playViewController.viewself.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.

相关问题 在按钮水龙头上推新视图 - Push a new view on button tap iOS-TapGestureRecognizer-Tap适用于整个屏幕而不适用于视图 - iOS - TapGestureRecognizer - Tap is applicable for the whole screen not for a view ios - 如何在不使用 IOS Swift 中的枚举的情况下根据流程在点击按钮后传递应在下一个屏幕中调用的函数? - How to pass which function should call in next screen after tap on button based on flow without using enum in IOS Swift? iOS自定义.xib视图按钮点击在视图控制器中检测(Swift) - iOS Custom .xib view button tap detect in view controller (Swift) 无法执行按钮操作/单击自定义MKannotation标注视图 - Not getting button action/ Single tap on custom MKannotation call out view 如何通过快速点击按钮创建新视图(或子视图)? - How do I create a new View (or subView) with the tap of a button in swift? 点按TextView上的手势,其超级视图在iOS 11中不起作用 - Tap Gesture on TextView and its super view does not work in iOS 11 iOS:为什么隐藏按钮仍会接收点击事件? - iOS: Why does hidden button still receive tap events? iPhone开发人员。 程序:当XCode和IOS8公开发布时会发生什么? - iPhone Dev. Program: what happens when public release of XCode and IOS8 are available? iOS导航到新视图但出现黑屏 - IOS navigate to new view but get a black screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM