我有一个UIView子类,它有一个标题的UILabel,一个按钮和另一个子视图,当另一个视图控制器中使用该视图时,该子视图将根据传递的值进行更改。
class CustomView: UIView {
enum PickerType {
case pikerView
case datePicker
}
private var dialogView: UIView!
private var titleLabel: UILabel!
private var pickerView: UIPickerView!
private var datePicker: UIDatePicker!
private var pickerType: PickerType!
init() {
super.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
setupView()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupView() {
self.dialogView = createContainerView()
self.dialogView.layer.shouldRasterize = true
self.dialogView.layer.rasterizationScale = UIScreen.mainScreen().scale
self.layer.shouldRasterize = true
self.layer.rasterizationScale = UIScreen.mainScreen().scale
self.dialogView.layer.opacity = 0.5
self.dialogView.layer.transform = CATransform3DMakeScale(1.3, 1.3, 1)
self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
self.addSubview(self.dialogView)
}
private func createContainerView() -> UIView {
let screenSize = getScreenSize()
let dialogSize = CGSizeMake(screenSize.width, 300)
// For the black background
self.frame = CGRectMake(0, 0, screenSize.width, screenSize.height)
let dialogContainer = UIView(frame: CGRectMake(0, screenSize.height - dialogSize.height, dialogSize.width, dialogSize.height))
//Title
self.titleLabel = UILabel(frame: CGRectMake(10, 10, dialogSize.width - 20, 30))
self.titleLabel.textAlignment = NSTextAlignment.Center
self.titleLabel.font = UIFont.boldSystemFontOfSize(17)
dialogContainer.addSubview(self.titleLabel)
//Add the PickerView
addPickerView(dialogContainer)
return dialogContainer
}
/**Add the appropriate view based on pickerType*/
private func addPickerView(dialogContainer: UIView) {
let screenSize = countScreenSize()
let dialogSize = CGSizeMake(screenSize.width, 250)
if pickerType == PickerType.pikerView {
self.pickerView = UIPickerView(frame: CGRectMake(10, 30, 0, 0))
self.pickerView.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin
self.pickerView.frame.size.width = dialogSize.width - 20
dialogContainer.addSubview(self.pickerView)
} else if pickerType == PickerType.datePicker {
self.datePicker = UIDatePicker(frame: CGRectMake(10, 30, 0, 0))
self.datePicker.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin
self.datePicker.frame.size.width = dialogSize.width - 20
dialogContainer.addSubview(self.datePicker)
}
}
func showPickerView(title: String) {
self.pickerType = PickerType.pikerView
self.titleLabel.text = title
}
func showDatePicker(title: String) {
self.pickerType = PickerType.datePicker
self.titleLabel.text = title
}
}
这个想法是,如果调用了CustomView()。showPickerView,那么将为pickerType变量分配适当的值并加载UIPickerView。如果调用了CustomView()。showDatePicker,则会加载UIDatePicker。
我的问题是,在调用showPickerView()或showDatePicker()之前调用addPickerView。
如何在初始化视图之前传递pickerType的值?
答案 0 :(得分:0)
我通过将初始化程序更改为:
来完成工作search_results
并在CustomView外部取出PickerType枚举。
现在我可以将自定义视图用作:
init(picker: PickerType) {
super.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
self.pickerType = picker
setupView()
}
但是,如果有更有效的方式来实现这一点,我会喜欢建议。