我注意到,当添加带有界面生成器和笔​​尖的viewController时,

我不必调用initWithNibName来拾取关联的笔尖,我只需调用init!

知道为什么吗?

即。

这个:

NotificationManagementController *notificationView = [[NotificationManagementController alloc] initWithNibName:@"NotificationManagementController" bundle:nil andCurrentNotifications:nil];

和这个:
NotificationManagementController *notificationView = [[NotificationManagementController alloc] init];

两者似乎可以互换...

因此,如果我再调用这些代码行:
notificationView.delegate = self;
notificationView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:notificationView animated:YES completion:NULL];

我看到笔尖的所有变化。

最佳答案

NotificationManagementController *notificationView = [[NotificationManagementController alloc] initWithNibName:@"NotificationManagementController" bundle:nil andCurrentNotifications:nil];

完全不是必需的,甚至甚至被某些人(包括我)所反对。
NotificationManagementController *notificationView = [[NotificationManagementController alloc] init];

更干净(更安全),因为它隐藏了实现细节,但可以有效地在后台调用initWithNibName:

我喜欢这样想:
- (id)init
{
    self = [[NotificationManagementController alloc] initWithNibName:@"NotificationManagementController" bundle:nil andCurrentNotifications:nil];
    if (self)
    {
        // Initialization
    }
    return self;
}

关于ios - initWithNibName vs init-意外行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18045991/

10-10 11:50