0

I got an array with viewcontrollers from different types, named viewControllersArray so first I check if the type is correct (QuestionViewController) and then I want to print out the property.

for(NSUInteger i = 0; i<viewControllersArray.count; i++) { 
if ([[viewControllersArray objectAtIndex:i] isKindOfClass:[QuestionViewController class]])    {
    NSLog(@"%@",((QuestionViewController*)[viewControllersArray objectAtIndex:i]).getQAnswer ); 
    }
}

However this just shows me (null) instead of the NSString property.

EDIT

What I got so far is this:

for(NSUInteger i = 0; i<viewControllersArray.count; i++) { 
    if ([[viewControllersArray objectAtIndex:i] isKindOfClass:[QuestionViewController class]]){
        NSLog(@"IDENT: %@", [[viewControllersArray objectAtIndex:i] ident]);
        NSLog(@"ANSWER: %@", [[viewControllersArray objectAtIndex:i] getQAnswer]);
    }
}

the second NSLog (getQAnswer) works. getQAnswer is a method in QuestionViewController. The first NSLog (ident) shows incorrect output (null), this is the property:

@property (strong, nonatomic) NSString *ident;
4
  • Once could you please print your viewControllersArray ??? Commented Apr 5, 2013 at 8:57
  • why did you type cast it to QuestionViewController? Commented Apr 5, 2013 at 8:59
  • If I don't typecast, it shows me: "Property 'getQAnswer' not found on object of type 'id'" Commented Apr 5, 2013 at 9:01
  • [[self.navigationController.viewControllers objectAtIndex:yourIndex] getQAnswer]; if(UINavigationController) Commented Apr 5, 2013 at 9:07

2 Answers 2

1

You typecasted to QuestionViewController

Use

NSLog(@"%@", [[viewControllersArray objectAtIndex:i] getQAnswer]); 

EDIT:

As per your comment viewControllersArray[0] gives you SendViewController.

You should change

isKindOfClass:

to

if( [[viewControllersArray objectAtIndex:i] class] == [QuestionViewController class])   
Sign up to request clarification or add additional context in comments.

15 Comments

I got an error when I do that: "Property 'getQAnswer' not found on object of type 'id'"
<SendViewController: 0x86989b0>
@AnoopVaidya you made him print the first object in the array. That is SendViewController. The OP did not say that QuestionViewController object was the first object in the array. Thats why they have put in a loop.
@Shinonuma do you have a getQAnswer property in your VC or just qAnswer? (check my answer)
@Shinonuma: in your edit, @property (strong, nonatomic) *ident; no datatype/class is set for ident
|
1

If you get a null, that could only mean that the value of qAnswer is not set. If the object wasn't a type of QuestionViewController, it would not get into the if condition.

Are you sure the value of qAnswer is set? And if you are using a getter/setter, shouldn't you be using [viewcontroller getQAnswer] or viewcontroller.qAnswer (unless you have a variable called getQAnswer)?

Also is there any view controller in the array which is an object of a subclass of QuestionViewController? If there is, then that viewcontroller will also satisfy the condition and go inside the if condition.

If that is a case, try using isMemberOfClass:. See here

1 Comment

if getQAnswer is not there, compiler will show error, and he cant even compile it. Now he is executing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.