简体   繁体   中英

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. Create PlayViewController and GMViewController, both subclass UIViewController.

  3. Went to mainstoryboard and from the object library, I dragged two View Controllers, with PLayViewController and GMViewController as their STORYBOARD IDs.

  4. IN ViewController.h I have these codes:

.

#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.

 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

first insret self.playViewController.view to self.view and then it remove from super view such like,

[self.view insertSubview: self.playViewController.view atIndex:0];
[self.view removeFromSuperview];

Hope that my suggestion will be work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM