how to add textfield delegate in swift - swift

I need to add delegate to textfields; my understanding is it can be done two ways:
we go to file and viewController.swift and under class, after UIviewcontroller we add comma and then type UITextFieldDelegate .
and then u under viewDidLoad we add method and function
when on storyboard we click and drag textfield to small icon on top of view that says view controller and pick delegate
Do they both work the same? or there is difference if we do one way or the other?
and what you do if you have more than one textfields, I have 10 textfields and have 7 functions and buttons, I need to do this so I can disable button if my textfield is empty for that button.

There is no difference, as far as I know. Choose what you like more.
In first case you also need to make #IBOutlet for your UITextField(click and drag UITextField from UIStoryboard to your UIViewController code) and after that make UIViewController delegate of UITextField
For example:
import UIKit
class MyViewController: UIViewController, UITextFieldDelegate
{
#IBOutlet weak var myTextField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
myTextField.delegate = self
}
}

Related

attach several UILabels from storyboard to one IBOutlet

I've 15 Labels in my Storyboard they are just texts, also set from storyboard, What I want to do is to style them, but programitically, Therefore I need to create 15 IBOutlets in my ViewController, I wonder if there is any other way of doing that, without 15 IBOutlets,if it's possible to create 1 IBOutlet and attach all of them to that one? because creating 15 of them is kinda stressing...
You can do this with Outlet Collections instead of an IBOutlet for all the labels you want to group together:
One way to do it is to ctrl+drag from your storyboard to your editor and select outlet collection
This will create #IBOutlet weak var labelCollection: UILabel! in your code
This works fine but then you need to add an additional check for the type when looping:
#IBOutlet weak var labelCollection: UILabel!
func setCustomLayout()
{
for label in labelCollection2.subviews
{
if let label = label as? UILabel
{
// do your custom set up here
}
}
}
What I like to do is to create the specific outlet collection in code first if I way to track the same type like so:
#IBOutlet var labelCollection: [UILabel]!
The I drag from the editor to the storyboard
Then I can work with it as follows
#IBOutlet var labelCollection: [UILabel]!
func setCustomLayout()
{
for label in labelCollection
{
// do your customization here
}
}
Then you can loop through the UIViews inside the IBOutletCollection and do the needful

How add IBOutlet to class in embedded TableView

Please give me a hint how to add IBOUTLET variables to the ViewControllerSettings class. TableViewController is embedded on Container View.
You need to control drag and drop it inside the ViewControllerSettings class's definition.
class ViewControllerSettings: UITableViewControntroller {
#IBOutlet weak var tableView: UITableView!
//...
}
Note: Make sure the class for the UITableViewController is set to ViewControllerSettings in your storyboard.
#IBOutlet weak var tableView: UITableView!
control Drag the table view to the above #IBOutlet
From your screenshot, it seems that you have not assigned the ViewControllerSettings for UITableViewController inside storyboard.
Inside storyboard, select added UITableViewController, got to Identity Inspector and set Class as ViewControllerSettings.
Also do connect outlets inside your class in .swift file.

How to create a custom UITextField prepackaged with its own formatting behavior?

I am trying to make a reusable textfield that will format its contents to be in a currency format without relying on a containing viewcontroller to implement this behavior for it.
Right now the viewcontroller that the textfield is in is implementing the desired behavior in a TextFieldChange action and it works fine:
class MyViewController: UIViewController {
#IBOutlet weak var TextField: UITextField!
...
...
#IBAction func TextFieldChanged(_ sender: Any) {
// Format text in text field
}
}
This works, but that means I'll have to copy and paste this code into every viewcontroller that I want to have this functionality. I would like it so I could just assign the textfield its own class in the inspector so that this behavior will be a part of every textfield I make using this class. How do I do this?

How to randomize UILabel text each time the view controller is showed

How do I make a label in my ViewController have a diffrent string of text each time the view crontroller is shown? Thanks! I'm using Swift 3
Assuming you know how to add UILabel to your ViewController, here is quick sample how to pick random text on start:
class ViewController: UIViewController {
let allTexts = ["Hey", "Hi", "Hello"]
#IBOutlet weak var label: UILabel! //get UILabel from storyboard
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.label.text = self.allTexts[Int(arc4random_uniform(UInt32(self.allTexts.count)))]
}
}
Adding this code to viewWillAppear will change your text anytime ViewController is about to appear - which means if you cover it with another ViewController (let's say popup) and then hide popup - it will change text.
If your prefer to just do it one time - when UIViewController is created put the same code inside viewDidLoad method.

IBOutlet and IBAction in Swift

I connected a IBOutlet and IBAction to my button variable from Interface Builder to my View Controller. How do I add an action method to the button in Swift?
This code doesn't seem to work.
#IBOutlet var OK: UIButton!
#IBAction func OK(sender: UIButton){}
The Objective-C equivalent I found is:
#interface Controller
{
IBOutlet id textField; // links to TextField UI object
}
- (IBAction)doAction:(id)sender; // e.g. called when button pushed
When you attach a button to the viewController and create an action (IBAction) using ctrl-drag, you create a method that looks likes this in Swift (if it doesn't have arguments):
#IBAction func buttonAction() {}
In Objective-C the same thing will look like this:
- (IBAction)buttonAction {}
So that means that #IBAction func OK(sender: UIButton){} is an action method.
If you want to know about the sender argument, I would recommend this SO post.
Edit:
For what you want to do, I create an IBOutlet and an IBAction, that way I can change its attributes with the outlet variable, and have the action side of things with the IBAction, like what you show above:
#IBOutlet var OK: UIButton!
#IBAction func OK(sender: UIButton){}
For example, if I want to hide the button, I would put this code in the viewDidLoad
OK.hidden = true
The OK in that code is for the outlet variable, if I wanted to print "You pressed me" to the console when the button is pressed, I would use this code:
#IBAction func OK(sender: UIButton){
println("You pressed me")
}
Above I am using the action to print "You pressed me" to the console.
A few things to note:
When Swift 2.0 gets released println will get changed to print. Also with you action and outlet, I would suggest giving them differing names, to make it easier to differentiate the two, something like this:
#IBOutlet var okOutlet: UIButton!
#IBAction func okAction(sender: UIButton){}
Along with that, you should use camel case when naming variables, constants, functions, etc.
One way to do it, is control-drag from your button to your viewcontroller and choose action:
If you have connected your button's action, your code should work just fine.
Here are the steps you can follow-
For #IBOutlet
1.Declare Your Interface builder Element property right after class name
class SomeViewController: UIViewController{
#IBOutlet weak var aTextField : UITextField! ////Your Interface builder Element
2.Hook the IB Element From Storyboard.
For #IBAction
1.Write A method inside your class(say SomeViewController)
#IBAction func anAction(_sender : AnyObject){
}
2.Hook the method from Storyboard.
Hope it might helps.
You can simply add action from your storyboard. See the image.