ios - 加载 View 时未调用方法

标签 ios objective-c uiviewcontroller

我有一个带有 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/

相关文章:

ios - 删除在 UIImage 上绘制的 UIBezirePath 线

iphone - 用引号将 UILabel 中的文本换行?

ios - cocoa : Can't remove child view controller

ios - 需要在底部中心为我的 UIButton 设置自动布局约束使用代码

ios - AVAudioPlayer 持续时间改变

objective-c - 尽管导入了正确的 header ,但使用自定义类的编译器错误

ios - 可以在 viewDidLoad 中使用 layoutIfNeeded 来确定角半径大小吗?

ios - 当我执行 CTFontManagerRegisterGraphicsFont 时出现内存泄漏

objective-c - Apple 魔术鼠标 API

objective-c - UIView 如何知道添加的 subview 的 UIViewController?