Domanda

With Xcode 5.0 I am trying to follow the Big Nerd Ranch book 2nd edition, which seems to be a bit outdated.

There is an example Quiz project with 2 labels and 2 buttons.

I have copied the source code from the book, esp. the AppDelegate.h:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate> {
    int currentQuestionIndex;
    NSMutableArray *questions;
    NSMutableArray *answers;
    IBOutlet UILabel *questionField;
    IBOutlet UILabel *answerField;
}

@property (strong, nonatomic) UIWindow *window;
- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;

@end

There is no MainWindow.xib mentioned in the book, but I do have a Main.storyboard, where I'v placed the labels and buttons:

screenshot

I can see the 4 hollow little circles at the left side of source code editor (in the middle of the above screenshot) and in the "Connection Inspector" (for example "Touch Up Inside"), but I just can't get them connected. I tried dragging and ctrl-dragging from the little circles to the buttons/labels, there is a blue line, but it doesn't connect.

And when I right-click the buttons/labels, then a grey menu about "Outlets" appears, but none of my IBOutlets/IBActions are listed there.

How can I add the connections to the labels and buttons please?

UPDATE:

On Rob's advice (thank you +1) I've moved the properties and methods to ViewController.* and been able to connect the labels and buttons. I see the methods being called, when I click the buttons.

But now I have the problem that the init method of the ViewController class is not run and thus both arrays are nil.

Any further hints please? And I'm not sure why the suggestions to close my question as "too broad" - I have attached the (short) code and the screenshot and my 2 questions are pretty specific (and probably basic).

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    int currentQuestionIndex;
    NSMutableArray *questions;
    NSMutableArray *answers;
    IBOutlet UILabel *questionField;
    IBOutlet UILabel *answerField;
}
- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (id)init {                 // XXX is never called??
    self = [super init];

    if(self) {
        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 self;
}

- (IBAction)showQuestion:(id)sender // XXX runs ok when clicked
{
    currentQuestionIndex++;
    if (currentQuestionIndex == [questions count]) {
        currentQuestionIndex = 0;
    }

    NSString *question = [questions objectAtIndex:currentQuestionIndex];
    NSLog(@"displaying question: %@", question);
    [questionField setText:question];
    [answerField setText:@"???"];
}

- (IBAction)showAnswer:(id)sender  // XXX runs ok when clicked
{
    NSString *answer = [answers objectAtIndex:currentQuestionIndex];
    [answerField setText:answer];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
È stato utile?

Soluzione

Your IBOutlet references should be in the view controller class, not the app delegate class. The scene's base class (as shown in the "Identity inspector" after you select the bar below the scene) is the view controller, and thus your IBOutlet references will be tied back to that. When you control dragging from your storyboard to the view controller class, you'll find it will start behaving like you intended.

The app delegate is intended to define behavior of what your app does when it starts, enters background, etc. For behavior while the user is interacting with the app, you generally put that in the view controller class.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top