Quiz App : Build Successful But Blank Screen


#1

I think I followed the directions exactly in chapter 1 but the quiz app builds with no issue but there are no buttons or fields in the simulator when it comes up. Just a grey screen.

This is my implementation file.

[code]#import “QuizViewController.h”

@interface QuizViewController ()

@end

@implementation QuizViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {

    // Call the init method implemented by the superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Create two arrays and make the pointers point to them
    questions = [[NSMutableArray alloc] init];
    answers = [[NSMutableArray alloc] init];

      [questions addObject:@"What is 7+7?"];
      [answers addObject:@"14"];
      
      [questions addObject:@"What is the capital of Vermont?"];
      [answers addObject:@"Montpelier"];
      
      [questions addObject:@"From what is cognac made?"];
      [answers addObject:@"Grapes"];
    

    }
    //Return the address of the new object
    return self;
    }

  • (IBAction)showQuestion:(id)sender
    {
    // Step to the next question
    currentQuestionIndex++;

    // Am I past the last question?
    if (currentQuestionIndex == [questions count]) {
    currentQuestionIndex = 0;
    }

    // Get the string at that index in the questions array
    NSString *question = [questions objectAtIndex:currentQuestionIndex];

    // Log the string to the console
    NSLog(@“displaying question: %@”, question);

    // Display the string in the question field
    [questionField setText];

    // Clear the answer field
    [answerField setText:@"???"];
    }

  • (IBAction) showAnswer:(id)sender
    {
    // What is the answer to the current question?
    NSString *answer = [answers objectAtIndex:currentQuestionIndex];

    //Display it in the answer field
    [answerField setText:answer];
    }

@end[/code]

This is my header file.

[code]#import <UIKit/UIKit.h>

@interface QuizViewController : UIViewController
{
int currentQuestionIndex;

//The Model Objects
NSMutableArray *questions;
NSMutableArray *answers;

// The View Objects
IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;

}

  • (IBAction)showQuestion:(id)sender;
  • (IBAction)showAnswer:(id)sender;

@end[/code]

I can send a picture of the .xib screen if that is needed.

Thanks in advance.


#2

Nothing really pops out to me as any problems in the code you’ve shown.

Did you add the appropriate lines to the AppDelegate.h & .m files?

AppDelegate.h


#import <UIKit/UIKit.h>

@class QuizViewController;

@interface ....

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) QuizViewController *viewController;

@end

AppDelegate.m

#import "QuizViewController.h"

@implementation ....

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:.... didFinishLaunchingWithOptions....
{
   <....>
   // Override point for customization after application launch.
   self.viewController = [[QuizViewController alloc] initWithNIbName:@"QuizViewController" bundle:nil];
   self.window.rootViewController = self.viewController;
   <....>
}

In reading my code from my quiz app, I would type some things differently now, but I believe I just followed the book at that point, so I’m guessing yours would be similar.

Anyway, since nothing is coming onto the screen for you, I’m guessing the setting of the viewController is what was missing…but I could obviously be wrong.

Hope that helped…


#3

Thanks for trying but still doesn’t work. :frowning:

Page 11 in the books tells me to not make any changes in the AppDelegate.

This is what I have in my QuizAppDelegate.h

[code]#import <UIKit/UIKit.h>

@class QuizViewController;

@interface QuizAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) QuizViewController *viewController;

@end[/code]

and this is what I have in the implementation file

[code]#import “QuizAppDelegate.h”

#import “QuizViewController.h”

@implementation QuizAppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[QuizViewController alloc] initWithNibName:@“QuizViewController” bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
    }[/code]

This is so frustrating! :angry:


#4

I use Xcode 4.3.1 and IPHONE simulator 5.1, if that’s useful information. :question:


#5

I’m missing something, but i don’t know what it is. It looks good to me, but I’m still a newbie at all this. Hopefully someone else can come along and help narrow down the problem more. Not sure why nothing is popping up on your screen at all.

Did you understand, and do, all the steps to ctrl-drag the appropriate items in the XIB file to File Owner? I can’t remember all the step that were automatic and which were manual, so in looking at my xib file, when I click on File’s Owner and view the Identity Inspector area, the Class is set to QuizViewController there. I can’t remember if I switched that or if it started that way. Check that.

The only other hand I can really give you is to give you a little tool to help you do some minor troubleshooting. NSLog. If you haven’t used it before, it’s basically a way of sending a message to your xcode screen while the program is running through itself.

For example, in the AppDelegate.m file, right before the self.viewController statement, you could insert the line:

Then run your app. You should see that message pop into your debug area (bottom of your editor window) about the time that the simulator window shows itself.

You can use statements like that anywhere in your app, to see how far the app has made it. There are other ways to do this, as you’ll learn, but this is a pretty useful one anyway. Put a similar statement, just change the message between quotes, after the self.window.rootViewController statement too. Maybe stick one or two in the initWithNibName method of your QuizViewController.m file to see if it’s even being triggered. Stick them anywhere you want.

If all else fails, and if nobody else responds with any ideas, just move on in the book. There must just be a typo or something small that was missed. It’s not like all of these concepts aren’t going to be covered over and over during the book.

Sorry I couldn’t help more. Good luck.


#6

Thanks for your help.

I checked my connections in the inspector and it looks like the screenshot provided on page 17.

I’ll fumble with quiz project a bit more using the NSLog and probably just move on to the next project as you suggested. Hopefully I’ll be more successful there.


#7

Ehem, thats embarrassing. :blush:

The problem was in my XIB file. When I followed the directions in the book, I dragged the items onto the grid area first and then placed them on top of the iPhone-like “View” canvas. So they were treated as independent objects. Things work now when I dragged the items directly onto the “View” canvas instead of dragging it twice.

Thanks! Woo Hoo! First app ever made and its not a Hello World! :mrgreen:


#8

Well at least it got resolved… and you found it!

I know what you mean about it not being a “Hello World” app. About time we advanced to something newer than the original 1984 Hello World example!