1

您好我正在尝试在 viewDidLoad 中推送视图控制器

我正在使用故事板。在 ViewDidLoad 中,我正在检查用户是否登录并创建了他的个人资料。如果用户未登录或未创建个人资料,则推送另一个视图控制器以进行登录/个人资料视图。以下代码不起作用。

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


    }

}
4

2 回答 2

0

我对你的代码有点困惑,但无论如何试试这个

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


    }`
 }
于 2013-10-30T10:09:12.490 回答
0

为什么不创建从 viewController 到 Login 和 Profile View Controller 的 segue 并使用 performSegueUsingIdentifier: 并使用 prepareForSegue: 方法推送导航控制器

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

在prepareforsegue方法中

-(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
}
}
于 2013-10-30T10:46:20.773 回答