view controller storyboard not displaying - ios

I am a beginner on Xcode and I am trying to embed YouTube video to child controllers of a side menu, but I can't connect my view controller file and my UI view controllers on the storyboard.
I am coding a side menu controller and the child views controllers are called using:
class Course2ViewController: UIViewController, MenuControllerDelegate2 {
private var sideMenu: SideMenuNavigationController?
private let abaController = abaViewController()
private let abbController = abbViewController()
private let abcController = abcViewController()
private let abdController = abdViewController()
override func viewDidLoad() {
super.viewDidLoad()
let menu = MenuController2(with: SideMenuItem2.allCases)
menu.delegate = self
sideMenu = SideMenuNavigationController(rootViewController: menu)
sideMenu?.leftSide = true
SideMenuManager.default.leftMenuNavigationController = sideMenu
SideMenuManager.default.addPanGestureToPresent(toView: view)
AddChildControllers()
}
private func AddChildControllers() {
addChild(abaController)
addChild(abbController)
addChild(abcController)
addChild(abdController)
view.addSubview(abaController.view)
view.addSubview(abbController.view)
view.addSubview(abcController.view)
view.addSubview(abdController.view)
abaController.view.frame = view.bounds
abbController.view.frame = view.bounds
abcController.view.frame = view.bounds
abdController.view.frame = view.bounds
abaController.didMove(toParent: self)
abbController.didMove(toParent: self)
abcController.didMove(toParent: self)
abdController.didMove(toParent: self)
abaController.view.isHidden = true
abbController.view.isHidden = true
abcController.view.isHidden = true
abdController.view.isHidden = true
}
#IBAction func didTapMenuButton2() {
present(sideMenu!, animated: true)
}
func didSelectMenuItem2(named: SideMenuItem2) {
sideMenu?.dismiss(animated: true, completion: nil)
title = named.rawValue
switch named {
case.aba:
abaController.view.isHidden = false
abbController.view.isHidden = true
abcController.view.isHidden = true
abdController.view.isHidden = true
case.abb:
abaController.view.isHidden = true
abbController.view.isHidden = false
abcController.view.isHidden = true
abdController.view.isHidden = true
case.abc:
abaController.view.isHidden = true
abbController.view.isHidden = true
abcController.view.isHidden = false
abdController.view.isHidden = true
case.abd:
abaController.view.isHidden = true
abbController.view.isHidden = true
abcController.view.isHidden = true
abdController.view.isHidden = false
}
}
}
then, I tried to connect my abaViewController file to a new view controller on storyboard by giving the later the class "abaViewController", but my view controller on storyboard is not displayed. Any tips/solutions?

Related

Presenting a UIViewController from an embedded ViewController keeping its bounds

I try to present a ViewController modally from a parent ViewController that's already embedded in a UIView. But whenever I try presenting it, it just pops over the entire window. I want it to be equally sized as its parent. I do not need an animation for the transition. The only thing that worked so far is to again add it's view manually and manipulate the constraints via auto layout. Therefore I would present the controller by hiding its corresponding view. But compared to present that seems more like a workaround than a solution. So far I tried some options on modalPresentationStyle, but none of them seem to work for me. Glad for any help.
Cheers!
class ViewController: UIViewController {
lazy var containerView: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
lazy var viewController: ParentViewController = {
let viewController = ParentViewController()
return viewController
}()
override func viewDidLoad() {
super.viewDidLoad()
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
containerView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.75).isActive = true
containerView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.75).isActive = true
viewController.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(viewController.view)
viewController.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
viewController.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
viewController.view.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
viewController.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
viewController.didMove(toParent: self)
}
}
class ParentViewController: UIViewController {
#IBAction func buttonPressed(_ button: UIButton) {
let childController = UIViewController() // This View Controller should fit it´s parents size. Instead it´s just presented normally and fits the window
present(childController, animated: false)
}
}
Edit
Derived from the community answers I implemented following solution:
extension UIViewController {
func open(_ viewControllerToPresent: UIViewController, in view: UIView? = nil, animated: Bool, duration: TimeInterval = 0.25, completion: (() -> Void)? = nil) {
viewControllerToPresent.view.isHidden = true
open(viewControllerToPresent, in: view)
if animated {
viewControllerToPresent.view.alpha = 0.0
viewControllerToPresent.view.isHidden = false
UIView.animate(withDuration: duration) {
viewControllerToPresent.view.alpha = 1.0
} completion: { (_) in
completion?()
}
}
else {
viewControllerToPresent.view.isHidden = false
completion?()
}
}
func open(_ viewControllerToPresent: UIViewController, in view: UIView? = nil) {
let parentView = view ?? self.view!
addChild(viewControllerToPresent)
viewControllerToPresent.view.translatesAutoresizingMaskIntoConstraints = false
parentView.addSubview(viewControllerToPresent.view)
viewControllerToPresent.view.leadingAnchor.constraint(equalTo: parentView.leadingAnchor).isActive = true
viewControllerToPresent.view.trailingAnchor.constraint(equalTo: parentView.trailingAnchor).isActive = true
viewControllerToPresent.view.topAnchor.constraint(equalTo: parentView.topAnchor).isActive = true
viewControllerToPresent.view.bottomAnchor.constraint(equalTo: parentView.bottomAnchor).isActive = true
viewControllerToPresent.didMove(toParent: self)
}
func close(animated: Bool, duration: TimeInterval = 0.25, completion: (() -> Void)? = nil) {
if animated {
UIView.animate(withDuration: duration) {
self.view.alpha = 0.0
} completion: { (_) in
self.close()
completion?()
}
}
else {
close()
completion?()
}
}
func close() {
view.removeFromSuperview()
willMove(toParent: nil)
removeFromParent()
}
}

FSCalendar content getting squished when I switch scope to month

My FSCalendar's content shrinks when I switch its scope from week to month if there is a view that's constrained to its bottom anchor.
Here is a quick gif to show what exactly is happening
I have tried everything at this point. Using calendar.setScope() instead of calendar.scope =, constraining attachedToCalendarView.topAnchor to calendar.bottomAnchor calendar.contentView.bottomAnchor, and calendar.daysContainer.bottomAnchor, even turning attachedToCalendarView 's constraints on and off depending on whether it's week scope or scope month.
Not sure what else to try. Here is the code:
import UIKit
import FSCalendar
class TestController : UIViewController, FSCalendarDataSource, FSCalendarDelegate, FSCalendarDelegateAppearance {
fileprivate weak var calendar: FSCalendar!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setUp()
}
#objc func switchCalendarScope(){
if self.calendar.scope == FSCalendarScope.month {
self.calendar.scope = FSCalendarScope.week
} else {
self.calendar.scope = FSCalendarScope.month
}
}
func setUp(){
let calendar = FSCalendar()
calendar.dataSource = self
calendar.delegate = self
self.calendar = calendar
self.calendar.scope = .week
self.calendar.locale = Locale(identifier: "en_EN")
self.calendar.calendarHeaderView.calendar.locale = Locale(identifier: "en_EN")
self.calendar.adjustsBoundingRectWhenChangingMonths = true
let testingView = UIView()
testingView.backgroundColor = .red
let attachedToCalendarView = UIView()
attachedToCalendarView.backgroundColor = .blue
view.addSubview(calendar)
view.addSubview(testingView)
view.addSubview(attachedToCalendarView)
self.calendar.translatesAutoresizingMaskIntoConstraints = false
self.calendar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
self.calendar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
self.calendar.widthAnchor.constraint(equalToConstant: view.bounds.size.width).isActive = true
self.calendar.heightAnchor.constraint(equalToConstant: 300).isActive = true
testingView.translatesAutoresizingMaskIntoConstraints = false
testingView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
testingView.widthAnchor.constraint(equalToConstant: view.bounds.size.width).isActive = true
testingView.heightAnchor.constraint(equalToConstant: 20).isActive = true
attachedToCalendarView.translatesAutoresizingMaskIntoConstraints = false
attachedToCalendarView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
// Attaching this view's topAnchor to the calendar's bottom anchor
attachedToCalendarView.topAnchor.constraint(equalTo: self.calendar.contentView.bottomAnchor).isActive = true
attachedToCalendarView.widthAnchor.constraint(equalToConstant: view.bounds.size.width).isActive = true
attachedToCalendarView.heightAnchor.constraint(equalToConstant: 20).isActive = true
// Title and button to toggle the calendar scope
self.navigationItem.title = "Test"
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Toggle", style: .done, target: self, action: #selector(switchCalendarScope))
}
}
Well, I couldn't figure out how to fix the problem itself but I did find a workaround. I placed the calendar inside of an empty container view (just a simple UIView) and attached attachedToCalendarView to the container's bottomAnchor instead of the calendar's itself.
Do note however that using setScope, which animates the transition, still causes the same issue. For it to work you have to set it manually like calendar.scope = x
example:
#objc func switchCalendarScope(){
if self.calendar.scope == FSCalendarScope.month {
// self.calendar.setScope(FSCalendarScope.week, animated: true) // this will cause the calendar to be squished again
self.calendar.scope = .week
movingConstraint.constant = view.safeAreaLayoutGuide.layoutFrame.size.height * -0.20
} else {
// self.calendar.setScope(FSCalendarScope.month, animated: true)
self.calendar.scope = .month
movingConstraint.constant = 0
}
}

Is there a better way to show and hide multiple textviews linked to segmented control than isHidden = true/false?

Beginner here and I've created a VC with 3 segmented controls with 3 segments each. Each segment selection shows a different textview. At any given time, 2 segmented controls show and so you have the option to switch between any of 6 textviews. There exist a total of 9 textviews.
The way I've written this now seems a little glitchy in the simulator and so I want to know of a better way to write this kind of repetitive code. I many lines of .isHidden = false and isHidden = true between all of selections of the 2/3 segmented controls that show at any time. Can anyone tell me how something like can be written in a safer way? If you want to just give a short example or tell me what to google, I appreciate that.
Here's what I have:
class ViewController: UIViewController {
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet weak var textSegmentedControl: UISegmentedControl!
#IBOutlet weak var translationSegmentedControl: UISegmentedControl!
#IBOutlet weak var firstLangText: UITextView!
#IBOutlet weak var secondLangText: UITextView!
#IBOutlet weak var thirdLangText: UITextView!
#IBOutlet weak var translationA: UITextView!
#IBOutlet weak var translationB: UITextView!
#IBOutlet weak var translationC: UITextView!
#IBOutlet weak var textInfo: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
style(control: segmentedControl)
segmentedControl.addTarget(self, action: #selector(updateMainSegmentedControl), for: .valueChanged)
textSegmentedControl.addTarget(self, action: #selector(updateTextSegmentedControl), for: .valueChanged)
translationSegmentedControl.addTarget(self, action: #selector(updateTranslationSegmentedControl), for: .valueChanged)
secondLangText.isHidden = true
thirdLangText.isHidden = true
translationSegmentedControl.isHidden = true
translationA.isHidden = true
translationB.isHidden = true
translationC.isHidden = true
}
//Mark: Stye SegmentControls & underlineBar
func style(control: UISegmentedControl) {
control.backgroundColor = .clear
control.tintColor = .clear
control.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16), NSAttributedStringKey.foregroundColor: UIColor.secondaryColor], for: .normal)
control.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16),NSAttributedStringKey.foregroundColor: UIColor.primaryColor], for: .selected)
}
#IBAction func textOrTranslationChange(_ sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0:
textSegmentedControl.isHidden = false
translationSegmentedControl.isHidden = true
translationUnderlineBar.isHidden = true
firstLangText.isHidden = false
translationA.isHidden = true
textInfo.isHidden = true
case 1:
textSegmentedControl.isHidden = true
translationSegmentedControl.isHidden = false
firstLangText.isHidden = true
translationA.isHidden = false
textInfo.isHidden = true
case 2:
textSegmentedControl.isHidden = true
translationSegmentedControl.isHidden = true
firstLangText.isHidden = true
textInfo.isHidden = false
default:
break;
}
}
#IBAction func selectTextLanguage(_ sender: UISegmentedControl) {
switch textSegmentedControl.selectedSegmentIndex {
case 0:
firstLangText.isHidden = false
secondLangText.isHidden = true
thirdLangText.isHidden = true
translationA.isHidden = true
translationB.isHidden = true
translationC.isHidden = true
case 1:
firstLangText.isHidden = true
secondLangText.isHidden = false
thirdLangText.isHidden = true
translationA.isHidden = true
translationB.isHidden = true
translationC.isHidden = true
case 2:
firstLangText.isHidden = true
secondLangText.isHidden = true
thirdLangText.isHidden = false
translationA.isHidden = true
translationB.isHidden = true
translationC.isHidden = true
default:
break;
}
}
#IBAction func selectTranslationLanguage(_ sender: UISegmentedControl) {
switch translationSegmentedControl.selectedSegmentIndex {
case 0:
translationA.isHidden = false
translationB.isHidden = true
translationC.isHidden = true
firstLangText.isHidden = true
secondLangText.isHidden = true
thirdLangText.isHidden = true
case 1:
translationA.isHidden = true
translationB.isHidden = false
translationC.isHidden = true
firstLangText.isHidden = true
secondLangText.isHidden = true
thirdLangText.isHidden = true
case 2:
translationA.isHidden = true
translationB.isHidden = true
translationC.isHidden = false
firstLangText.isHidden = true
secondLangText.isHidden = true
thirdLangText.isHidden = true
default:
break;
}
}
First you need to extract the behavior which is common from the switch case so that you can remove some duplicate.
#IBAction func textOrTranslationChange(_ sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0:
translationUnderlineBar.isHidden = true
textInfo.isHidden = true
firstLangText.isHidden = false
translationA.isHidden = true
textSegmentedControl.isHidden = false
translationSegmentedControl.isHidden = true
case 1:
textInfo.isHidden = true
firstLangText.isHidden = true
translationA.isHidden = false
textSegmentedControl.isHidden = true
translationSegmentedControl.isHidden = false
case 2:
textInfo.isHidden = false
firstLangText.isHidden = true
textSegmentedControl.isHidden = true
translationSegmentedControl.isHidden = true
default:
break;
}
}
#IBAction func selectTextLanguage(_ sender: UISegmentedControl) {
switch textSegmentedControl.selectedSegmentIndex {
case 0:
showLanaguage(which: firstLangText)
case 1:
showLanaguage(which: secondLangText)
case 2:
showLanaguage(which: thirdLangText)
default:
break;
}
translationA.isHidden = true
translationB.isHidden = true
translationC.isHidden = true
}
func showLanaguage(which: UITextView){
[firstLangText,secondLangText,thirdLangText].forEach({
$0?.isHidden = $0 != which
})
}
func showTranslation(which: UITextView){
[translationA,translationB,translationC].forEach({
$0?.isHidden = $0 != which
})
}
#IBAction func selectTranslationLanguage(_ sender: UISegmentedControl) {
switch translationSegmentedControl.selectedSegmentIndex {
case 0:
showTranslation(which: translationA)
case 1:
showTranslation(which: translationB)
case 2:
showTranslation(which: translationC)
default:
break;
}
firstLangText.isHidden = true
secondLangText.isHidden = true
thirdLangText.isHidden = true
}
You could set them all to a default value before the switch and then just update the different ones in the case. This works especially well in your third method where the default state is true for most views.
#IBAction func selectTranslationLanguage(_ sender: UISegmentedControl)
{
[translationA, translationB, translationC, firstLangText,
secondLangText, thirdLangText].forEach { $0.isHidden = true }
switch translationSegmentedControl.selectedSegmentIndex
{
case 0: translationA.isHidden = false
case 1: translationB.isHidden = false
case 2: translationC.isHidden = false
default: break
}
}
You can reuse your text views if they have the same position. If they are meant to be different, you can group them using outlet collections. So you will have
#IBOutlet weak var langTextViews: [UITextView]!
Instead of:
#IBOutlet weak var firstLangText: UITextView!
#IBOutlet weak var secondLangText: UITextView!
#IBOutlet weak var thirdLangText: UITextView!
It will make your code more organized and readable. You can optimize the textView hiding/showing part. For example, textOrTranslationChange mehtod will look something like this:
#IBAction func textOrTranslationChange(_ sender: UISegmentedControl) {
textSegmentedControl.isHidden = (segmentedControl.selectedSegmentIndex != 0)
langTextViews[0].isHidden = (segmentedControl.selectedSegmentIndex != 0)
translationSegmentedControl.isHidden = (segmentedControl.selectedSegmentIndex != 1)
textInfo.isHidden = (segmentedControl.selectedSegmentIndex != 2)
etc...
}
An elegant solution is to use the beauty of UISegmentedControl. Swift Dictionaries and the tag property.
You can utilize a single UITextView by setting the initial tag of the segmented control and then referencing the tag and selectedIndex to determine which field or value to change. You can then easily store the values in a Dictionary without the need of complex switch statements.
Here is a minimal, repeatable example. you should implement your own methods of handling values after input such as validation.
import Foundation
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
// Properties
var activeSegment: UISegmentedControl?
var activeIndex: Int?
var values:[String: String] = [:]
lazy var segmentedController: UISegmentedControl = {
let seg = UISegmentedControl(items: ["Name", "Age", "Language"])
seg.translatesAutoresizingMaskIntoConstraints = false
seg.tag = 0
seg.addTarget(self, action: #selector(segmentedControllerValueChanged(sender:)), for: .valueChanged)
return seg
}()
lazy var secondSegmentedController: UISegmentedControl = {
let seg = UISegmentedControl(items: ["Type", "Kind", "Thing"])
seg.translatesAutoresizingMaskIntoConstraints = false
seg.tag = 1
seg.addTarget(self, action: #selector(segmentedControllerValueChanged(sender:)), for: .valueChanged)
return seg
}()
let textField: UITextField = {
let tf = UITextField(frame: CGRect.zero)
tf.translatesAutoresizingMaskIntoConstraints = false
tf.placeholder = "Select Field"
return tf
}()
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
self.layoutSubviews()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
func layoutSubviews() {
let guide = self.view.safeAreaLayoutGuide
let spacing:CGFloat = 12.0
self.view.addSubview(segmentedController)
self.view.addSubview(secondSegmentedController)
self.view.addSubview(textField)
segmentedController.widthAnchor.constraint(equalTo: guide.widthAnchor).isActive = true
segmentedController.heightAnchor.constraint(equalToConstant: 55.0).isActive = true
segmentedController.bottomAnchor.constraint(equalTo: guide.bottomAnchor, constant: -spacing).isActive = true
segmentedController.centerXAnchor.constraint(equalTo: guide.centerXAnchor).isActive = true
secondSegmentedController.widthAnchor.constraint(equalTo: guide.widthAnchor).isActive = true
secondSegmentedController.heightAnchor.constraint(equalToConstant: 55.0).isActive = true
secondSegmentedController.bottomAnchor.constraint(equalTo: segmentedController.topAnchor, constant: -spacing).isActive = true
secondSegmentedController.centerXAnchor.constraint(equalTo: guide.centerXAnchor).isActive = true
textField.centerXAnchor.constraint(equalTo: guide.centerXAnchor).isActive = true
textField.centerYAnchor.constraint(equalTo: guide.centerYAnchor).isActive = true
textField.heightAnchor.constraint(equalToConstant: 55.0).isActive = true
textField.widthAnchor.constraint(equalTo: guide.widthAnchor, constant: -spacing*2).isActive = true
}
#objc func segmentedControllerValueChanged(sender: UISegmentedControl) {
// Clear all other segments
let segmentedcontrols = [segmentedController, secondSegmentedController]
segmentedcontrols.forEach { (control) in
if (control.tag != sender.tag) {
control.selectedSegmentIndex = -1
}
}
// Safely get the title for the index
guard let titleForIndex = sender.titleForSegment(at: sender.selectedSegmentIndex) else {
self.textField.placeholder = "Select Field"
return
}
// Set the active fields.
self.textField.text = nil
self.textField.placeholder = "Input " + titleForIndex
self.activeSegment = sender
self.activeIndex = sender.selectedSegmentIndex
// Handle Text Input with your own methods / switch statements
print("Value did change to: \( sender.selectedSegmentIndex) with tag \(sender.tag)" )
print("values: \(values)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - Text Field Delegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Safely get the title for the index
guard let segment = activeSegment, let index = activeIndex, let activeSegmentTitle = segment.titleForSegment(at: index) else {
return false
}
guard let text = textField.text else {
return false
}
values[activeSegmentTitle] = text
textField.resignFirstResponder()
return true
}
}

Eureka Form in UITableView programmatically?

I am using Eureka Forms in my Swift 4 app and I do not use storyboards. Setting up the form programmatically is working fine, however I am using a popoverPresentationController to display a UIViewController with a NavBar in and using the form as a FormViewController means the top of the form is hidden underneath the NavBar.
I'd like make the Class as UIViewController and then add the Eureka form as a Subview so I can using constraints as normal. I understand this is achieved by using UITableView, so I have set it up as follows but I'm not sure how to wire in the Eureka Form and I can't find anything in the documentation about it.
import UIKit
import Eureka
class TestForm: UIViewController, UITableViewDelegate {
let navBar: UINavigationBar = {
let nav = UINavigationBar(frame: .zero)
nav.translatesAutoresizingMaskIntoConstraints = false
return nav
}()
let formTable: UITableView = {
let table = UITableView(frame: .zero, style: .grouped)
table.translatesAutoresizingMaskIntoConstraints = false
table.backgroundColor = .red
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(navBar)
view.addSubview(formTable)
formTable.delegate = self
navBar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
navBar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
navBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
navBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
formTable.translatesAutoresizingMaskIntoConstraints = false
formTable.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
formTable.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
formTable.topAnchor.constraint(equalTo: navBar.bottomAnchor).isActive = true
formTable.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
let navItem = UINavigationItem(title: "Add Event")
let cancelItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel, target: nil, action: #selector(dismissView))
navItem.leftBarButtonItem = cancelItem
navBar.setItems([navItem], animated: false)
Form +++ Section("First Section")
<<< TextRow("Section 1 Text"){ row in
row.title = "Text Row"
row.placeholder = "Enter text here"
}
<<< PhoneRow("Section 1 Phone"){
$0.title = "Phone Row"
$0.placeholder = "And numbers here"
}
.onCellSelection({ (cell, row) in
self.saveForm()
})
}
}
Any pointers?

Hide buttons when textfield equals nil and show when not nil

With my code the buttons are hiding when the viewcontroller is showing because the textfields are empty. The buttons should however show when the textfields are not empty anymore. The buttons are not showing with my code - what am I doing wrong?
let allInputValues = nameInput.text! + middleInput.text! + surnameInput.text!
if allInputValues == "" {
nextButton.hidden = true
finishButton.hidden = true
} else {
nextButton.hidden = false
finishButton.hidden = false
}
Hope you can help me
Set up your view controller as a textField delegate. Then use textFieldDidEndEditing() for each textField to run your check and either keep the button hidden or show the button.
class MyViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
nextButton.hidden = true
finishButton.hidden = true
nameInput.delegate = self
middleInput.delegate = self
surnameInput.delegate = self
}
func textFieldDidEndEditing(textField: UITextField) {
let allInputValues = nameInput.text! + middleInput.text! + surnameInput.text!
if allInputValues == "" {
nextButton.hidden = true
finishButton.hidden = true
} else {
nextButton.hidden = false
finishButton.hidden = false
}
}
}
Swift 3
class MyViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
nextButton.isHidden = true
finishButton.isHidden = true
nameInput.delegate = self
middleInput.delegate = self
surnameInput.delegate = self
}
func textFieldDidEndEditing(textField: UITextField) {
let allInputValues = nameInput.text! + middleInput.text! + surnameInput.text!
if allInputValues == "" {
nextButton.isHidden = true
finishButton.isHidden = true
} else {
nextButton.isHidden = false
finishButton.isHidden = false
}
}
}
You are only running the "if" once, at the start of the program. Place it into a loop that runs it each time the values change.

Resources