Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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
Ios 如何通过将类名传递给方法来创建子类UIViewController?_Ios_Uiviewcontroller - Fatal编程技术网

Ios 如何通过将类名传递给方法来创建子类UIViewController?

Ios 如何通过将类名传递给方法来创建子类UIViewController?,ios,uiviewcontroller,Ios,Uiviewcontroller,我有4个子类UIViewController,名为AViewController,BViewController,CViewController,DViewController 现在,在工具栏中,单击内容视图,显示AViewController的视图等 我是一个懒惰的人,讨厌写4次“alloc]initwithnibname”代码,所以我写了下面的代码在代码中创建它们 - (void)addChildView:(UIViewController *)childViewController cla

我有4个子类UIViewController,名为AViewController,BViewController,CViewController,DViewController

现在,在工具栏中,单击内容视图,显示AViewController的视图等

我是一个懒惰的人,讨厌写4次“alloc]initwithnibname”代码,所以我写了下面的代码在代码中创建它们

- (void)addChildView:(UIViewController *)childViewController className:(NSString *)viewClassName{
if (childViewController != nil) {
    // add view
    [self.contentView addSubview:childViewController.view];
}else
{
    // init.
    Class v = NSClassFromString(viewClassName);
    UIViewController *childViewControllerNew = nil;
    childViewControllerNew = [[v alloc] initWithNibName:viewClassName bundle:nil];
    [self.contentView addSubview:childViewController.view];
}}
但这不会创建任何ViewController,调试时始终返回nil

你能告诉我有什么问题吗?我可以用这种方法创建子类UIViewController吗


提前谢谢

问题是您正在创建一个视图控制器,但是没有对它做任何操作。有两个问题:

  • 输入错误表示您设置了错误的视图(
    [self.contentView addSubview:childViewController.view];
  • 您没有保留
    childViewControllerNew
  • 您应该将最后一段代码更改为:

    Class v = NSClassFromString(viewClassName);
    UIViewController *childViewControllerNew = nil;
    childViewControllerNew = [[v alloc] initWithNibName:viewClassName bundle:nil];
    [self.contentView addSubview:childViewControllerNew.view];
    
    [self addChildViewController:childViewControllerNew];
    

    如果你真的很懒,你可以声明枚举值,比如

    typedef NS_ENUM (NSInteger, VC) {
    VCa,
    VCb,
    VCc,
    VCd
    };
    
    然后在你的生活中

    - (void)addChildView:(UIViewController *)childViewController className:(VC)typeOfVC {
    switch(typeOfVC)
          case VCa: ViewControllerA *vc = [ViewControllerA alloc] initwithNibName@"YourNibName"]
    [self.contentView addSubview:vc.view];
    case: VCb : same with B 
    etc
    etc
    }
    

    此外,您可以在其他类中使用这些情况来区分ViewController…

    是创建后为nil,还是您只是不保留它,因此在方法结束时将其销毁?不是nil。我会尽量保留它。哦,我的错!我太粗心了。[self.contentView addSubview:childViewController.view]应为[self.contentView addSubview:childviewControllerNew.view]。抱歉耽误您的时间。仅此更改仍然无法保留控制器,仅保留视图…是的,这就是问题所在…正在研究如何保留控制器。我在addChildView方法中将self.viewControllerA作为参数“childViewController”传递,并在代码中添加childViewController=childViewControllerNew。但似乎不起作用。该方法似乎找不到参数acutally是定义的属性(非原子,强)。:(可能一个接一个地编写初始化控制器代码更安全、更快。但至少这个问题让我更了解内存管理和ARC。最后用传统的方法解决这个问题。放弃addChildView方法,逐个编写viewController初始化代码。这次保留了视图控制器。