Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.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 在Xcode中有多个情节提要_Ios_Swift_Xcode_Uistoryboard - Fatal编程技术网

Ios 在Xcode中有多个情节提要

Ios 在Xcode中有多个情节提要,ios,swift,xcode,uistoryboard,Ios,Swift,Xcode,Uistoryboard,问题:在具有表视图的xib中创建了视图控制器。但是,表视图的行为非常奇怪,看起来不像我们在故事板中使用的表视图 我的计划是用我在另一个xib文件中创建的自定义表格视图单元格填充此表格视图。但是,遗憾的是,它没有像我预期的那样工作,因为一切都关闭了,并且这些单元格被实例化。我知道我的自定义单元格可以工作,因为它在故事板中创建的另一个视图控制器中工作: let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil) let view

问题:在具有表视图的xib中创建了视图控制器。但是,表视图的行为非常奇怪,看起来不像我们在故事板中使用的表视图

我的计划是用我在另一个xib文件中创建的自定义表格视图单元格填充此表格视图。但是,遗憾的是,它没有像我预期的那样工作,因为一切都关闭了,并且这些单元格被实例化。我知道我的自定义单元格可以工作,因为它在故事板中创建的另一个视图控制器中工作:

let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let viewController = storyboard.instantiateInitialViewController()!


我想要一种设计视图控制器的方法,这样我就可以在需要的时候实例化它们,我的理由是我不想有一个非常填充的故事板。现在我知道我不能像在故事板中那样在xib文件中使用表视图。有办法解决这个问题吗?我需要另一个故事板来实现这一点吗?

在同一个项目中可以有多个故事板

我通常希望每个“主”屏幕有一个故事板,这很容易通过编程实例化,也可以在IB中链接


至于您的问题,我建议在.xib文件中创建自定义uitableviewcell。作为一个独立的观点。通过这种方式,您可以在代码中将其注册为所需任何视图控制器的“可重用单元”,而不考虑包含它的情节提要

是的,您可以在应用程序中使用任意数量的故事板

我尝试为每个工作流设置一个故事板。然而,一些不太热衷于情节提要的开发人员在每个视图控制器上使用一个情节提要

let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "MyViewController")!
要从名为“MyStoryboard”的故事板获取初始视图控制器,请执行以下操作:

或者获取标识符为“MyViewController”的视图控制器


视图控制器访问另一个故事板中的第二个视图控制器有两种基本方式

  • 在情节提要中,使用指向第二个视图控制器的按钮,然后使用显示序列推送第二个视图控制器

  • let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
    let viewController = storyboard.instantiateViewController(withIdentifier: "MyViewController")!
    
  • 在视图控制器的源中,使用或创建第二个视图控制器,并将其推送到导航控制器上

  • let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
    let viewController = storyboard.instantiateViewController(withIdentifier: "MyViewController")!
    

    任务1。加载UIViewcontroller表单Xib

    我们可以加载UIViewcontroller表单Xib,而不是故事板。我们可以使用以下程序:

    1。创建UIViewcontroller。

     XCode File -> New -> File -> Cocoa Touch Class -> Fill Class with your class name , subclass of with UIViewController , check Also create Xib file, language Swift -> Next - Create.
    
     Example: ViewControllerFromXib
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) 
    {
       super.init(nibName: "ViewControllerFromXib", bundle: Bundle.main)
    }
    
    required init?(coder aDecoder: NSCoder) 
    {
       super.init(coder:aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Item"
        self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle:Bundle.main), forCellReuseIdentifier: "CustomTableViewCell");
    }
    
    extension ViewControllerFromXib:UITableViewDelegate {
    
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
    
            return UITableViewAutomaticDimension;
    
        }
    
    }
    
    2。重写初始化()。

     XCode File -> New -> File -> Cocoa Touch Class -> Fill Class with your class name , subclass of with UIViewController , check Also create Xib file, language Swift -> Next - Create.
    
     Example: ViewControllerFromXib
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) 
    {
       super.init(nibName: "ViewControllerFromXib", bundle: Bundle.main)
    }
    
    required init?(coder aDecoder: NSCoder) 
    {
       super.init(coder:aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Item"
        self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle:Bundle.main), forCellReuseIdentifier: "CustomTableViewCell");
    }
    
    extension ViewControllerFromXib:UITableViewDelegate {
    
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
    
            return UITableViewAutomaticDimension;
    
        }
    
    }
    
    3。打开新创建的控制器

    let controller = ViewControllerFromXib.init()
    self.present(controller, animated: true, completion: nil)
    
    extension ViewControllerFromXib:UITableViewDataSource {
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 3;
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
            return cell
    
        }
    
    }
    
    通过上述方式,我们可以从XIB加载UIViewcontroller

    任务2。创建tableview并使用自定义xib填充其单元格

    1。创建自定义UItableViewCell

    XCode File->New->File->cococoa Touch Class->用您的单元格名称填充类,使用TableViewCell的子类,同时选中CreateXIB File,language Swift->Next-create

    示例:CustomTableViewCell

    1.为您的TableView注册UItableViewCell。

     XCode File -> New -> File -> Cocoa Touch Class -> Fill Class with your class name , subclass of with UIViewController , check Also create Xib file, language Swift -> Next - Create.
    
     Example: ViewControllerFromXib
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) 
    {
       super.init(nibName: "ViewControllerFromXib", bundle: Bundle.main)
    }
    
    required init?(coder aDecoder: NSCoder) 
    {
       super.init(coder:aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Item"
        self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle:Bundle.main), forCellReuseIdentifier: "CustomTableViewCell");
    }
    
    extension ViewControllerFromXib:UITableViewDelegate {
    
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
    
            return UITableViewAutomaticDimension;
    
        }
    
    }
    
    2。在Viewcontroller中实现UITableViewDataSource

    let controller = ViewControllerFromXib.init()
    self.present(controller, animated: true, completion: nil)
    
    extension ViewControllerFromXib:UITableViewDataSource {
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 3;
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
            return cell
    
        }
    
    }
    
    2。在Viewcontroller中实现UITableViewDelegate。

     XCode File -> New -> File -> Cocoa Touch Class -> Fill Class with your class name , subclass of with UIViewController , check Also create Xib file, language Swift -> Next - Create.
    
     Example: ViewControllerFromXib
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) 
    {
       super.init(nibName: "ViewControllerFromXib", bundle: Bundle.main)
    }
    
    required init?(coder aDecoder: NSCoder) 
    {
       super.init(coder:aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Item"
        self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle:Bundle.main), forCellReuseIdentifier: "CustomTableViewCell");
    }
    
    extension ViewControllerFromXib:UITableViewDelegate {
    
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
    
            return UITableViewAutomaticDimension;
    
        }
    
    }
    

    您所创建的看起来像一个表视图,而不是一个表视图控制器。这就是他们不同的原因。此外,没有什么可以阻止您创建另一个故事板。随着项目的发展,这也变得非常普遍。嗨,Yuchen,它本来是一个表视图,而不是一个表视图控制器。为什么人们在他们的项目中有不止一个故事板,为什么它是有用的和常见的?故事板是基于xml的,当有多个人同时处理它时,很容易导致冲突。这些冲突很难解决。此外,随着情节提要变得越来越复杂,它的渲染速度可能会非常慢,也很难使用。所以,是的,保持它们的小型化更容易。看看故事板参考资料。例如,请参见:感谢您的回复,如果我的第一个故事板包含导航视图控制器,那么我的第二个故事板如何知道嵌入在第一个故事板中的导航控制器?换句话说,如何继续“导航”?我创建了一个自定义表视图单元格。为什么有很多故事板对你的工作流程有帮助?感谢因为在处理大型项目时,许多人可以专注于应用程序的各个部分。另外,现在拆分它们确实没有什么坏处,因为故事板可以使用故事板引用彼此链接,或者只是有一个管理类以编程方式显示所需的故事板。这对于MVVM模式特别好,因为视图和它们的控制器中几乎没有业务逻辑。