i have one view.xib file and it's having small container(container view) which holds all the controls like button/textfield, all the events of controls is handle by parent view.xib class file.
我有一个view.xib文件,它有小容器(容器视图),它包含所有控件,如按钮/文本字段,所有控件事件都由父view.xib类文件处理。
My requirement is at one place i need to add/show parent i.e view.xib completed screen. At one more place i need to add only container view. when i add/show container view only, control's associated events/methods doesn't works.
我的要求是在一个地方,我需要添加/显示父,即view.xib完成屏幕。在另一个地方,我只需要添加容器视图。当我只添加/显示容器视图时,控件的相关事件/方法不起作用。
So i thought, if i can change class of container view with parent view.xib's class, my work can be done.
所以我想,如果我可以用父view.xib的类改变容器视图的类,我的工作就可以完成。
so either suggest me some other solutions or class swizzaling if it's possible to handle in this way.
所以,如果有可能以这种方式处理,或者建议我使用其他一些解决方案或类别。
basically i am adding container view on uitableview cell's container(view) my code for same is as below
基本上我在uitableview单元格的容器上添加容器视图(查看)我的代码如下所示
if let questionContainerView = cellQuestionView.viewQuestionContainer {
// let questionContainerView = cellQuestionView
cell.viewQuestionContainer.addSubview(questionContainerView)
questionContainerView.translatesAutoresizingMaskIntoConstraints = false
cell.viewQuestionContainer.addConstraint(NSLayoutConstraint(item: questionContainerView, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: cell.viewQuestionContainer, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0))
cell.viewQuestionContainer.addConstraint(NSLayoutConstraint(item: questionContainerView, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: cell.viewQuestionContainer, attribute: NSLayoutAttribute.right, multiplier: 1, constant: 0))
cell.viewQuestionContainer.addConstraint(NSLayoutConstraint(item: questionContainerView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: cell.viewQuestionContainer, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
cell.viewQuestionContainer.addConstraint(NSLayoutConstraint(item: questionContainerView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: cell.viewQuestionContainer, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
cellQuestionView.layoutIfNeeded()
}
1 个解决方案
#1
1
I believe what you are trying to do is create a re-usable UIView with your control buttons, that you can use in different view controllers. There is a great tutorial on Youtube entitled iOS Basically: Reusable UIView - Programming in Swift (Part 1).
我相信你要做的是用你的控制按钮创建一个可重复使用的UIView,你可以在不同的视图控制器中使用它。在Youtube上有一个很棒的教程,名为iOS基本上:可重用的UIView - Swift中的编程(第1部分)。
Basically, you will want to create a .Xib file dedicated to the view that you want to re-use, with all the actions handled by that custom view class. Every time you will want your custom view, you will need to instantiate it from the Xib file and manually add it as a subview onto the container view.
基本上,您将需要创建一个专用于您要重用的视图的.Xib文件,并且该自定义视图类处理所有操作。每次您需要自定义视图时,您都需要从Xib文件中实例化它并手动将其作为子视图添加到容器视图中。
Good luck and happy coding!
祝你好运,编码愉快!
-- edit --
- 编辑 -
Your updated code shows that this will be part of a UITableView and you are trying to add your custom view on top of a UITableViewCell. You should instead instantiate a custom UITableViewCell and register it as reusable with the table view. This tutorial should guide you on doing just that: Custom UITableViewCell Tutorial - TableView with Images and Text in Swift
您更新的代码显示这将是UITableView的一部分,并且您尝试在UITableViewCell之上添加自定义视图。您应该实例化一个自定义UITableViewCell并将其注册为可重复使用表视图。本教程应该指导您这样做:自定义UITableViewCell教程 - 在Swift中使用图像和文本的TableView
You can define your @IBAction in your custom table cell, and attach your button selectors to your action in your custom cell's nib.
您可以在自定义表格单元格中定义@IBAction,并将按钮选择器附加到自定义单元格的笔尖中的操作。
Cheers!
干杯!
#1
1
I believe what you are trying to do is create a re-usable UIView with your control buttons, that you can use in different view controllers. There is a great tutorial on Youtube entitled iOS Basically: Reusable UIView - Programming in Swift (Part 1).
我相信你要做的是用你的控制按钮创建一个可重复使用的UIView,你可以在不同的视图控制器中使用它。在Youtube上有一个很棒的教程,名为iOS基本上:可重用的UIView - Swift中的编程(第1部分)。
Basically, you will want to create a .Xib file dedicated to the view that you want to re-use, with all the actions handled by that custom view class. Every time you will want your custom view, you will need to instantiate it from the Xib file and manually add it as a subview onto the container view.
基本上,您将需要创建一个专用于您要重用的视图的.Xib文件,并且该自定义视图类处理所有操作。每次您需要自定义视图时,您都需要从Xib文件中实例化它并手动将其作为子视图添加到容器视图中。
Good luck and happy coding!
祝你好运,编码愉快!
-- edit --
- 编辑 -
Your updated code shows that this will be part of a UITableView and you are trying to add your custom view on top of a UITableViewCell. You should instead instantiate a custom UITableViewCell and register it as reusable with the table view. This tutorial should guide you on doing just that: Custom UITableViewCell Tutorial - TableView with Images and Text in Swift
您更新的代码显示这将是UITableView的一部分,并且您尝试在UITableViewCell之上添加自定义视图。您应该实例化一个自定义UITableViewCell并将其注册为可重复使用表视图。本教程应该指导您这样做:自定义UITableViewCell教程 - 在Swift中使用图像和文本的TableView
You can define your @IBAction in your custom table cell, and attach your button selectors to your action in your custom cell's nib.
您可以在自定义表格单元格中定义@IBAction,并将按钮选择器附加到自定义单元格的笔尖中的操作。
Cheers!
干杯!