I have used struct to set constant.I have maxTextLength in integer form I have to set different values for different controller like one for 300 and another 1000.Here is my code
struct Validations {
static let maxAudioRecSec:Int = 150
static var maxTextLength = 300 // Default value
}
SecondVC :ViewController {
override func viewDidLoad () {
Validations.maxTextLength = 1000
}
}
So value changed inside SecondVC is retain inside that controller only that is 1000.If I access this value inside another controller should be default 300.
You must use like below, don't use static variable for maxTextLength.
struct Validations {
static let maxAudioRecSec:Int = 150
var maxTextLength = 300 // Default value
}
Now in any ViewController you must use like below.
class SecondVC :ViewController {
var validations = Validations() // create Validations struct object
override func viewDidLoad () {
validations.maxTextLength = 1000 // use like this
}
}
Any doubt plz comment.
Related
I want to pass data between two view controllers, but don't want the view to change when the users presses my save data button.
The users needs to fill in multiple data fields, and when finish can press another button to go to the second view controller.
I found many tutorials how to pass data using segue, but they all change view as soon as the 'save button is pressed'.
Any one can explain to me how to alter the code?
#Phillip Mills: here is how I used your code. (what am I doing wrong?)
code:
//////// declaring classes on FirstViewController (trying it first on only one ViewController)
class FakeVC1 {
func userInput() {
DataModel.shared.username = outbj14u.text
}
class FakeVC2 {
func viewAppears() {
if let name = DataModel.shared.username {
outbj14p.text = name
print("I have nothing to say")
}
}
}
class DataModel {
static let shared = DataModel()
var username: String?
}
////till here
//// here is where i call the functions
override func viewDidAppear(_ animated: Bool) {
FakeVC1().userInput()
FakeVC2().viewAppears()
if let xbj14p = UserDefaults.standard.object(forKey: "outbj14p") as? String
{
outbj14p.text = xbj14p
}
if let xbj14u = UserDefaults.standard.object(forKey: "outbj14u") as? String
{
outbj14u.text = xbj14u
}
////
#Phillip Mills: Below is what I have know. I think I got the code on the FirstViewController right, but the code on the Second View controller must be wrong. I don't get any errors, but the text field on the SecondViewController remains unchanged after putting input on in the FirstViewController
//// Code on the FirstViewController
class DataModel {
static let shared = DataModel()
var username: String?
}
#IBAction func savebj14p(_ sender: Any) {
outbj14p.text = inbj14p.text
DataModel.shared.username = outbj14p.text
UserDefaults.standard.set(inbj14p.text, forKey: "namebj14p")
}
//and on the SecondViewController
#IBOutlet weak var bj14u: UILabel! // connected to a label
//and
class DataModel {
static let shared = DataModel()
var username: String?
}
override func viewDidAppear(_ animated: Bool) {
if let name = DataModel.shared.username {
bj14u.text = name
}
}
In your case, don't pass data.
Create a shared object to act as your data model. When users fill in the fields, update the data model.
When the user moves to the second controller/view, that controller uses the data model object to show what it needs to.
class FakeVC1 {
func userInput() {
DataModel.shared.username = "Me"
}
}
class FakeVC2 {
func viewAppears() {
if let name = DataModel.shared.username {
print(name)
} else {
print("I have nothing to say")
}
}
}
class DataModel {
static let shared = DataModel()
var username: String?
}
FakeVC1().userInput()
FakeVC2().viewAppears()
If you need to pass value to another viewcontroller without changing the view , you can user NSNotificationCenter class
Refer this link for more details
NSNotificationCenter addObserver in Swift
what i will recommend is to use a global variable or array, you will have the info in all view controllers and you will be able to call it in your new view controller.
On my iOS app written in Swift, I have a variable which is initialized on FirstViewController.swift.
I want to assign its value to a label on SecondViewController.swift.
At first I've tried to do it like this on SecondViewController.swift:
var firstViewController: FirstViewController = FirstViewController(nibName: nil, bundle: nil)
var name = firstViewController.name
After the didn't work, I tried to do it using a struct:
// FirstViewController.swift
struct GlobalVariables {
var name: String = "test"
}
// SecondViewController.swift
var name = FirstViewController.GlobalVariables.name
But that didn't work either. After both methods I'm printing the new variable to the console and assign its value to the label, but all I see is nothing on the label and nil on the console.
Can you please help me with that? How can I access to a variable on FirstViewController.swift through SecondViewController.swift?
To pass arguments between View Controllers, you can use segues.
First you have the variable in FirstViewController
FirstViewController: UIViewController {
var name: String = "test"
...
}
Then you have a variable of the same type in SecondViewController
SecondViewController: UIViewController {
var name: String = ""
...
}
To move from FirstViewController, you use a programmatic segue.
self.performSegue(withIdentifier: "Indentifier", sender: nil)
Then, in FirstViewController, define prepareForSegue:sender.
You get a reference to the destination view controller, then set the variable to the one from sending view controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! SecondViewController
vc.name = name
}
EDIT:
If you need it to be accessible in all view controllers, define a Global class. You stated in your question you tried a struct. Instead, try static variables
class Global {
static var name: String?
}
EDIT 2:
Another way to have global variables is with a singleton class
class Global {
static let sharedInstance = Global()
var name: String?
}
Here's a barebones idea of how to do it with a separate file to hold data (values) for your labels to display. Disclaimer: it might not be the best idea (you probably don't want to bind your ViewControllers to this single data file like this), but it should do the trick before you learn other better ways. So, here we go.
You want to use a separate file (name it for example MyTextValuesModel.swift) for all the values you'd like your FirstViewController and SecondViewController to access.
You select File > New > File... from the menu, and choose Swift File.
Then you declare your class:
import Foundation
class ValuesForLabels {
var textForLabel1 = "Label1"
var textForLabel2 = "Label2"
var textForLabel3 = "Label3"
// etc...
}
Now in your FirstViewController and SecondViewController you declare a variable to refer to this class:
var textsForLabels = ValuesForLabels()
Now you can access (read/write) the labels from any view controllers like this:
textsForLabels.textForLabel1 = "NewLabel1Text"
// you've just set a new value for Label1
label1.text = textsForLabels.textForLabel1
// your label1 text is now set to textForLabel1
If you'd want to access label values from another view controller, add a new variable to your new view controller to reference the labels.
I need to execute a function when a variable value changes.
I have a singleton class containing a shared variable called labelChange. Values of this variable are taken from another class called Model. I have two VC classes, one of them has a button and a label and the second only a button.
When the button in the first VC class is pressed I am updating the label with this func:
func updateLabel(){
self.label.text = SharingManager.sharedInstance.labelChange
}
But I want to call the same method whenever the value of the labelChange is changed. So in button click I will only update the labelChange value and when this thing happen I want to update the label with the new value of the labelChange. Also in the second VC I am able to update the labelChange value but I am not able to update the label when this value is changed.
Maybe properties are the solution but can anyone show me how to do so.
Edited second time:
Singleton Class:
class SharingManager {
func updateLabel() {
println(labelChange)
ViewController().label.text = SharingManager.sharedInstance.labelChange
}
var labelChange: String = Model().callElements() {
willSet {
updateLabel()
}
}
static let sharedInstance = SharingManager()
}
First VC:
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBAction func Button(sender: UIButton) {
SViewController().updateMessageAndDismiss()
}
}
Second VC:
func updateMessageAndDismiss() {
SharingManager.sharedInstance.labelChange = modelFromS.callElements()
self.dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func b2(sender: UIButton) {
updateMessageAndDismiss()
}
I made some improvements but I need to reference a label from the first VC class in singleton. Therefore I will update that label of VC in singleton.
When I print the value of labelChange the value is being updated and everything is fine. But when I try to update that value on label from singleton I receive an error:
unexpectedly found nil while unwrapping an Optional value
and the error is pointing in 4th line of singleton class.
You can simply use a property observer for the variable, labelChange, and call the function that you want to call inside didSet (or willSet if you want to call it before it has been set):
class SharingManager {
var labelChange: String = Model().callElements() {
didSet {
updateLabel()
}
}
static let sharedInstance = SharingManager()
}
This is explained in Property Observers.
I'm not sure why this didn't work when you tried it, but if you are having trouble because the function you are trying to call (updateLabel) is in a different class, you could add a variable in the SharingManager class to store the function to call when didSet has been called, which you would set to updateLabel in this case.
Edited:
So if you want to edit a label from the ViewController, you would want to have that updateLabel() function in the ViewController class to update the label, but store that function in the singleton class so it can know which function to call:
class SharingManager {
static let sharedInstance = SharingManager()
var updateLabel: (() -> Void)?
var labelChange: String = Model().callElements() {
didSet {
updateLabel?()
}
}
}
and then set it in whichever class that you have the function that you want to be called, like (assuming updateLabel is the function that you want to call):
SharingManager.sharedInstance.updateLabel = updateLabel
Of course, you will want to make sure that the view controller that is responsible for that function still exists, so the singleton class can call the function.
If you need to call different functions depending on which view controller is visible, you might want to consider Key-Value Observing to get notifications whenever the value for certain variables change.
Also, you never want to initialize a view controller like that and then immediately set the IBOutlets of the view controller, since IBOutlets don't get initialized until the its view actually get loaded. You need to use an existing view controller object in some way.
Hope this helps.
In Swift 4 you can use Key-Value Observation.
label.observe(\.text, changeHandler: { (label, change) in
// text has changed
})
This is basically it, but there is a little catch. "observe" returns an NSKeyValueObservation object that you need to hold! - when it is deallocated, you’ll receive no more notifications. To avoid that we can assign it to a property which will be retained.
var observer:NSKeyValueObservation?
// then assign the return value of "observe" to it
observer = label.observe(\.text, changeHandler: { (label, change) in
// text has changed,
})
You can also observe if the the value has changed or has been set for the first time
observer = label.observe(\.text, changeHandler: { (label, change) in
// just check for the old value in "change" is not Nil
if let oldValue = change.oldValue {
print("\(label.text) has changed from \(oldValue) to \(label.text)")
} else {
print("\(label.text) is now set")
}
})
For More Information please consult Apples documentation here
Apple provide these property declaration type :-
1. Computed Properties:-
In addition to stored properties, classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.
var otherBool:Bool = false
public var enable:Bool {
get{
print("i can do editional work when setter set value ")
return self.enable
}
set(newValue){
print("i can do editional work when setter set value ")
self.otherBool = newValue
}
}
2. Read-Only Computed Properties:-
A computed property with a getter but no setter is known as a read-only computed property. A read-only computed property always returns a value, and can be accessed through dot syntax, but cannot be set to a different value.
var volume: Double {
return volume
}
3. Property Observers:-
You have the option to define either or both of these observers on a property:
willSet is called just before the value is stored.
didSet is called immediately after the new value is stored.
public var totalSteps: Int = 0 {
willSet(newTotalSteps) {
print("About to set totalSteps to \(newTotalSteps)")
}
didSet {
if totalSteps > oldValue {
print("Added \(totalSteps - oldValue) steps")
}
}
}
NOTE:- For More Information go on professional link
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html
There is another way of doing so, by using RxSwift:
Add RxSwift and RxCocoa pods into your project
Modify your SharingManager:
import RxSwift
class SharingManager {
static let sharedInstance = SharingManager()
private let _labelUpdate = PublishSubject<String>()
let onUpdateLabel: Observable<String>? // any object can subscribe to text change using this observable
// call this method whenever you need to change text
func triggerLabelUpdate(newValue: String) {
_labelUpdate.onNext(newValue)
}
init() {
onUpdateLabel = _labelUpdate.shareReplay(1)
}
}
In your ViewController you can subscribe to value update in two ways:
a. subscribe to updates, and change label text manually
// add this ivar somewhere in ViewController
let disposeBag = DisposeBag()
// put this somewhere in viewDidLoad
SharingManager.sharedInstance.onUpdateLabel?
.observeOn(MainScheduler.instance) // make sure we're on main thread
.subscribeNext { [weak self] newValue in
// do whatever you need with this string here, like:
// self?.myLabel.text = newValue
}
.addDisposableTo(disposeBag) // for resource management
b. bind updates directly to UILabel
// add this ivar somewhere in ViewController
let disposeBag = DisposeBag()
// put this somewhere in viewDidLoad
SharingManager.sharedInstance.onUpdateLabel?
.distinctUntilChanged() // only if value has been changed since previous value
.observeOn(MainScheduler.instance) // do in main thread
.bindTo(myLabel.rx_text) // will setText: for that label when value changed
.addDisposableTo(disposeBag) // for resource management
And don't forget to import RxCocoa in ViewController.
For triggering event just call
SharingManager.sharedInstance.triggerLabelUpdate("whatever string here")
HERE you can find example project. Just do pod update and run workspace file.
var item = "initial value" {
didSet { //called when item changes
print("changed")
}
willSet {
print("about to change")
}
}
item = "p"
override var isHighlighted: Bool {
get { super.isHighlighted }
set {
super.isHighlighted = newValue
if newValue {
label.textColor = highlightedTextColor
contentView.backgroundColor = highlightedBackgroundColor
} else {
label.textColor = normalTextColor
contentView.backgroundColor = normalBackgroundColor
}
}
}
I am making a app with many Views. When I am navigating from one View to another I want the data to be sent to a third view without entering the view. I know that it is a way to do this by using this code:
let nextView = self.storyboard?.instantiateViewControllerWithIdentifier("view3") as lastViewController
nextView.data = 1103
But if I do it this way and don't move to the view the data won't be saved. How can I save the data without entering it? Do I need to open it?
In the first view:
var chosenItem:Int = 0
#IBAction func buttonClicked(sender: AnyObject) {
if sender.tag == 0 {
chosenItem = 1
} else if sender.tag == 1 {
chosenItem = 2
} else if sender.tag == 2 {
chosenItem = 3
}
}
This is how my code looks now:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toView2" {
let nextView = segue.destinationViewController as questionAgeViewController
nextView.firstAnswer = chosenItem
}
}
In the next view I send both the first item and the second. And as you understand this is not the best way to get the information. Is there a better?
I think you might be looking for a global variable. Use this code to make the variables:
struct globalVars {
static var firstVar = "Your text"
static var secondVar = Some number
static var thirdVar = Some array
}
In a other view you can access the information like this:
let globalText = globalVars.firstVar
println(globalText)
To edit the variables just use this code:
globalVars.firstVar = "The new text"
Create a singleton class and add properties into it, as per your requirement.
Now you can set its properties from your firstViewController and access the same from SecondViewController.
class Singleton {
class var sharedInstance: Singleton {
struct Static {
static var instance: Singleton?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = Singleton()
}
return Static.instance!
}
}
Singleton tutorials for Swift
http://thatthinginswift.com/singletons/
http://code.martinrue.com/posts/the-singleton-pattern-in-swift
http://www.raywenderlich.com/86477/introducing-ios-design-patterns-in-swift-part-1
I have two view controllers and I have this code to navigate between the two views
ViewController:
func goToSecondViewController() {
let aa = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(aa, animated: true)
}
/
SecondViewController:
func goToFirstViewController() {
self.navigationController.popToRootViewControllerAnimated(true)
}
How to send some data to the first view before popping it ?
If A is a view controller with the following declaration:
class A : UIViewController {
var data: String!
}
Then, whenever you have an instance of A, you can just set the data property directly:
let a = A() // assuming you've defined the init method
a.data = "hello"
Edit
If you want to send data before popping, you'd do something like:
func goToFirstViewController() {
let a = self.navigationController.viewControllers.first as! A
a.data = "data"
self.navigationController.popToRootViewControllerAnimated(true)
}
(haven't compiled the above)