简体   繁体   English

Xcode中有多个故事板

[英]Having more than one storyboard in Xcode

The problem: Created a view controller in a xib with a table view. 问题:在带有表视图的xib中创建了一个视图控制器。 But, the table view acts very weirdly and doesn't look like the table view we use in the storyboard. 但是,表格视图的行为非常奇怪,看起来与我们在情节提要中使用的表格视图不一样。

在此处输入图片说明

My plan was to populate this table view with a custom table view cell which I have created in another xib file. 我的计划是使用在另一个xib文件中创建的自定义表格视图单元格填充此表格视图。 But, sadly, it doesn't work as I have expected because everything was off and those cells are instantiated, I know my custom cells work because it has worked in my other view controller that was created in the storyboard: 但是,可悲的是,它没有按我预期的那样工作,因为一切都已关闭并且实例化了这些单元格,我知道我的自定义单元格可以工作,因为它已在故事板中创建的另一个视图控制器中工作:

]]]]]]

I wanted a way to design my view controllers so I can just instantiate them when I need, my reasoning behind this is I don't want to have a very populated storyboard. 我想要一种设计视图控制器的方法,这样我就可以在需要时实例化它们,其背后的原因是我不想拥有一个人口众多的情节提要。 Now I know that I can't use a table view in a xib file like how we use it in a storyboard. 现在,我知道我无法像在情节提要中一样使用xib文件中的表格视图。 Is there a work around to this? 有没有解决的办法? do I need another storyboard to achieve this? 我需要另一个情节提要来实现吗?

Yes, you can use as many storyboards as you would like in an application. 是的,您可以在应用程序中使用任意数量的情节提要。

I try to setup 1 storyboard per workflow. 我尝试为每个工作流程设置1个故事板。 However some less storyboard enthusiastic developers use 1 storyboard per view controller. 但是,对故事板感兴趣的开发人员较少,每个视图控制器使用1个故事板。

To get the initial view controller from a storyboard named "MyStoryboard": 要从名为“ MyStoryboard”的情节提要中获取初始视图控制器,请执行以下操作:

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

or to get a view controller with the identifier "MyViewController". 或获取具有标识符“ MyViewController”的视图控制器。

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

There are two basic ways for a view controller to access a second view controller in another storyboard. 视图控制器有两种基本方法可以访问另一个情节提要中的第二个视图控制器。

  1. In the storyboard, use a Storyboard Reference to the second view controller, then use a show segue to push the second view controller. 在情节提要中 ,使用“ 情节提要”引用第二个视图控制器,然后使用show segue推送第二个视图控制器。

  2. In the view controller's source, create the second view controller using instantiateInitialViewController() or instantiateViewController(withIdentifier:) and push it onto the navigation controller. 在视图控制器的源代码中,使用instantiateViewController(withIdentifier:) instantiateInitialViewController()instantiateViewController(withIdentifier:)创建第二个视图控制器,并将其推送到导航控制器上。

Task 1. Load UIViewcontroller form Xib 任务1.加载UIViewcontroller表单Xib

We can load a UIViewcontroller form Xib instead of Storyboard . 我们可以加载一个UIViewController形式厦门国际银行 ,而不是故事板 We can use following procedure: 我们可以使用以下过程:

1. Create a UIViewcontroller. 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

2. Override init(). 2.覆盖init()。

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) 
{
   super.init(nibName: "ViewControllerFromXib", bundle: Bundle.main)
}

required init?(coder aDecoder: NSCoder) 
{
   super.init(coder:aDecoder)
}

3. Open newly created Controller 3.打开新创建的控制器

let controller = ViewControllerFromXib.init()
self.present(controller, animated: true, completion: nil)

In this above way, We can load a UIViewcontroller from XIB. 通过以上方式,我们可以从XIB加载UIViewcontroller。

Task 2. Create a tableview & populate it's cell using custom xib 任务2.使用自定义xib创建一个tableview并填充其单元格

1. Create a Custom UItableViewCell 1.创建一个自定义UItableViewCell

XCode File -> New -> File -> Cocoa Touch Class -> Fill Class with your cell name , subclass of with TableViewCell , check Also create Xib file, language Swift -> Next - Create. XCode文件->新建->文件->可可触摸类->用您的单元格名称填充类,使用TableViewCell的子类,还请选择创建Xib文件,语言Swift->下一步-创建。

Example: CustomTableViewCell 示例:CustomTableViewCell

1.Register UItableViewCell for Your TableView. 1.为您的TableView注册UItableViewCell。

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "Item"
    self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle:Bundle.main), forCellReuseIdentifier: "CustomTableViewCell");
}

2. Implement UITableViewDataSource into Your Viewcontroller 2.在您的Viewcontroller中实现UITableViewDataSource

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. Implement UITableViewDelegate into Your Viewcontroller. 2.在您的Viewcontroller中实现UITableViewDelegate。

extension ViewControllerFromXib:UITableViewDelegate {

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{

        return UITableViewAutomaticDimension;

    }

}

You can have many storyboards in the same project. 同一项目中可以有许多情节提要。

I usually like to have 1 storyboard per "main" screen, which is easy to instantiate programmatically, and can be linked in the IB as well. 我通常喜欢每个“主”屏幕有1个情节提要,可以很容易地通过编程实例化,也可以在IB中链接。

As for your problem, i'd suggest your custom uitableviewcell be created in a .xib file. 至于您的问题,我建议您在.xib文件中创建自定义的uitableviewcell。 As an independent view. 作为独立的观点。 This way on your code you can just register it as the "reusable cell" for any view controller you want regardless of the storyboard that contains it. 这样,您就可以在代码中将其注册为所需的任何视图控制器的“可重用单元”,而不管包含它的情节提要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有一个故事板的Xcode通用应用程序 - Xcode universal application with one storyboard xcode故事板:导航控制器存在视觉问题 - xcode storyboard: Having visual problems with navigation controllers 在一个xCode项目中拥有多个* .xcodeproj文件是一种好习惯吗? - Is it a good practice to have more than one *.xcodeproj file in an xCode project? xcode iOS NSMutableAttributedString添加属性多次 - xcode iOS NSMutableAttributedString add attribute more than one time 如何让一个按钮做多个动作 xcode - How to Make A button do more than one action xcode 在Xcode中生成一页以上的PDF文件 - Generate PDF file more than one page in Xcode Xcode 4.2 如何一次链接多个object? - How to link more than one object at a time in Xcode 4.2? 显示警报消息同时具有多个零 - Display Alert message while having more than one zeros UIPasteboard没有给出选项,如果有多个字符串,则粘贴该内容 - UIPasteboard not giving option, what to paste if it is having more than one Strings iOS 9:Gesture Recognizer在storyboard / xib中设置,可添加到多个视图中(不工作) - iOS 9: Gesture Recognizer was setup in a storyboard/xib to be added to more than one view (not working)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM