我有一个带有 xib、m 和 h 文件的普通 View Controller 。我希望在加载 View 时自动调用一个方法。在我当前的 M 文件中,我有代码调用另一个 View ,这只是为了让我可以查看 checkIfLogged 方法是否正常工作。当应用程序加载时,它不会调用其他 View ,而是停留在自己的 View 中。如何在加载 View 时调用 checkIfLogged 方法?实际上,如果可能的话,我更希望在加载 View 之前调用该方法。
这是我的 M 文件。
#import "ViewController.h"
#import "LoginView.h"
@interface ViewController ()
@end
@implementation ViewController
-(void) viewDidLoad{
[self checkIfLogged];
}
- (void) checkIfLogged
{
LoginView *loginView = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
[loginView setModalPresentationStyle:UIModalPresentationFormSheet]; //you can change the way it is presented
[loginView setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; //you can change the animation
[self presentViewController:loginView animated:YES completion:nil]; //show the modal view
}//end checkIfLogged
@end
这是我的H文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
-(IBAction)checkIfLogged;
@end
最佳答案
首先,调用 [super viewDidLoad];
作为 viewDidLoad
实现的第一行。
其次,您不应该尝试从 viewDidLoad
中呈现 View Controller 。此时,您的 UIViewController View 不是 View 层次结构的一部分。改为从 viewDidAppear:
显示 View Controller 。
关于ios - 加载 View 时未调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19104471/