【问题标题】:Can't set title on UIViewController subclass无法在 UIViewController 子类上设置标题
【发布时间】:2015-04-07 03:02:36
【问题描述】:

我遇到了以下问题。

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

这是.h中的代码

@interface MYViewController : UIViewController

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

@end

这是我的 MYViewController 类的 init 方法:

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

    return self;
}

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

@interface MYOtherViewController : MYViewController

- (void)childControllerMethod;

@end

这是 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];
}

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

【问题讨论】:

    标签: ios objective-c uiviewcontroller


    【解决方案1】:

    在 alloc init 之后还没有设置 viewcontroller 的标题,你需要在 viewDidLoad 之后设置它(这是所有 UI 元素都已初始化的时候),所以你可以做一个 @property在 alloc init 之后设置的视图控制器上,然后在 viewDidLoad 中,将标题设置为 @property 的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      相关资源
      最近更新 更多