Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kapthehat

macrumors member
Original poster
Jul 1, 2013
51
0
hello again,

my second post.

I am following the Big nerd ranch guide on IOS programming. I am on page 15 /16 and trying to set an objects target and action. However, I don't seem to get the choices stated in the book - namely showAnswer and showQuestion under "Sent Events". I attach a screenshot of what I do see. I have doubled checked my IBActions in Quizviewcontroller.h and the methods are there.
 

Attachments

  • Screen Shot 2013-07-02 at 09.10.33.png
    Screen Shot 2013-07-02 at 09.10.33.png
    334.7 KB · Views: 134
Thanks. that helps - but still cant see how to join the showquestion and show answer to the file owner. The book says select showquestion etc but i dont seem to be able to.

----------

have now got it working !!

----------

have now got it working !!
 
that bit is now working - I am now inserting the method code. getting there - but ok for now. thanks for asking.
 
lol !!!

Have entered all the code, got rid of the bugs - and message says build succeeded. however when i launch the IOS Simulator, and press the button at the bottom although the blank white quiz icon appears, nothing subsequently happens. i.e no quiz actually appears.

Any thoughts ? thanks

Kaps
 
lol !!!

Have entered all the code, got rid of the bugs - and message says build succeeded. however when i launch the IOS Simulator, and press the button at the bottom although the blank white quiz icon appears, nothing subsequently happens. i.e no quiz actually appears.

Any thoughts ? thanks

Kaps

Click the blank white icon.. it will launch the app you built. :cool:
 
Thanks. I should have said - done that and get the blank black screen !!
 
As requested - thanks !!!

Code:
//
//  QuizViewController.h
//  Quiz
//
//  Created by Kapil Kapur on 01/07/2013.
//  Copyright (c) 2013 Kapil Kapur. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface QuizViewController : UIViewController
{
    int currentQuestionindex;
    // the model objects
    NSMutableArray *questions;
    NSMutableArray *answers;
    
    // the view objects - dont worry about IBoutlet
    // we will talk about it shortly
    
    IBOutlet UILabel *questionfield;
    IBOutlet UILabel *answerfield;
}
- (IBAction)showquestion:(id)sender;
- (IBAction)showanswer:(id)sender;

@end


and:-

I have just noticed that there is an "incomplete implementation" message on "QuizViewController.m" file btw.

Code:
//
//  QuizViewController.m
//  Quiz
//
//  Created by Kapil Kapur on 01/07/2013.
//  Copyright (c) 2013 Kapil Kapur. All rights reserved.
//

#import "QuizViewController.h"

@interface QuizViewController ()

@end

@implementation QuizViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{
    
    // call the init method initiated by the super class
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
    
        //create two arrays and make pointers to them
        questions= [[NSMutableArray alloc] init];
        answers =[[NSMutableArray alloc] init];
        
        
        // add questions and answers to the arrays
        
        [questions addObject:@"What is 3 + 7 ?"];
        [answers addObject:@"10"];
        
        [questions addObject:@"what is the capital of india ?" ];
        [answers addObject :@"New Delhi"];
        
        [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 next question
        currentQuestionindex++;
        
        // am I past the last question ?
        if(currentQuestionindex == [questions count])
        {
            // go back to first question
            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:question];
        
        // clear the answer field
        [answerfield setText:@"???"];
        
    
    }

-(IBAction)showanswer:(id)sender
{
// what is the answer to the current question ?
    NSString *answer = [answers objectAtIndex:currentQuestionindex];
    
    // Dispplay it in the answer field
    [answerfield setText:answer];
    
}

@end
 
The code looks fine - the reason you're getting an incomplete warning is because in your header file you have this:

Code:
- (IBAction)showquestion:(id)sender;

Then in your implementation file:

Code:
- (IBAction)showQuestion:(id)sender;

These are two different methods.

Objective-C is a case-sensitive language.

Also, to make your code read better - you need to make your methods interCape - like in the .m file.

It's easier to read if every word in the method (Or instance variable) starts with a capital letter - like this:

Code:
-(void)someVeryLongMethodName:(id)someObject;

Now without interCape..

Code:
-(void)someverylongmethodname(id)someobject;

Sticking to the first example will avoid these sort of errors. :cool:

Now back to the issue at hand:

It sounds to me like your .xib file is not connected to a view controller at all.

Can you do this:

Select .xib file - then click on the view at the top where the status bar is - then open up the connections inspector.

You should see a connection to your view in there?

If not - there's your problem.


Cheers
 
Thanks - I have fixed the anomaly in the methods capitalisation. I attach a screenshot showing the connections inspector - for the view. I am not sure what it should be :
 

Attachments

  • Screen Shot 2013-07-03 at 09.24.05.png
    Screen Shot 2013-07-03 at 09.24.05.png
    357.1 KB · Views: 117
Okay - so view is not there as suspected.

Do this:

Under Placeholder from the left of your screen..

Click on file owner - then control-drag to your grey background in your view - (Anywhere as long as it's not on a view object like a button or label or something)

You will see a blue line when doing this. When you let go a black box will pop up. Click "view"

This should connect your view to the file owner.

Run the app - it should be working now?
 
cool - it ran and then when I pressed "Show Question" - it crashed !!! I attach a screenshot
 

Attachments

  • Screen Shot 2013-07-03 at 09.40.58.png
    Screen Shot 2013-07-03 at 09.40.58.png
    220.1 KB · Views: 107
actually ignore that last post. seems to have resolved itself in that it no longer crashes. however pressing the "Show Question" button results in the app closing down with nothing happening !!
 
actually ignore that last post. seems to have resolved itself in that it no longer crashes. however pressing the "Show Question" button results in the app closing down with nothing happening !!

Did you read the error message if your second to last post?

It says "Unrecognised selector sent to an instance.."

Looking a few lines up you will see it mentions "showquestion" (This should say" showQuestion)

Did you clear the incomplete warning from earlier?

Both the header and implementation file method's should match exactly.
 
thanks again

The issue was in the connections - both the sent events referred to Showquestion and Showanswer rather than the ShowQuestion and ShowAnswer methods !!!

its now all working !!!
 
thanks again

The issue was in the connections - both the sent events referred to Showquestion and Showanswer rather than the ShowQuestion and ShowAnswer methods !!!

its now all working !!!

I'd like to mention those initial S's should be lowercase... that's the convention for method names.
 
Last edited:
I'd like to mention those initial S's should be lowercase. Class methods start with uppercase letters while instance methods start with lowercase letters.

ClassMethod
vs
instanceMethod

Actually, class NAMES should start with upper-case, and all METHODS (whether class or instance methods) should start with lower-case.

Reference guide:
http://developer.apple.com/library/...ngWithObjectiveC/Conventions/Conventions.html

Also see:
http://developer.apple.com/library/...ics.html#//apple_ref/doc/uid/20001281-1002226
 
Actually, class NAMES should start with upper-case, and all METHODS (whether class or instance methods) should start with lower-case.

Reference guide:
http://developer.apple.com/library/...ngWithObjectiveC/Conventions/Conventions.html

Also see:
http://developer.apple.com/library/...ics.html#//apple_ref/doc/uid/20001281-1002226

Oh dear... you're right, I've removed what I said about class methods. I guess it's not important to have special naming conventions for class vs. instance methods... when it's being used it should be clear whether a class or instance is having a method called, and when it's declared it has the + if it's a class method or the - if it's the instance method... I'm too tired to think any more clearly now and I was evidently too tired when I posted that...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.