ios:实例化视图控制器不工作(View Controller没有推送)?

时间:2014-02-25 06:11:31

标签: ios iphone objective-c

在我的项目中我正在从webservice解析json并在屏幕上显示,我正在使用stroyboards进行我的项目,在我的登录pagem输入用户详细信息后解析json,我收到状态为1它将转到下一页,否则它将显示错误信息。现在我的问题是它检查状态但下一页没有加载。我正在使用导航控制器这是代码,我试图加载视图控制器,但它无法正常工作

  -(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];

        }
        }
        }
    }
}

我来到这里

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];
                    }

我无法通过instatinating加载viewController。 View Controller is not pushing请帮我怎么做。在此先感谢

2 个答案:

答案 0 :(得分:0)

尝试

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];

答案 1 :(得分:0)

也许你应该再次检查你的故事板?您是否忘记在所有场景的身份检查器中设置自定义类和StoryboardID?

在视图控制器的类似情况下,这段代码对我有用:

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