Ios Can';t在UIViewController子类上设置标题

Ios Can';t在UIViewController子类上设置标题,ios,objective-c,uiviewcontroller,Ios,Objective C,Uiviewcontroller,我有以下问题 我已经创建了一个自定义视图控制器,其中包含一些我需要的有用方法 这是.h中的代码 @interface MYViewController : UIViewController - (void)method; - (void)otherMethod; @end 这是MYViewController类的初始化方法: - (instancetype)init { self = [super init]; return self; } 然后,当我尝试扩展该类时,我

我有以下问题

我已经创建了一个自定义视图控制器,其中包含一些我需要的有用方法

这是.h中的代码

@interface MYViewController : UIViewController

- (void)method;
- (void)otherMethod;

@end
这是MYViewController类的初始化方法:

- (instancetype)init
{
    self = [super init];

    return self;
}
然后,当我尝试扩展该类时,我无法设置子控制器的标题。例如,在“MYOtherController.h”中

这是MYOtherViewController的初始值:

- (instancetype)init
{
    self = [super init];

    return self;
}
然后,如果我实例化一个MYOtherViewController对象并尝试设置标题,它将不会发生任何事情。例如:

MYOtherViewController *controller = [[MYOtherViewController] alloc] init];
controller.title = @"Hello";
如果我将其放在MYOtherViewController类的viewDidLoad中,它会记录标题为nil:

- (void)viewDidLoad {
    NSLog(@"title: %@", self.title);
    [super viewDidLoad];
}

为什么不能在这个子类中设置标题?

在alloc init之后还没有为viewcontroller设置标题,您需要在viewDidLoad之后设置它(当所有UI元素都已初始化时),所以您可以在viewcontroller上创建一个在alloc init之后设置的
@property
,然后在viewDidLoad中,将标题设置为
@属性的值

- (void)viewDidLoad {
    NSLog(@"title: %@", self.title);
    [super viewDidLoad];
}