开发者

Init from nib, but alloc only [UIViewController]

开发者 https://www.devze.com 2022-12-28 15:41 出处:网络
So I\'m doing this in my code now: UIViewController* ctrl = [[UIViewController alloc] // i\'m alloc\'ing a UIViewController...

So I'm doing this in my code now:

UIViewController* ctrl =
     [[UIViewController alloc] // i'm alloc'ing a UIViewController...

       initWithNibName:@"TheNibName" // But this NIB has, within
       // interface builder, a link to "UIViewControllerDERIVATIVE".  So really,
       // `ctrl` is a UIViewControllerDERIVATIVE instance, not just
       // a UIViewController instance.

       bundle:nil] ;

The reason I'm doing this is it makes a massive convenience in writing some code that pushes modal dialogs on.. since Objective-C doesn't support <template>.

M开发者_StackOverflow中文版y question is, is this ok?? Can I [alloc] a UIViewController only, while really what comes out of a NIB is an instance of UIViewControllerDERIVATIVE? Or will it bite me in the ass later?


You cannot allocate an UIViewController and initialize it as a UIViewControllerDERIVATIVE, because there will be a mismatch between the how the two classes see their instances.

Once an instance has been allocated by a class, it is the responsibility of the class to initialize it. This is because the class of the instance is set when the instance is allocated. See NSObject reference more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号