Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iphone sdk initWithNibName被多次调用_Objective C_Cocoa Touch_Uiviewcontroller - Fatal编程技术网

Objective c iphone sdk initWithNibName被多次调用

Objective c iphone sdk initWithNibName被多次调用,objective-c,cocoa-touch,uiviewcontroller,Objective C,Cocoa Touch,Uiviewcontroller,我试图通过指定视图控制器的名称来动态加载它们,但问题是initWithNibName方法被调用了两次,所以我不能依赖它来进行初始化。这是一个深夜,所以我可能只是错过了一些东西。下面是我用来加载控制器的代码,也许你会在这里发现错误: /* Loads a view from a nib file. The parameter of this method is a nib name withoug an extension (eg. MyView). A controller for the

我试图通过指定视图控制器的名称来动态加载它们,但问题是initWithNibName方法被调用了两次,所以我不能依赖它来进行初始化。这是一个深夜,所以我可能只是错过了一些东西。下面是我用来加载控制器的代码,也许你会在这里发现错误:

/*
 Loads a view from a nib file. The parameter of this method is a nib name 
 withoug an extension (eg. MyView). A controller for the view must exist
 with the same name + "Controller" (eg. MyViewController)
 */
+(UIViewController *)loadViewFromNib:(NSString *)nibName
{
    // Try to create an object by class name
    // We need this so that the controller-specific overriden methods could be called
    Class ctrlClass = NSClassFromString([nibName stringByAppendingString:@"Controller"]);
    NSObject *customctrl = [ctrlClass new];
    UIViewController *ctrl = (UIViewController *)customctrl;
    // Init the controller
    [ctrl initWithNibName:nibName bundle:nil];
    [[ctrl view] setHidden:NO];
    [ctrl autorelease];
    return ctrl;
}
谢谢你的想法

是的,是的

这就是你的问题:

NSObject *customctrl = [ctrlClass new];
UIViewController *ctrl = (UIViewController *)customctrl;
[ctrl initWithNibName:nibName bundle:nil];
+new
alloc
/
init
的同义词
-[UIViewController init]
仅调用
-initWithNibName:bundle:
,并将
nil
作为两个参数。你自己也这么说

换句话说,您要初始化对象两次,这是一个很大的禁忌。以下是您想要的:

UIViewController *ctrl = [[ctrlClass alloc] initWithNibName:nibName bundle:nil];

如果删除
[ctrl-initWithNibName:nibName bundle:nil]它到底调用了一次还是两次?很抱歉耽搁了。你完全正确,我甚至不知道“新”关键字的这种行为。谢谢!