【问题标题】:How to show a ViewController with Xib in Another ViewController as a SubView如何在另一个 ViewController 中将带有 Xib 的 ViewController 显示为 SubView
【发布时间】:2023-03-05 07:05:02
【问题描述】:

我有一个Base Class 我从这个Base Class 继承了我的Parent Class

现在我有另一个ViewControllerXIB。我只想在我的父类中展示这个XIB as 一个视图。

我的 ViewController 与 XIB 的名称是:SearchResultDisplayVC

这段代码我正在使用我的基类将我的 XIB 显示为视图:

 - (void)showSearchResultsView
 {

SearchResultDisplayVC  *searchView =(SearchResultDisplayVC *)[[[NSBundle mainBundle] loadNibNamed:@"SearchResultDisplayVC" owner:[SearchResultDisplayVC class] options:nil] firstObject];


[self.view addSubview:searchView.view];

}

在我的父类中,我将其称为“”

[self showSearchResultsView];

虽然我在编译时没有收到任何错误消息,但是当我运行我的应用程序时它会崩溃并显示以下消息:

*** Terminating app due to uncaught exception
'NSUnknownKeyException',
reason: '[<SearchResultDisplayVC 0x1104bb380> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'

【问题讨论】:

  • 是的,您将 ViewController 添加为 View "addSubView"。
  • 我怎样才能将它添加为子视图或者是他们做@the_UB的任何其他方式
  • 你应该检查你从 nib 收到的第一个对象。我猜它本身就是一个视图而不是你的视图控制器。如果那是一个视图,你可以简单地将它添加到当前视图

标签: ios objective-c iphone xcode uiviewcontroller


【解决方案1】:

您必须在父视图中添加为子视图控制器。

  SearchResultDisplayVC * searchView = [[SearchResultDisplayVC alloc] initWithNibName:@"SearchResultDisplayVC" bundle:nil];
    self.addChildViewController(searchView)

【讨论】:

  • 添加子视图后如何
【解决方案2】:

我希望您的 SearchResultDisplayVC.xib 文件有问题,请检查文件所有者中的视图的插座连接,它应该设置为主视图,以便任何视图控制器都能正常工作。如下所示,

所有视图控制器都希望从 nib 文件中定义和映射“视图”对象。您有一个视图控制器笔尖,但其中没有称为“视图”的插座。否则,您编写的任何代码都可以正常工作。

【讨论】:

    【解决方案3】:

    请将您的“showSearchResultsView”修改为:

    - (void)showSearchResultsView
     {
    
    UIView  *searchView =(SearchResultDisplayVC *)[[[NSBundle mainBundle] loadNibNamed:@"SearchResultDisplayVC" owner:[SearchResultDisplayVC class] options:nil] firstObject];
    
    
    [self.view addSubview:searchView];
    
    }
    

    【讨论】:

    • 感谢您的回复..但是当我运行此代码时,我收到警告
    【解决方案4】:

    经过这么多研究,我终于找到了将其添加到 window 的方法。

    现在我可以使用viewDidLoad 和所有其他方法了。

    - (void)showSearchResultsView
    {
    
    if(!self.searchDisplayView){
    
        self.searchDisplayView = [[SearchResultDisplayVC alloc] initWithNibName:@"SearchResultDisplayVC" bundle:nil];
    
        self.searchDisplayView.delegate = self;
    
    
        CGSize mainScreenSize = [[UIScreen mainScreen] bounds].size;
    
        self.searchDisplayView.view.frame = CGRectMake(0,
                                                   0,
                                                   mainScreenSize.width,
                                                   mainScreenSize.height);
    }
    
    AppDelegate *delegate =  (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate.window addSubview:self.searchDisplayView.view];
    
    
     }
    

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 2023-03-08
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      相关资源
      最近更新 更多