我有两个视图控制器:BSViewControllerBSotherViewControllerBSViewController上有一个按钮,可以与BSotherViewController相连。该部分工作正常,当我在模拟器上运行时,我可以看到一个VC,然后可以看到“另一个” VC。但是我在NSLog(@"other");BSotherViewController中有一个-viewDidLoad,但未在控制台中显示。控制台中显示的NSLog(@"other");BSViewController中类似的-viewDidLoad。我怀疑问题是缺少IBAction,但我无法弄清楚应该去哪里以及如何在情节提要中链接它。

BSViewController.h

#import <UIKit/UIKit.h>

@interface BSViewController : UIViewController

@property (nonatomic) NSInteger number;
@end

BSViewController.m
#import "BSViewController.h"

@interface BSViewController ()
@end
@implementation BSViewController
@synthesize number;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.number = 25;
    NSLog(@"number: %d", self.number);
}

@end

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

@interface BSotherViewController : UIViewController
@property (nonatomic) NSInteger anumber;
@end

BSotherViewController.m
#import "BSotherViewController.h"
#include "BSViewController.h"
@interface BSotherViewController ()

@end

@implementation BSotherViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    NSLog(@"other number:");
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    BSViewController *view = [[BSViewController alloc] init];
    self.anumber = view.number;
    NSLog(@"other number: %d", self.anumber);
}

最佳答案

在情节提要中,第二个视图控制器是否设置为BSotherViewController?您可以将鼠标悬停在情节提要中的视图控制器图标上进行验证。

如果不是,请单击它,然后转到身份控制器并键入。

关于ios - View 确实已加载但未加载viewDidLoad,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15348425/

10-10 21:11