Any problems with this code?

Discussion in 'iOS Programming' started by nashyo, Jan 2, 2012.

  1. nashyo, Jan 2, 2012
    Last edited: Jan 2, 2012

    nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    Hi,

    I'm pulling my hair out trying to figure out why my label and button text aren't being set.

    The following code is an extract from my QuizViewController.m file.

    Q is a label (NSString) <IBOutlet>
    btnA, btnB, btnC are round rect buttons <IBOutlet>
    currentQuestion is a property (NSSring object)

    currentQuestion was set as @"Q1" by the prepareForSegue method from the previous viewController.


    Code:
    NSMutableDictionary *questions;
    NSMutableDictionary *answers;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [questions setObject:@"A 57 year old male presented to the out of hours GP service having splashed a oven cleaner in his left eye. His work colleagues irrigated his eye in the sink and have driven him promptly to see you. After instillation of proxymetacaine, his visual acuity was 6/9." forKey:@"Q1"];
        
        [answers setObject:@"Fax a referral and send the patient home." forKey:@"1A"];
        [answers setObject:@"Start chloramphenicol and  arrange Ophthalmic follow up in the morning." forKey:@"1B"];
        [answers setObject:@"Irrigate the eye copiously checking PH every 15-30 minutes." forKey:@"1C"];
        
        Q.text = [questions objectForKey:currentQuestion];
        
        Answer *a1 = [[Answer alloc] init];
        
        a1.answerA = [answers objectForKey:@"1A"];
        a1.answerB = [answers objectForKey:@"1B"];
        a1.answerC = [answers objectForKey:@"1C"];
        
        [btnA setTitle:[Answer.answerA] forState:UIControlStateNormal];
        [btnB setTitle:[Answer.answerB] forState:UIControlStateNormal];
        [btnC setTitle:[Answer.answerC] forState:UIControlStateNormal];
    }
    The compiler is saying that there is no property answerA in class Answer WHEN THERE IS!

    Also, the build will success if I replace Answer.answerA with a direct string, but when testing, the labels and buttons do appear empty.

    PLEASE help.

    Thanks
    Rob
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    Why are you saying
    Code:
    Answer.answerA
    
    and not
    Code:
    a1.answerA
    
    ?
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    Ok, silly mistake.

    I still have the problem of my labels/buttons not updating with text.
     
  4. EnigMoiD macrumors member

    Joined:
    Aug 10, 2007
    #4
    You should make sure to call properties on the instance of Answer, a1, instead of the class Answer. You're still doing it when you set the button titles, which could explain why they're showing up blank. Admanimal suggested this but you didn't do it everywhere.

    The reason you need to call the properties on the instance instead of the class is because (first of all) you're setting the properties for the instance, but also because you can't set or call properties from a class. The whole point of creating the instance a1 is to use it for all your Answer-related operations.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Post the code for the Answer class. Post both the @interface (header file) and the @implementation (.m file).

    If the compiler has a problem with the Answer class, because it thinks it doesn't have a property you believe it has, then simple logic suggests we might need to see the Answer class.


    Code:
        [btnA setTitle:[Answer.answerA] forState:UIControlStateNormal];
    
    Does the compiler complain about this? It should. For the same reason admanimal already pointed at.

    If the compiler doesn't complain, then you've changed the code. That means we need to see your revised code.


    You should also ask yourself where and when 'questions' and 'answers' are being assigned an NSMutableDictionary instance.

    In general, you need to confirm your expectations. This is basic debugging. Confirm that answers is non-null. Confirm it has keys and values with expected values. You can use NSLog() to do this, or learn to use the debugger.
     
  6. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    Did you confirm that the labels are not nil? Did you create them in Interface Builder and forget to connect them?
     
  7. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #7
    Thank for this basic but sound advice. I wrote some NSLog functions and discovered that the labels were in fact nil. The mutable dictionaries first needed to be initialised with a capacity of 1.

    Thanks
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    This is a better way to phrase it. You rarely need to specify the capacity upon initialization. Just initializing it is the important part.
     
  9. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #9
    Thanks

    A small point. But what I've noticed with programming so far, is that he small stuff matters!
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    So, you feel it's important to specify the capacity with the init? If so, why?
     
  11. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #11
    Not quite

    No, you misunderstand. What I mean is, you have pointed out a small fact to me (not needing to specify capacity), which is a valuable fact. I will remember it.

    The devil is in the detail.
     

Share This Page