简体   繁体   中英

ios:Instatinate View Controller is not working(View Controller is not pushing)?

In my project am parsing json from webservice and displaying in screen, am using stroyboards for my project, In my login pagem after entering user details am parsing json, is i receives status as 1 it will goto next page else it will show error message. Now my problem is it checks status but no loads next page. am using navigation controller. Here is the code what i hat i have tried to load view controller but its not working

  -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
      NSLog(@"%@",redirectString);
    tweet = [[NSDictionary alloc]init];
    tweet = [NSJSONSerialization JSONObjectWithData:webData options:NSJSONReadingAllowFragments error:nil];

    NSLog(@"%@",tweet);
    if ([tweet isKindOfClass:[NSDictionary class]]){ //Added instrospection as suggested in comment.
        NSArray *yourStaffDictionaryArray = tweet[@"userDetail"];
        if ([yourStaffDictionaryArray isKindOfClass:[NSArray class]]){//Added instrospection as suggested in comment.
            for (NSDictionary *dictionary in yourStaffDictionaryArray) {
               if([[dictionary objectForKey:@"status"] isEqualToString:@"1"])
        {
            NSLog(@"%@",[dictionary objectForKey:@"status"]);
            NSLog(@"%@",redirectString);
            if([redirectString isEqualToString:@"CA"]) {
                NSLog(@"Prints 1");
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
                // Get instance of initial Viewcontroller from storyboard
                UINavigationController *navController = self.navigationController;
                // Get instance of desired viewcontroller
                RTCommunityViewController *viewController =[storyboard instantiateViewControllerWithIdentifier:@"community_id"];
                //viewController.kApiKey = apikey;
                //viewController.kSessionId = sessionid;
                //viewController.kToken = token;
                [self.navigationController pushViewController:viewController animated:NO];
                }
            else if([redirectString isEqualToString:@"CC"]) {
              NSLog(@"Prints 1");
              UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
              // Get instance of initial Viewcontroller from storyboard
              UINavigationController *navController = self.navigationController;
              // Get instance of desired viewcontroller
              RTCounsellingViewController *viewController =[storyboard instantiateViewControllerWithIdentifier:@"community_id"];
              //viewController.kApiKey = apikey;
              //viewController.kSessionId = sessionid;
              //viewController.kToken = token;
              [self.navigationController pushViewController:viewController animated:NO];
          }
          else if([redirectString isEqualToString:@"mgar"]) {
              NSLog(@"Prints 1");
              UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
              // Get instance of initial Viewcontroller from storyboard
              UINavigationController *navController = self.navigationController;
              // Get instance of desired viewcontroller
              MyGivingViewController *viewController =[storyboard instantiateViewControllerWithIdentifier:@"community_id"];
              //viewController.kApiKey = apikey;
              //viewController.kSessionId = sessionid;
              //viewController.kToken = token;
              [self.navigationController pushViewController:viewController animated:NO];
          }
            else if([redirectString isEqualToString:@""]){
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
            NSLog(@"Prints 2");
            // Get instance of initial Viewcontroller from storyboard
            UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:@"MYhome1"];
            // Get instance of desired viewcontroller
            RTHomeViewController *viewController = (RTHomeViewController *)[navController topViewController];
            //viewController.kApiKey = apikey;
            //viewController.kSessionId = sessionid;
            //viewController.kToken = token;
            [self presentViewController:navController animated:NO completion:nil];
            }

            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [SingleTon sinlgeTon].myname = username;
            [defaults setObject:username forKey:@"firstname"];
            [defaults setObject:pass1 forKey:@"lastname"];
            [defaults setObject:valuestat forKey:@"USER_ID"];
            [defaults setObject:usertype forKey:@"USER_TYPE"];
            [defaults synchronize];
            NSLog(@"Data saved");
        }
        else if([[dictionary objectForKey:@"status"] isEqualToString:@"0"])
        {
            UIAlertView *newAlert = [[UIAlertView alloc]initWithTitle:@"User Not Enabled!!" message:@"Contact Admin" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [newAlert show];
            [user resignFirstResponder];
            [pass resignFirstResponder];
            user.text = nil;
            pass.text = nil;
        }
        else if([[dictionary objectForKey:@"status"] isEqualToString:@"invalid"]){
            UIAlertView *newAlert = [[UIAlertView alloc]initWithTitle:@"Invalid User!!" message:@"Contact Admin" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [newAlert show];

        }
        }
        }
    }
}

I struck over here

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
                    // Get instance of initial Viewcontroller from storyboard
                    UINavigationController *navController = self.navigationController;
                    // Get instance of desired viewcontroller
                    RTCommunityViewController *viewController =[storyboard instantiateViewControllerWithIdentifier:@"community_id"];
                    //viewController.kApiKey = apikey;
                    //viewController.kSessionId = sessionid;
                    //viewController.kToken = token;
                    [self.navigationController pushViewController:viewController animated:NO];
                    }

i cannot able to load viewController by instatinating. View Controller is not pushing Please help me how to do it. Thanks in Advance

Try

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];           

UINavigationController *navController = self.navigationController;

RTCommunityViewController *viewController =[storyboard instantiateViewControllerWithIdentifier:@"community_id"];

//Comment this
//[self.navigationController pushViewController:viewController animated:NO];

[self performSegueWithIdentifier:@"community_id" sender:self];

Perhaps you should check one more time your storyboard? Didn't you forget to set Custom classes and StoryboardID in identity inspector for all scenes?

In similar situation in view controller this code worked for me:

RTCommunityViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"community_id"];
[self.navigationController pushViewController:viewController animated:NO];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM