Question

Hi I am trying to push a view controller in viewDidLoad

I am using storyboard. In ViewDidLoad, I am checking that user is login and he is created his profile. if user is not logged in or he does not created his profile then pushing the another view controller for login/profile view. The following code does not working.

- (void)viewDidLoad

if(USER_IS_LOGGED_IN)
{
    if(USER_PROFILE_COMPLETED)
    {
       [self sendRequest];
    }
    else
    {
        //push profile view      
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ProfileViewController *profileViewController = (ProfileViewController *)[storyboard instantiateViewControllerWithIdentifier:@"ProfileView"];
        profileViewController.isFromDealView = YES;
        profileViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:profileViewController animated:YES];
    }

    }
    else
    {

    //push login view
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LoginViewController *loginViewController = (LoginViewController *)[storyboard instantiateViewControllerWithIdentifier:@"LoginView"];
    loginViewController.isFromDealView = YES;
    loginViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:loginViewController animated:YES];


    }

}
Was it helpful?

Solution

why don't you create the segue from your viewController to the Login and Profile View Controller and use performSegueUsingIdentifier: and push the navigation Controller using prepareForSegue: method

 - (void)viewDidLoad 
   {
    if(USER_IS_LOGGED_IN)
    {
        if(USER_PROFILE_COMPLETED)
        {
           [self sendRequest];
        }
        else
        {
            //push profile view      

            [self performSegueWithIdentifier:@"profileSegue" sender:self] ;
        }
        }
        else
        {
           //push login view
            [self performSegueWithIdentifier:@"loginSegue" sender:self] ;
        }
    }

in prepareforsegue method

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


    if ([segue.identifier isEqualToString:@"profileSegue"]) {
      ProfileViewController *profileViewController = segue.destinationViewController;
      profileViewController.isFromDealView = YES;
      profileViewController.hidesBottomBarWhenPushed = YES;
}
else if([segue.identifier isEqualToString:@"loginSegue"]){

    LoginViewController *loginViewController = segue.destinationViewController;
    loginViewController.isFromDealView = YES;
    loginViewController.hidesBottomBarWhenPushed = YES;
}
else{
//no segue to push
}
}

OTHER TIPS

I am little confuse with your code but any way try this

 - (void)viewDidLoad {

 if(USER_IS_LOGGED_IN) {
    if(USER_PROFILE_COMPLETED)
    {
       [self sendRequest];
    }
    else
    {
        //push profile view      
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ProfileViewController *profileViewController = (ProfileViewController *)[storyboard instantiateViewControllerWithIdentifier:@"ProfileView"];
        profileViewController.isFromDealView = YES;
        profileViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:profileViewController animated:YES];
    }
 } else {

    //push login view
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LoginViewController *loginViewController = (LoginViewController *)[storyboard instantiateViewControllerWithIdentifier:@"LoginView"];
    loginViewController.isFromDealView = YES;
    loginViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:loginViewController animated:YES];


    }`
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top