2

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

** 。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
4

2 回答 2

3

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

如果将代码移动到该viewDidLoad方法,您的模式将只被调用一次,因为该方法仅在分配 viewController 时调用。

如果您希望用户看不到第一个 viewController,您可能需要在 to 中设置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将被调用一次。

于 2013-09-02T02:24:17.560 回答
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;
   }
}
于 2013-09-02T01:23:12.333 回答