解雇后,模态视图重新出现

时间:2013-09-02 01:11:12

标签: ios modal-dialog

我在视图中以模态方式调用登录视图在根视图控制器中执行加载方法,但是当我解除模态视图控制器时它再次出现!有什么建议? 听到代码:

** .h **

//
//  FirstViewController.h
//  Voyager
//
//  Created by Peter Unger on 9/1/13.
//  Copyright (c) 2013 Penumbra Software. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface FirstViewController : UIViewController

@end

@interface DefaultSettingsViewController :
UIViewController <PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate>

@end

** .m **

//
//  FirstViewController.m
//  Voyager
//
//  Created by Peter Unger on 9/1/13.
//  Copyright (c) 2013 Penumbra Software. All rights reserved.
//

#import "FirstViewController.h"
#import <Parse/Parse.h>

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
       }

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    PFLogInViewController *login = [[PFLogInViewController alloc] init];
    login.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton | PFLogInFieldsSignUpButton;
    login.delegate = (id)self;
    login.signUpController.delegate = (id)self;
    [self presentViewController:login animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)signUpViewController:(PFSignUpViewController *)signUpController didSignUpUser:(PFUser *)user {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

2 个答案:

答案 0 :(得分:3)

正如@Bergasms已经说过,你有一个循环。每次viewController出现在屏幕上时都会调用viewDidAppear方法。因此,当您重新设置呈现的viewController时,第一个视图控制器应该出现,然后调用viewDidAppear,它将再次显示PFLogInViewController,从而创建一个循环。

如果将代码移动到viewDidLoad方法,则只会调用一次模态,因为只有在分配了viewController时才会调用此方法。

如果您希望用户看不到第一个viewController,您可能需要将presentViewController:animated:completion:中的动画设置为NO


修改

一个人在另一个问题中说,如果你在viewDidLoad中调用另一个视图控制器,你可能会收到错误(虽然我已经这样做了一次,但还没有检查过错误的行为)。如果是这种情况并且您收到错误,则必须将代码放在viewDidAppear中。


如果您仍然希望使用viewDidAppear,则只需创建一个BOOL属性即可控制是否已调用PFLogInViewController。它会是这样的

@property(nonatomic, assign) BOOL isLogged; 

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (!self.isLogged)
    {
        self.isLogged = YES;
        PFLogInViewController *login = [[PFLogInViewController alloc] init];
        login.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton | PFLogInFieldsSignUpButton;
        login.delegate = (id)self;
        login.signUpController.delegate = (id)self;
        [self presentViewController:login animated:YES completion:nil];
    }
}

通过这种方式,您的PFLogInViewController将被调用一次。

答案 1 :(得分:0)

你有一个循环。当模态控制器被解除时,将在第一个视图控制器上调用viewDidAppear方法,该控制器将再次启动模态视图。您应该在启动代码中添加注释或断点以帮助调试此类事情。

@interface FirstViewController (){
BOOL has_shown_login_modal;
}
@end  


 - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    has_shown_login_modal = NO;
   }  


 - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
   if(!has_shown_login_modal){
      PFLogInViewController *login = [[PFLogInViewController alloc] init];
     login.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton |        PFLogInFieldsSignUpButton;
     login.delegate = (id)self;
     login.signUpController.delegate = (id)self;
     [self presentViewController:login animated:YES completion:nil];
      has_shown_login_modal = YES;
   }
}