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

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
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
 
Last edited:
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.
 
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.
 
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
 
Thanks

This is a better way to phrase it. You rarely need to specify the capacity upon initialization. Just initializing it is the important part.

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

So, you feel it's important to specify the capacity with the init? If so, why?

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.