Adding time to a to do list app - swift

I am making a "To Do List" app where you can enter what to do and click submit and it shows up in a table view.
I want to add a time as well, but I cant figure out how to connect a datepicker with the "submit" button then have the time show up in the table view.
can anyone help? thank you.
#IBOutlet weak var date: UIDatePicker!
#IBOutlet weak var item: UITextField!
#IBAction func addItem(sender: AnyObject) {
self.view.endEditing(true)
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
here is the code for the add item page

Related

printing percentage value into decimal swift

The Result I wantedi have three buttons under same IBAction showing different percentages %0, %10, %20.
and I have another button called calculate in another IBAction. so what I want is. when I choose %10 and press calculate I want to print out the 0.1 in the console. this is very beginner question but I m really stuck here
below is my code
import UIKit
class CalculatorViewController: UIViewController {
#IBOutlet weak var billTextField: UITextField!
#IBOutlet weak var zeroPctButton: UIButton!
#IBOutlet weak var tenPctButton: UIButton!
#IBOutlet weak var twentyPctButton: UIButton!
#IBOutlet weak var splitNumberLabel: UILabel!
#IBAction func tipChanged(_ sender: UIButton) {
zeroPctButton.isSelected = false
tenPctButton.isSelected = false
twentyPctButton.isSelected = false
sender.isSelected = true
}
#IBAction func stepperValueChanged(_ sender: Any) {
}
#IBAction func calculatePressed(_ sender: UIButton) {
}
}
The easiest way is to add the output on the percentage button tapped, e.g. in the #IBAction of the "10%" button:
print("0.1")
If you really need to output on the Calculate button tap, you can first determine which percantage button is selected:
if zeroPctButton.isSelected {
print("0.0")
} else if tenPctButton.isSelected {
// ...
If you don't want to hardcode the output, you need some data to base on. Since you don't show us any model code, we can only rely on the text on the buttons. So, the solution might look something like this (inside the Calculate button's #IBAction):
[zeroPctButton,
tenPctButton,
twentyPctButton]
.filter { $0.isSelected }
.forEach {
print("\(Double(($0.titleLabel.text ?? "").filter { ("0"..."9").contains($0) }) / 100.0)")
}
(It takes the text of the selected percentage buttons, filters out only digits from it, converts the resulting digit string to Double, and divides the result by 100.0.)

Add custom button UISearchController in swift

i want to add a custom button on UISearchController in swift.
Like this image i want to add the custom button. In this image it is a search bar. The plus button will show if there is text in the search bar and it will disappear if search bar is empty. can any one help me to implement it.
When the TextField changes, check for whitespace.
Hide or position the button depending on whether it is blank or not.
After the view is loaded, the text field is blank, so it is initialized with ishidden().
I hope my answer is helpful to you.
SearchViewController.swift
import UIKit
class SearchViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var searchTextField: UITextField!
#IBOutlet weak var plusButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
searchTextField.delegate = self
plusButton.isHidden = true
}
func textFieldDidChangeSelection(_ textField: UITextField) {
if searchTextField.text == "" {
plusButton.isHidden = true
} else {
plusButton.isHidden = false
}
}
}
Preview

textfield not empty show save button in Swift

My save button is hidden with the function saveBtnHidden() in the code below. However, the save button won't reappear when text is typed into the text field. I have tried multiple solutions similar to this. Every time that I type into the text field, the save button just won't show up.
import UIKit
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
}
func saveBtnHidden() {
if (nicknameField.text?.isEmpty ?? true) {
// is empty
saveBtn.isHidden = true
} else {
saveBtn.isHidden = false
}
}
#IBAction func saveBtnPressed(_ sender: Any) {
performSegue(withIdentifier: "nextPage", sender: nil)
}
}
You are getting this error because your function saveBtnHidden() is only called once in viewDidLoad(). It does not get called again when the text in your text field changes. To detect when text changes, you will need to add a target to your text field that calls a function when it changes (.editingChanged) like this:
nicknameField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Then in the textFieldDidChange call your saveBtnHidden() function:
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
Code adapted from: How do I check when a UITextField changes?
Use delegate to be notify of any change. Delegates is a key part of iOS development and Apple's framework.
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
nicknameField.delegate = self
}
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
// More of your source code below...

Dismiss UITextField keyboard?

I have multiple textfields
#IBOutlet var emailLogin: UITextField!
#IBOutlet var passLogin: UITextField!
#IBOutlet var emailSignUp: UITextField!
#IBOutlet var passSignUp: UITextField!
as of now these aren't really needed, because of how I am dismissing it by tapping anywhere on the screen, however I also want it to dismiss when I press the return key.
override func viewDidLoad() {
super.viewDidLoad()
self.emailLogin.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
First question: When I need to dismiss with only one I would set the delegate like so, but how do I handle this when I have multiple views that need to be dismissed on return key?
Also, there are two separate views, but both use the same class. Is this a problem for what I am trying to do?
From here
func dismissKeyboard() {
view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
This dismisses the keyboard but only for the textfield that I set as self.
Any info is appreciated!
EDIT:
Change your code to the following and it should work:
#IBOutlet var emailLogin: UITextField?
#IBOutlet var passLogin: UITextField?
#IBOutlet var emailSignUp: UITextField?
#IBOutlet var passSignUp: UITextField?
emailLogin?.delegate = self
passLogin?.delegate = self
emailSignUp?.delegate = self
passSignUp?.delegate = self
The IBOutlets from other class were not initialized when your LoginViewController loads, thus end up with unwrapping nil objects, which is not allowed in Swift. (You should be able to see that in your console output in Xcode.) Use optional variables will prevent that from happening.
Use ViewControllerDelegate to handle tap outside of any textField and textView. It will dismiss keyboard when you tap on outside of textField. Put below code in your view controller:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent:event)
self.view.endEditing(true)
}
You don't need to add TapGesture now to handle this.

Swift: Chain Textfields for Next Button

How does one simply chain up a few textfields for a form?
I found an old Post for obj-C here and unsuccessfully tried to "swift it".
What I tried to do:
#IBOutlet weak var nameTextfield: UITextField!
#IBOutlet weak var emailTextfield: UITextField!
nameTextfield.addTarget(target: emailTextfield, action: becomeFirstResponder(), forControlEvents: UIControlEvents.EditingDidEndOnExit)
Do you have to target the textfield in an other way than by its IBOutlet?
add this function:
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true
}
After return is hit on each textfield the keyboard will resign. You can add tags to your textfields and then add if/else statements for different behaviors depending on which textField.tag responded if you wish.
You do not have to target the textfield by anything other than the IBOutlet for this function.
Try out this code.
func textFieldShouldReturn(textField: UITextField) -> Bool
{
if (usernameTF.resignFirstResponder())
{
passwordTF.becomeFirstResponder()
}
textField.resignFirstResponder();
return true
}