Error jumping between viewControllers - ios

Having some difficultly with this one.
I am loading questions, answers, correct answers from a plist. All data is being read in fine and looks like below
- (void)viewDidLoad
{
[super viewDidLoad];
rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"question" ofType:#"plist"]];
currentQuestion = -1;
[self showNextQuestion];
}
-(void) showNextQuestion{
currentQuestion++;
int numItems = [rootArray count];
NSMutableArray *question = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *A = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *B = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *C = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *addimage = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *Answer = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in rootArray) {
[question addObject:[itemData objectForKey:#"question"]];
[A addObject:[itemData objectForKey:#"A"]];
[B addObject:[itemData objectForKey:#"B"]];
[C addObject:[itemData objectForKey:#"C"]];
[addimage addObject:[itemData objectForKey:#"ImageUse"]];
[Answer addObject:[itemData objectForKey:#"ANS"]];
}
self.questionasked.text = question[currentQuestion];
self.answer1.text = A[currentQuestion];
self.answer2.text = B[currentQuestion];
self.answer3.text = C[currentQuestion];
additionImage.image = [UIImage imageNamed:addimage[currentQuestion]];
self.correctAns = Answer[currentQuestion];
if(currentQuestion == 4){
[ self performSegueWithIdentifier:#"levelclear" sender:nil];// here I am performing a segue to indicate that when 4 questions are displayed it will go to this view controller
}
}
I have 3 buttons in my VC, One for each option A,B,C which all have a function
- (IBAction)SelectA:(id)sender {
if ([self.correctAns isEqualToString:#"A"]) {
currentQuestion ++;
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier:#"incorrect" sender:nil];
}
}
- (IBAction)SelectB:(id)sender {
if ([self.correctAns isEqualToString:#"B"]) {
currentQuestion ++;
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier:#"incorrect" sender:nil];
}
}
- (IBAction)SelectC:(id)sender {
if ([self.correctAns isEqualToString:#"C"]) {
currentQuestion ++;
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier:#"incorrect" sender:nil];
}
}
When I answer the question correctly it moves to the next question. However when I answer it incorrectly it goes to the incorrect VC informing of the incorrect question. This VC has a segue when the button is pressed to go back to the question VC(which I made in the storyboard). It does go back to the question VC but simply jumps back to the first question. I know it is something regarding the currentQuestion but not sure what.

Looks like when you segue to the incorrect answer you are destroying the questions view controller, and when yo go back to it, it is re created.

Related

Rearranging viewControllers in self.navigationController.viewControllers is not working obj c

Say, In my self.navigationController.viewControllers I have n number of viewControllers. Within them, I only want to remain 2 viewControllers in my self.navigationController.viewControllers. One is at 0 index & another one is at 1 index. Here is my code.
NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
NSMutableArray *savedVCsArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [VCs count]; i++) {
UIViewController *vc = VCs[i];
if ([vc isKindOfClass:[LanguageSettingsViewController class]]) {
[savedVCsArray insertObject:vc atIndex:0];
} else if ([vc isKindOfClass:[SignInViewController class]]) {
[savedVCsArray insertObject:vc atIndex:1];
}
}
[self.navigationController setViewControllers:savedVCsArray];
NSLog(#"counts %i", [self.navigationController.viewControllers count]);
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
But, here I always get NSLog(#"counts %i", [self.navigationController.viewControllers count]); 0. Is there I am missing something? Thanks in advance. Glad if you reply.
xcode Version 8.3.2 (8E2002)
iOS 10.3
Make sure you confirm that you actually have the VCs you want:
UIViewController *langVC = nil;
UIViewController *signVC = nil;
for (UIViewController *vc in self.navigationController.viewControllers) {
if ([vc isKindOfClass:[LanguageSettingsViewController class]]) {
// [savedVCsArray insertObject:vc atIndex:0];
langVC = vc;
} else if ([vc isKindOfClass:[SignInViewController class]]) {
// [savedVCsArray insertObject:vc atIndex:1];
signVC = vc;
}
}
if (langVC == nil || signVC == nil) {
NSLog(#"Problem exists because langVC is %# / signVC is %#", langVC, signVC);
} else {
[self.navigationController setViewControllers:#[langVC, signVC] animated:YES];
}

AMSlideMenu Cell click takes time to open another view?

I’m building an article reading app.I’m using AMSliderMenu(https://github.com/SocialObjects-Software/AMSlideMenu)
library for menu list.When i click on any cell in AMSlideMenu it load into another table view which contain list of articles.
I’m fetching article data in uitableview with JSON.
The issue is when i click on list of menu in AMSlideMenu it takes time to open another view.How can i resolve this problem.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL myBool = [self isNetworkAvailable];
if (myBool)
{
#try {
_Title1 = [[NSMutableArray alloc] init];
_Author1 = [[NSMutableArray alloc] init];
_Images1 = [[NSMutableArray alloc] init];
NSData* data = [NSData dataWithContentsOfURL:ysURL];
NSArray *ys_avatars = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if(ys_avatars){
for (int j=0;j<ys_avatars.count;j++)
{
[_Title1 addObject:ys_avatars[j][#"title"]];
[_Author1 addObject: ys_avatars[j][#"author"]];
[_Images1 addObject: ys_avatars[j][#"featured_img"]];
}
}
else
{
NSLog(#"asd");
}
}
#catch (NSException *exception) {
}
[self LoadMore];
[self LoadMore];
});
});
}
}
-(void)LoadMore
{ BOOL myBool = [self isNetworkAvailable];
if (myBool)
{
#try {
// for table cell seperator line color
self.tableView.separatorColor = [UIColor colorWithRed:190/255.0 green:190/255.0 blue:190/255.0 alpha:1.0];
// for displaying the previous screen lable with back button in details view controller
UIBarButtonItem *backbutton1 = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backbutton1];
NSString *urlString=[NSString stringWithFormat:#"www.example.com&page=%d",x]; NSURL *newUrl=[NSURL URLWithString:urlString];
NSData* data = [NSData dataWithContentsOfURL:newUrl];
NSArray *ys_avatars = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
x++;
if(ys_avatars){
for (int j=0;j<_Title1.count ;j++)
{
[_Title1 addObject:ys_avatars[j][#"title"]];
[_Author1 addObject: ys_avatars[j][#"author"]];
[_Images1 addObject: ys_avatars[j][#"featured_img"]];
} }
else
{ NSLog(#"asd"); }
[self.tableView reloadData];}
#catch (NSException *exception) {
}} }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _Title1.count;
}
Its because you have done many many works in viewDidLoad method that's executing synchronously, and that view controller will not be shown until the code in viewDidLoad will not executed completely.
So, load your data asynchronously or do it in viewDidAppear method, but I strongly suggest you to do it asynchronously.

Display image based on random string from array

I have a list of strings in two arrays truth & dare that are loaded from a plist then randomised and displayed to the user in a label these could be truth or dares. I would like to display a image for both truth and dare when each one is displayed dependant on which array it is loaded from.
As i load the plist file into an array on load im not sure how to go about doing this? Im very new to xcode and am excited at the progress im making so far however.
This is the full code from my .m file
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#synthesize plistArray;
- (void)viewDidAppear:(BOOL)animated {
[self becomeFirstResponder];
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
NSString *path = [[NSBundle mainBundle] pathForResource:
#"data" ofType:#"plist"];
if ([[defaults objectForKey:#"truthonoff"] isEqualToString:#"YES"] && [[defaults objectForKey:#"dareonoff"] isEqualToString:#"YES"] ) {
self.text.text =#"Are you ready for this?";
NSDictionary *plistDict1 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray * plistArray1 = plistDict1[#"truth"];
NSDictionary *plistDict2 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray2 = plistDict2[#"dare"];
self.plistArray = [[plistArray1 arrayByAddingObjectsFromArray:plistArray2] mutableCopy];
}
else if ([[defaults objectForKey:#"truthonoff"] isEqualToString:#"YES"] ) {
self.text.text =#"I hope you are feeling brave!";
NSDictionary *plistDict3 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray3 = plistDict3[#"truth"] ;
self.plistArray = [plistArray3 mutableCopy];
NSLog(#"%#", plistArray);
}
else if ([[defaults objectForKey:#"dareonoff"] isEqualToString:#"YES"] ) {
self.text.text =#"This could be interesting!";
NSDictionary *plistDict4 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *plistArray4 = plistDict4[#"dare"];
self.plistArray = [plistArray4 mutableCopy];
NSLog(#"%#", plistArray);
}
else {
self.text.text =#"Please turn on Truth or Dare";
}
}
- (void)viewDidDisappear:(BOOL)animated {
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
// Do your thing after shaking device
if ([plistArray count] == 0) {
self.text.text = #"Please Upgrade for more";
}
else {
////display random quote from array
int randV = arc4random() % self.plistArray.count;
self.text.text = self.plistArray[randV];
[self.plistArray removeObjectAtIndex:randV];
}
}
-(IBAction)modal:(id)sender{
}
- (IBAction)container:(id)sender {
}
- (IBAction)shownext:(id)sender {
if ([plistArray count] == 0) {
self.text.text = #"Please Upgrade for more";
}
else {
////display random quote from array
int randV = arc4random() % self.plistArray.count;
self.text.text = self.plistArray[randV];
[self.plistArray removeObjectAtIndex:randV];
}
}
#end
And here is my plist structure there will be alot more data added here when finished tho
It's a little tough to understand the code, so I'll state this in general terms. If you have two arrays of strings (maybe your truth array and dare array), and you have a string that the user selected (this is the part that's unclear in your code -- is it self.text.text?) Let's just call it selectedString, then you can test:
if ([self.truthArray containsObject:selectedString]) {
// selectedString is from the truth array
} else if ([self.dareArray containsObject:selectedString]) {
// selectedString is from the dare array
} else {
// it's in neither array. maybe this is an error
}

View Controller not Displaying

I'm working on a baseball game app that contains multiple Modal View Controllers. However, when I make the modal transition from "BasicGameSetupViewController" to "BasicViewController", the transition happens (because I have NSLog statements within my ViewDidLoad method in the BasicViewController), but the actual view does not update. Here is the method where I transition to the BasicViewController (this code is in BasicGameSetupViewController):
- (IBAction)pressedBeginPlay:(id)sender {
numOfInnings = [selectInnings selectedSegmentIndex];
numOfInnings = numOfInnings + 1;
row = [teamSelector selectedRowInComponent:0];
visitor = [teams objectAtIndex:row];
row = [teamSelector selectedRowInComponent:1];
home = [teams objectAtIndex:row];
NSLog(#"%#", visitor);
NSLog(#"%#", home);
if([awaySelect selectedSegmentIndex] == 0)
{
awayPlayer = #"Human";
}
else
{
awayPlayer = #"CPU";
}
if([homeSelect selectedSegmentIndex] == 0)
{
homePlayer = #"Human";
}
else
{
homePlayer = #"CPU";
}
[self performSegueWithIdentifier:#"startBeginnerToBasic" sender:self];
}
(This block also has a "prepareForSegue" method)
And here is the viewDidLoad method in BasicViewController:
- (void)viewDidLoad{
NSLog(#"Made it to basic View controller");
homeLineup = [[NSMutableArray alloc] initWithCapacity:9];
visitorLineup = [[NSMutableArray alloc] initWithCapacity:9];
batter = [[NSMutableArray alloc] initWithCapacity:22];
homePitcher = [[NSMutableArray alloc] initWithCapacity:22];
awayPitcher = [[NSMutableArray alloc] initWithCapacity:22];
pitcher = [[NSMutableArray alloc] initWithCapacity:22];
homeAlreadyPitched = [[NSMutableArray alloc]initWithCapacity:5];
awayAlreadyPitched = [[NSMutableArray alloc] initWithCapacity:5];
NSLog(#"arrays initialized");
[super viewDidLoad];
[self setUpTeams];
[self checkForComputer];
[self newBatter];
[self atBat];
// Do any additional setup after loading the view.
}
I'm completely stumped as to why my view isn't showing. The NSLog statements that are in the ViewDidLoad code block above show up, but the I can't get the view to show. Does anyone have any ideas?
Thank you in advance for your help!

Differentiating between two View Controllers

I have 3 VC. A which displays questions and answers. B is displayed when the Question is wrongly selected a C once the level is clear.
I am having an issue going from A-B & A-C.
Here is my code
#import "AViewController.h"
#import "BViewController.h"
#import "CViewController.h"
#interface AViewController ()
#end
Now here is the implementation code
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
BViewController *incorrect = [segue destinationViewController];
incorrect.currentQuestion = self.currentQuestion;}
//Here A is sending B information about which question was answered incorrectly i.e Current Question
- (void)viewDidLoad
{
[super viewDidLoad];
rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Addition1" ofType:#"plist"]];
currentQuestion = -1;
[self showNextQuestion];
}
-(void) showNextQuestion{
currentQuestion++;
if (currentQuestion <= 3) {
int numItems = [rootArray count];
NSMutableArray *question = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *A = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *B = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *C = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *addimage = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *Answer = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in rootArray) {
[question addObject:[itemData objectForKey:#"question"]];
[A addObject:[itemData objectForKey:#"A"]];
[B addObject:[itemData objectForKey:#"B"]];
[C addObject:[itemData objectForKey:#"C"]];
[addimage addObject:[itemData objectForKey:#"ImageUse"]];
[Answer addObject:[itemData objectForKey:#"ANS"]];
}
self.questionasked.text = question[currentQuestion];
self.answer1.text = A[currentQuestion];
self.answer2.text = B[currentQuestion];
self.answer3.text = C[currentQuestion];
additionImage.image = [UIImage imageNamed:addimage[currentQuestion]];
self.correctAns = Answer[currentQuestion];}
else{
NSLog(#"End of Array ");
[self performSegueWithIdentifier:#"levelpassed" sender:nil];
}
}
This area above here is where I am having the problem
When the the level is cleared C VC is having this error displaying
[CViewController setCurrentQuestion:]: unrecognized selector sent to instance 0x71cae70
2013-03-30 21:17:31.264 thefyp[14311:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CViewController setCurrentQuestion:]: unrecognized selector sent to instance 0x71cae70'
I know the issue is that A is finding it hard to differentiate between segue and is trying to send C data about current question when all I want to do is display C when the A is finished showing all questions and no data is being transferred like when A is segueing to B. B & C also is a subclass of A. Any help would be appreciated
In the prepareForSegue method check the segue identifier UIStoryboardSegue.identifier whether it matches the transition to your B-VC.
Because calling performSegueWithIdentifier:#"levelPassed" will also run into this method and there you do not have a B-VC and therefore no currentQuestion.

Resources