简体   繁体   English

视图加载时未调用方法

[英]Method not being called when view loads

I have a normal view controller with xib, m, and h file. 我有一个带有xib,m和h文件的普通视图控制器。 I want when the view loads for it to automatically call a method. 我想要在视图加载时自动调用方法。 In my current M file I have the code call another view, this is just so I can see if the checkIfLogged method is working. 在我当前的M文件中,我有代码调用另一个视图,这仅仅是为了让我看到checkIfLogged方法是否有效。 When the app loads it doesn't call the other view it stays in its own view. 应用加载时,它不会调用其他视图,而是停留在自己的视图中。 How can I get the checkIfLogged method called when the viewloads? 加载视图时如何获取checkIfLogged方法? Actually I would prefer the method to be called before the view is even loaded if that is possible. 实际上,如果可能的话,我希望在视图加载之前就调用该方法。

Here is my M file. 这是我的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

Here is my H file 这是我的H档案

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

-(IBAction)checkIfLogged;

@end

First, call [super viewDidLoad]; 首先,调用[super viewDidLoad]; as the first line in your viewDidLoad implementation. 作为viewDidLoad实现的第一行。

Second, you shouldn't attempt to present a view controller from viewDidLoad . 其次,您不应尝试通过viewDidLoad呈现视图控制器。 Your UIViewController's view is not part of the view hierarchy at this point. 此时,您的UIViewController的视图不属于视图层次结构。 Present the view controller from viewDidAppear: instead. 而是从viewDidAppear:显示视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM