I’m working on a little quiz app, and I was working on an algorithm to quickly proceed to the next question. The first part of the code was adequate. It worked well, but the last line threw an error. I was wondering if you could use an NSString to access a UIView variable. Here’s the code:
NSString *QuestionString = [NSString stringWithFormat:@"%d", Question]; // THE "Question" VARIABLE IS AN INT...
NSString *PlayViewString = [NSString stringWithFormat:@"%@", @"PlayView"];
NSString *ThePlayViewString = [PlayViewString stringByAppendingString: QuestionString];
NSLog(@"%@", ThePlayViewString); //WORKS GREAT TO HERE (Logged to make sure everything went well)
self.view = ThePlayViewString; // THROWS ERROR "Incompatible pointer types assigning from 'UIView *' to 'NSString *'"
UPDATE
The algorithm is inside a IBAction for a UIButton. It’s meant so that whenever you press the button, or in further questions any other object, you’ll proceed onto the next question. Thank you all for establishing that I CAN NOT use a NSString to get to a UIView.
Thanks for your help!
its quite clear what happens..
NSLog(ThePlayViewString, @"%@");iplain wrong way round, it should beNSLog(@"%@",ThePlayViewString, );self.view = ThePlayViewString;gives you the error"Incompatible pointer types assigning from 'UIView *' to 'NSString *'"from this error it should be absolutely clear what is wrong.
self.viewis of typeUIView *and if you want to assign something to it, it has to be of typeUIView *as well. you cannot assign a string to a view.Solution:
if you do not know how to create a view, this is where to look: create a view programmatically
you could create your views with .xib files (interface builder), name them after your questions and open them with the string, like that:
Edit for your purpose:
i would use another approach to what you are trying to do.
put all questions in an NSArray like that
NSArray * array = [[NSArray alloc]initWithObjects: @”question 0″, @”question 1″, …, nil];
get your question number as before
get the next question
NSString *nextQuestion = [[array objectAtIndex: questionNumber]]; (questionNumber must be type int)
and display it in a label:
i would use interfacebuilder to make a label with a connection.
sebastian