I am new to iPhone,as per requirement I have to select a single question from list of question in the tableview,and want to show that question in the next new view.so I send my row value to new Xib file as the time of creation.
I stored my question-ans in plist as a array of dictionary format.I can fetch the array from Plist,but unable to fetch dictionary data..I made my custom class to store question-Answer.but at the new get only the last question that is stored in the plist.
Please help me.thank u..
Here I am attaching my code tableView.m,newView.m and customClass.m..
- (void)viewDidLoad
{
//rowValue tells me which row is selected
[self firstView:rowValue];
}
-(void)firstView: (NSInteger)indexNumber{
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"Qus_ans" ofType:@"plist"];
NSArray *rawElementsArray = [[NSArray alloc] initWithContentsOfFile:thePath];
NSDictionary *eachElement;
//overlap the data here
for (eachElement in rawElementsArray)
{
element = [[Question alloc] initWithDictionary:eachElement];}
//get only last question in the plist
questionLabel.text =[NSString stringWithFormat:@"%@",element.ques];
}
element is my Custom class object.This is my custom class:
Question.m
- (id)initWithDictionary:(NSDictionary *)aDictionary {
self = [[Question alloc] init];
if (self) {
self.ques = [aDictionary valueForKey:@"question"];
self.ans = [aDictionary valueForKey:@"answer"];
[self data];
}
return self;
}
Here element.ques is overlapped with the last value.
How I get proper question without using the forLoop...
This is my plist format
<array>
<dict>
<key>question</key>
<string>Approximately how much does the human brain weigh?</string>
<key>answer</key>
<string>Three pounds</string>
</dict>
<dict>
<key>question</key>
<string>The brain has this number of The brain has this number of nerve cells?</string>
<key>answer</key>
<string>1 to 100 billion</string>
</dict>
Solution
Well, I'm not entirely sure I undestand your problem, but if you are trying to get rid of your for loop (which I guess is the case), you could simply write