[英]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.