我正在尝试将我在myScrollView
课程AddViewController
中定义的所有元素(UItextfields)转移到另一个FormViewController
,其中我只有一个空getFormView
scrollview我想要显示myScrollView
的所有元素,无论是以复制形式还是获得相同的形式。
我正在将myScrollView
的子视图添加到getFormView
,但这给了我错误
在打开一个可选的
我通过获取输入来创建文本字段,就像设置输入为7一样,将在表单中生成七个文本字段并单击创建我希望这些测试字段应该在formViewController的uiscrollview中,即getformview
FormViewController
import UIKit
class FormViewController: UIViewController {
@IBOutlet weak var menu: UIBarButtonItem!
@IBOutlet weak var getFormView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
menu.target = revealViewController()
menu.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
getFormView.alwaysBounceVertical = true
getFormView.scrollsToTop = true
getFormView.isScrollEnabled = true
getFormView.contentSize = CGSize(width: 343, height: 1500)
print("formViewController")
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
let desController = mainStoryBoard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
self.view.addSubview(self.getFormView)
self.getFormView.addSubview(desController.myScrollView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我的myScrollView的AddViewController图像
getFormView的FormViewController图像
AddViewController
import UIKit
class AddViewController: UIViewController {
var templateName : String?
var templateDesc : String?
var noButtons : Int?
var noTextFields : Int?
var noLabels : Int?
var xValue = 20, yValue = 100
var tagNo = 0
var textObjects : Array<Any>?
var incrementerText : Int?
var xButton = 15 , yButton = 90
var tagButton = 10
var buttonObject : Array<Any>?
var buttonIncreament : Int?
var xLabel = 10 , yLabel = 80
var tagLabel = 15
var labelObject : Array<Any>?
var labelIncreament : Int?
@IBOutlet weak var menu: UIBarButtonItem!
@IBOutlet weak var myScrollView: UIScrollView!
@IBOutlet weak var reset: UIButton!
@IBAction func reset(_ sender: Any) {
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
let desController = mainStoryBoard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
revealViewController().pushFrontViewController(newFrontViewController, animated: true)
}
@IBOutlet weak var createForms: UIButton!
@IBAction func createForms(_ sender: Any) {
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
let desController = mainStoryBoard.instantiateViewController(withIdentifier: "FormViewController") as! FormViewController
// desController.getFormView.addSubview(myScrollView)
let newFrontViewController = UINavigationController.init(rootViewController:desController)
revealViewController().pushFrontViewController(newFrontViewController, animated: true)
}
@IBAction func addForm(_ sender: Any) {
let alertController = UIAlertController(title: "Create Template", message: "", preferredStyle: .alert)
alertController.extendedLayoutIncludesOpaqueBars = true
alertController.addTextField { (textField: UITextField) in
// textField.keyboardAppearance = .dark
textField.keyboardType = .default
textField.autocorrectionType = .default
textField.placeholder = "Title"
textField.clearButtonMode = .whileEditing
}
alertController.addTextField { (textField: UITextField) in
//textField.keyboardAppearance = .dark
textField.keyboardType = .default
textField.autocorrectionType = .default
textField.placeholder = "Description"
textField.clearButtonMode = .whileEditing
}
let okAction = UIAlertAction(title: "Continue", style: UIAlertActionStyle.default) {
UIAlertAction in
print("Ok action")
let templateText = alertController.textFields![0]
let descriptionText = alertController.textFields![1]
self.templateName = templateText.text!
self.templateDesc = descriptionText.text!
if self.templateName?.isEmpty == false && self.templateDesc?.isEmpty == false {
print("Template: \(templateText.text!)\nDescription: \(descriptionText.text!)")
print("\(String(describing: self.templateName!)), \(String(describing: self.templateDesc!))")
self.dismiss(animated: true, completion: nil)
self.drawPop()
}
else {
self.okAlert()
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive) {
UIAlertAction in
self.dismiss(animated: true, completion: nil)
NSLog("Cancel Pressed")
}
//alertController.addAction(alertActionOkay)
// Add the actions
alertController.addAction(cancelAction)
alertController.addAction(okAction)
// alertController.willChangeValue(forKey: "Continue")
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
func drawPop() {
let alertController = UIAlertController(title: "\(self.templateName!)", message: "\(self.templateDesc!)", preferredStyle: .alert)
//attributing textfields labels
alertController.addTextField { (textField: UITextField) in
//textField.keyboardAppearance = .dark
textField.keyboardType = UIKeyboardType.numberPad
// textField.keyboardType = .default
textField.placeholder = "TextFileds"
textField.clearButtonMode = .whileEditing
}
alertController.addTextField { (textField: UITextField) in
// textField.keyboardAppearance = .dark
textField.keyboardType = UIKeyboardType.numberPad
textField.autocorrectionType = .default
textField.placeholder = "Buttons"
textField.clearButtonMode = .whileEditing
}
alertController.addTextField { (textField: UITextField) in
// textField.keyboardAppearance = .dark
textField.keyboardType = UIKeyboardType.numberPad
textField.autocorrectionType = .default
textField.placeholder = "Labels"
textField.clearButtonMode = .whileEditing
}
// Create the actions
let okAction = UIAlertAction(title: "Done", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
let notextFieldText = alertController.textFields![0]
let noButtonText = alertController.textFields![1]
let noLabelText = alertController.textFields![2]
self.noTextFields = Int(notextFieldText.text!)
self.noButtons = Int(noButtonText.text!)
self.noLabels = Int(noButtonText.text!)
print("Template: \(notextFieldText.text!)\nDescription: \(noButtonText.text!)")
print("\(noLabelText.text!)")
print("\(String(describing: self.noTextFields!)), \(String(describing: self.noButtons!)) , \(String(describing: self.noLabels!))")
self.createForms.isHidden = false
self.myScrollView.isHidden = false
self.reset.isHidden = false
self.incrementerText = 20
if self.noTextFields! > 0 {
for _ in 0..<self.noTextFields! {
print ("hello")
let sampleTextField = UITextField(frame: CGRect(x: self.xValue, y: self.yValue, width: 300, height: 40))
sampleTextField.placeholder = "Enter text here"
sampleTextField.font = UIFont.systemFont(ofSize: 15)
sampleTextField.borderStyle = UITextBorderStyle.roundedRect
sampleTextField.autocorrectionType = UITextAutocorrectionType.no
sampleTextField.keyboardType = UIKeyboardType.default
sampleTextField.returnKeyType = UIReturnKeyType.done
sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
sampleTextField.delegate = self as? UITextFieldDelegate
sampleTextField.tag = self.tagNo
// self.view.addSubview(sampleTextField)
//trying to print at container view
self.view.addSubview(self.myScrollView)
self.myScrollView.addSubview(sampleTextField)
let frametext = sampleTextField.frame.size.height
self.textObjects?.append(sampleTextField)
self.yValue = self.yValue + Int(frametext) + 20
self.xValue = 20
}
}
else {
print("no text field found")
}
self.buttonIncreament = 20
if self.noButtons! > 0 {
for _ in 0..<self.noButtons! {
print ("Buttons")
let sampleButton = UIButton(frame: CGRect(x: self.xButton, y: self.yButton, width: 150, height: 25))
sampleButton.contentVerticalAlignment = UIControlContentVerticalAlignment.center
sampleButton.tag = self.tagNo
// self.view.addSubview(sampleButton)
let frametext = sampleButton.frame.size.height
self.view.addSubview(self.myScrollView)
self.myScrollView.addSubview(sampleButton)
self.buttonObject?.append(sampleButton)
self.yButton = self.yButton + Int(frametext) + 20
self.xButton = 20
}
}
else {
print("no button found")
}
self.labelIncreament = 20
if self.noLabels! > 0 {
for _ in 0..<self.noLabels! {
print ("Labels")
let sampleLabel = UILabel(frame: CGRect(x: self.xLabel, y: self.yLabel, width: 150, height: 25))
// sampleLabel.contentVerticalAlignment = UIControlContentVerticalAlignment.center
sampleLabel.tag = self.tagNo
// self.view.addSubview(sampleButton)
let frameLabel = sampleLabel.frame.size.height
self.view.addSubview(self.myScrollView)
self.myScrollView.addSubview(sampleLabel)
self.labelObject?.append(sampleLabel)
self.yLabel = self.yLabel + Int(frameLabel) + 20
self.xLabel = 20
}
}
else {
print("no button found")
}
self.dismiss(animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive) {
UIAlertAction in
self.dismiss(animated: true, completion: nil)
NSLog("Cancel Pressed")
}
//alertController.addAction(alertActionOkay)
// Add the actions
alertController.addAction(cancelAction)
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
func createForm() {
let sampleTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
sampleTextField.placeholder = "Enter text here"
sampleTextField.font = UIFont.systemFont(ofSize: 15)
sampleTextField.borderStyle = UITextBorderStyle.roundedRect
sampleTextField.autocorrectionType = UITextAutocorrectionType.no
sampleTextField.keyboardType = UIKeyboardType.default
sampleTextField.returnKeyType = UIReturnKeyType.done
sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
sampleTextField.delegate = self as? UITextFieldDelegate
self.view.addSubview(sampleTextField)
}
func okAlert() {
let alertController = UIAlertController(title: "Oops!", message: "Enter required data", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
/*
let scrollmyView : UIScrollView = {
let scrollView = UIScrollView()
scrollView.alwaysBounceVertical = true
scrollView.scrollsToTop = true
// myScrollView.alwaysBounceHorizontal = true
scrollView.isScrollEnabled = true
scrollView.contentSize = CGSize(width: 200, height: 1500)
return scrollView
}()
*/
override func viewDidLoad() {
super.viewDidLoad()
menu.target = revealViewController()
menu.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
myScrollView.alwaysBounceVertical = true
myScrollView.scrollsToTop = true
// myScrollView.alwaysBounceHorizontal = true
myScrollView.isScrollEnabled = true
myScrollView.contentSize = CGSize(width: 343, height: 1500)
// scroller.contentSize = CGSize(width: yourWidth, height: yourHeight)
createForms.isHidden = true
myScrollView.isHidden = true
reset.isHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
错误
答案 0 :(得分:1)
如果您使用相同类型的元素,为什么要使用scrollview。最好的方法是使用表格视图。如果大多数行中的元素不同,我们使用scrollview。使用表格视图后,您不必复制任何内容。
解决问题的最佳方法:
如果你有相同类型的视图控制器,那么你可以使用一个视图控制器作为 AddViewController 和 FormViewController 。
答案 1 :(得分:0)
在FormViewController中创建相同的UITextField。
当用户在AddViewController中填写表单时,使用AddViewController的文本字段文本更新FormViewController的文本字段
答案 2 :(得分:0)
content.view.frame = CGRect(x: -UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width, height: view.frame.size.height)
getFormView = content.scrollView
view.addSubview(content.scollView)
content.didMove(toParentViewController: self)
这里的内容是你要添加的视图控制器,它是AddViewController的实例