我有一个ViewController,名为" TapRaceViewController"它继承自父母,#34; GameViewCController"。但是,没有显示任何TapRaceViewControllers子视图 - 只显示来自GameController的视图。这是为什么?正在调用TapRacerViewController的viewDidLoad,因此我不明白为什么没有添加子视图。
//
// TapRacerViewController.h
// MiniGame
//
// Created by Software Engineering on 11/16/14.
//
//
#import "GameViewController.h"
@interface TapRacerViewController : GameViewController
@end
TapRacerViewController.m
//
// TapRacerViewController.m
// MiniGame
//
// Created by Software Engineering on 11/16/14.
//
//
#import "TapRacerViewController.h"
@interface TapRacerViewController ()
@end
@implementation TapRacerViewController
{
//A single tap on the screen
UITapGestureRecognizer* singleTap;
//the player
UILabel* player;
//The view
UIView* m_baseView;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Create the base view
m_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[m_baseView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:m_baseView];
//Create the tap gesture
singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap)];
[m_baseView addGestureRecognizer:singleTap];
//Create the player
player = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 50, self.view.bounds.size.height / 2, 50, 50)];
[player setBackgroundColor:[UIColor blackColor]];
[m_baseView addSubview:player];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Handles a tap
- (void) handleSingleTap
{
NSLog(@"Tap!");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
GameViewController,
//
// GameViewController.h
// MiniGame
//
// Created by Software Engineering on 11/16/14.
//
//
#import <UIKit/UIKit.h>
@interface GameViewController : UIViewController
//Base init
-(id) initWithDifficulty:(int)difficulty;
//On each tick
-(void) onTick;
@end
GameViewController.m
//
// GameViewController.m
// MiniGame
//
// Created by Software Engineering on 11/16/14.
//
//
#import "GameViewController.h"
@interface GameViewController ()
@end
@implementation GameViewController
{
//Timer
NSTimer* m_timer;
//Counter of the timer
float m_timerCounter;
//How long the minigame lasts
float m_time;
//Base view
UIView* m_baseView;
//How often a tick occurs
float m_tick;
//Text that tells the user what to do
UILabel* m_instructionLabel;
}
//Base initialization
-(id) initWithDifficulty:(int)difficulty
{
NSLog(@"Heyea");
//Base initialization
self = [super init];
if(self)
{
if(difficulty == 1)
{
m_time = 5.0f;
}
else if(difficulty == 2)
{
m_time = 4.0f;
}
else
{
m_time = 3.0f;
}
//Set the timer counter to 0.0f;
m_timerCounter = 0.0f;
//Set the tick frequency to 0.001f;
m_tick = 0.001f;
//Set the base view
m_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[m_baseView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:m_baseView];
//Set the default instruction text
m_instructionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[m_instructionLabel setText:@"Instruction!"];
[m_instructionLabel setFont:[UIFont fontWithName:@"Georgia-BoldItalic" size:15.0]];
[m_instructionLabel setTextColor:[UIColor redColor]];
[m_baseView addSubview:m_instructionLabel];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
//When the view loads, start the countdown
-(void) viewDidAppear:(BOOL)animated
{
//Start the timer
m_timer = [NSTimer scheduledTimerWithTimeInterval:m_tick target:self selector:@selector(onTick) userInfo:nil repeats:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//On every tick from m_timer
- (void)onTick
{
// NSLog(@"Tick!");
// NSLog(@"%.2f", m_timerCounter);
//Incriment the alpha of the text
if(m_instructionLabel.alpha > 0.7) {
[m_instructionLabel setAlpha:m_instructionLabel.alpha - 0.001];
}
//Incriment the timer counter by a tick
m_timerCounter += m_tick;
//If the timer counter is more than the alloted time for the mini game, quit the mini game
if(m_timerCounter > m_time)
{
//Stop the timer
[m_timer invalidate];
m_timer = nil;
//Dismiss this view controller
[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
答案 0 :(得分:-1)
我还没有将视图添加到它的超级视图中我猜。
[self.view addSubview:m_baseView];
将此行写在viewDidLoad方法的末尾。