setting targets and actions Xcode

Discussion in 'iOS Programming' started by Kapthehat, Jul 2, 2013.

  1. Kapthehat macrumors member

    Joined:
    Jul 1, 2013
    #1
    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.
     

    Attached Files:

  2. forum user, Jul 2, 2013
    Last edited: Jul 2, 2013
  3. Kapthehat thread starter macrumors member

    Joined:
    Jul 1, 2013
    #3
    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 !!
     
  4. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #4
    So is it working now or do you still need some help?

    Cheers
     
  5. Kapthehat thread starter macrumors member

    Joined:
    Jul 1, 2013
    #5
    that bit is now working - I am now inserting the method code. getting there - but ok for now. thanks for asking.
     
  6. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #6
    All good. I am also pretty new to development in iOS (About 1 month in!:eek::cool:)

    So I know how it feels to be lost!
     
  7. Kapthehat thread starter macrumors member

    Joined:
    Jul 1, 2013
    #7
    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
     
  8. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #8
    Click the blank white icon.. it will launch the app you built. :cool:
     
  9. Kapthehat thread starter macrumors member

    Joined:
    Jul 1, 2013
    #9
    Thanks. I should have said - done that and get the blank black screen !!
     
  10. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #10
    Show me your code bud.

    Both the .m and the .h files.
     
  11. Kapthehat thread starter macrumors member

    Joined:
    Jul 1, 2013
    #11
    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
    
    
     
  12. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #12
    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
     
  13. Kapthehat thread starter macrumors member

    Joined:
    Jul 1, 2013
    #13
    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 :
     

    Attached Files:

  14. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #14
    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?
     
  15. Kapthehat thread starter macrumors member

    Joined:
    Jul 1, 2013
    #15
    cool - it ran and then when I pressed "Show Question" - it crashed !!! I attach a screenshot
     

    Attached Files:

  16. Kapthehat thread starter macrumors member

    Joined:
    Jul 1, 2013
    #16
    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 !!
     
  17. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #17
    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.
     
  18. Kapthehat thread starter macrumors member

    Joined:
    Jul 1, 2013
    #18
    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 !!!
     
  19. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
  20. ArtOfWarfare, Jul 3, 2013
    Last edited: Jul 3, 2013

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #20
    I'd like to mention those initial S's should be lowercase... that's the convention for method names.
     
  21. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #21
    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
     
  22. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #22
    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...
     

Share This Page