Disable textfield in destination view controller - swift

I have two view controllers: ViewController1 and ViewController2.
My objective is, when the segue is triggered if a certain condition in ViewController1 is met a textfield in viewController2 to be disabled.
I have setup shouldPerformSegueWithIdentifier and prepareForSegue and everything works fine, but when i put the condition it crash saying that it found an error unwrapping an optional- the textfield.
my ViewController1 is :
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject!) -> Bool {
if condition1=true{
return true
}
else{
return false
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier=="segue"){
let destVC:ViewController=segue.destinationViewController as! ViewController2
if n==1{
destVC.myTextField.enabled=false
}
}
}

In prepareForSegue method myTextField of ViewController2 is not initialized yet, so thats why you are getting an error unwrapping an optional textField, To solve your crash create one Bool instance inside your ViewController2 and pass its value in prepareForSegue method.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier=="segue"){
let destVC:ViewController=segue.destinationViewController as! ViewController2
if n==1{
destVC.isEnabled=false
}
}
}
Create instance isEnabled inside ViewController2 like this and used it in the viewDidLoad of ViewController2
var isEnabled: Bool = true
override func viewDidLoad() {
super.viewDidLoad()
self.myTextField.enabled = self.isEnabled
}

The views associated with the destination controller are not yet loaded/instantiated at that point.
Create an instance boolean flag on the destination VC and set that in the prepare segue function. Then in viewDidLoad or later in the destination VC check for that switch and disable the text field.

You can set condition at your destination view controller then you can do this
myTextField.userInteractionEnabled = false
Enable and disable userInteractionEnabled of your textfield.

Related

Value not passing between viewControllers

Struggling to get my viewControllers to send value from the main viewController to a second. I want it to happen on a button click, I'm going to get the value from the button and pass it to the new form. But it just isn't working.
Code for main ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func butClick(_ sender: UIButton) {
NSLog("Button Pressed : %#",[sender .currentTitle])
//var tt = [sender .currentTitle]
// Create the view controller
let vc = TimesTablesViewController(nibName: "TimesTablesViewController", bundle: nil)
vc.passedValue = "xx"
self.performSegue(withIdentifier: "pushSegue", sender: nil)
}
}
Code for second viewController called TimesTablesViewController:
class TimesTablesViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
var passedValue:String = ""
override func viewDidLoad() {
super.viewDidLoad()
titleLabel?.text = "\(passedValue) Times Table"
}
}
I've followed tutorials but can't seem to solve the problem! Thanks for any help!
Replace
self.performSegue(withIdentifier: "pushSegue", sender: nil)
with
self.present(vc,animated:true,completion:nil)
or ( if the current vc is inside a naigation )
self.navigationController?.pushViewController(vc,animated:true)
Using
self.performSegue(withIdentifier: "pushSegue", sender: nil)
is fit with storyboards not xibs and if this your case then you need to use the above line only inside the button action with implementing this method inside the source vc
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pushSegue" {
if let nextViewController = segue.destination as? TimesTablesViewController{
nextViewController.passedValue = "xx"
}
}
}
I’m assuming that the new view controller is appearing, but you’re simply not seeing the data. If so, you’re evidently using storyboards. The TimesTablesViewController(nibName:bundle:) only works if you’re using XIB/NIBs and manually presenting new view controller.
If you’re really using storyboards, simplify your butClick method:
#IBAction func butClick(_ sender: UIButton) {
NSLog("Button Pressed")
performSegue(withIdentifier: "pushSegue", sender: self)
}
But implement prepare(for:sender:):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? TimesTablesViewController {
destination.passedValue = "xx"
}
}
Assuming the above fixes your problem, I might suggest a further simplification. Notably, if your butClick(_:) method is really only calling performSegue, you can segue to this next scene without any #IBAction method at all:
remove butClick(_:) entirely;
remove the connection between the button and the butClick method in IB, on the “Connections Inspector” tab in the right panel; and
control-drag from the button previously hooked up to butClick(_:) to the scene for TimesTablesViewController.
That will simplify your code further.

ViewDidAppear not called automatically

I have a HomeViewController that is opening a WelcomeViewController using segue and is from type Sheet.
In WelcomeViewController the User is entering information and then closing(dismissing) it. After WelcomeViewController is closed I want to update HomeViewController by changing the String value of a TextField.
The Problem is, that ViewDidAppear in HomeViewController is not called after dismissing WelcomeViewController. I did not have this Problem before when developing for iOS.
How can I fix this? I tried to call it manually but got problems, because my IBOutlets were set to nil and I could not change the text in my TextField.
This is the code that invokes the segue:
self.performSegue(withIdentifier: "welcome", sender: self)
this is my viewDidAppear in HomeViewController:
override func viewDidAppear() {
super.viewDidAppear()
if (CoreDataAccess().countUsers() == 0) {
self.performSegue(withIdentifier: "welcome", sender: self)
} else {
//continue execution..
}
}
In WelcomeViewController I dismiss the ViewController using this code
dismiss(self)
This is how I solved it using a delegate.
Thanks to #ullstrm for the suggestion.
First I added this protocol to HomeViewController
protocol WelcomeDelegate {
func insertData(_ name: String, birthday: Date)
}
Then this prepare(for segue:)
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destinationController as? WelcomeViewController {
destinationViewController.delegate = self
}
}
And this extension at the bottom (I know it's not ok to call viewDidAppear manually , but it's working for now)
extension HomeViewController : WelcomeDelegate {
func insertData(_ name: String, birthday: Date) {
viewDidAppear()
}
}
Then in WelcomeViewController I added this variable
var delegate:WelcomeDelegate? = nil
And this line right before dismiss(self)
self.delegate?.insertData(tfName.stringValue, birthday: dpAge.dateValue)
Add your code to ViewWillAppear() instead of ViewDidAppear().
or you call a method in the HomeViewController when dismissing WelcomeViewcontroller
dismiss(animated: true) {
self.presentedViewController.myMethod()
}

How to update a label on the next screen?

Say I have two screens. Screen A has a picker and a button, which triggers a segue to screen B, which displays some content depending on the option selected by the picker.
How do I have the information as to what picker was selected in A, passed to B? So far, I have A doing:
#IBAction func pickThing(_ value: Int) {
self.thing = value;
}
Which seems to work; I believe that it is detecting the value and storing it. However, when I try adding #IBOutlet weak var thingLabel: WKInterfaceLabel! to match the label in B, I can only set the value of it when the app first loads.
If I put self.thingLabel.setText("test") in the awake() function, it sets the label to "test", so that works. But changing it to self.thingLabel.setText("thing \(self.thing)") doesn't work - it sets it to whatever self.thing is initialized as, but doesn't change it later. So awake() is not the right method to use. I've also tried putting it in willActivate and in pickThing, but neither of them did anything.
Is there some method that gets called when a screen is switched to? If not, how can I send data from one screen to the next?
For example on ViewController A use this function
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ViewControllerB {
vc.thing = self.thing
}
}
Or you can use closures in same methods and callback change from B ViewController on A
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ViewControllerB {
vc.clouser = { [weak self] thingB in
guard let `self` = self else { return }
self.thing = thingB
}
}
}
You need to make a variable in view controller B titled something like "parentVC," which would be of view controller A's class. In view controller A's class, you need to call prepare(for segue UIStoryboardSegue, sender: Any?). In this method, you can access the segue's destination property, which would be view controller B. From here you can set view controller B's "parentVC" property to "self," i.e. view controller A. Then in view controller B's class you can access properties of view controller A by using the "parentVC" variable. The code in view controller A's class would look something like this:
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is ViewControllerB {
let viewControllerB = segue.destination as? ViewControllerB
viewControllerB.parentVC! = self
}
}

Change a button's characteristic outside class programatically

In the code below, I want to be able to change the visibility of a button in another class, but when I try to change button.isHidden to false, the button still doesn't show up.
View Controller 1:
override viewDidLoad(){
button.isHidden = true
}
View Controller 2:
ViewController1().button.isHidden = false
How can I change the button's visibility from another controller
Calling ViewController1() creates a ViewController1 instance instead of working with the ViewController1 instance that has already been instantiated.
To access properties (in this case the button) of ViewController1 from ViewController2, you have to pass a reference to that button from ViewController1 to ViewController2 and change the properties through that reference.
You need to set the reference in your prepare(for segue) function in ViewController1.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegue {
let nextVC = segue.destination as! ViewController2
nextVC.button = sender as! UIButton
}
}
You also need to set the segue to manual and call it in ViewController1 by self.performSegue(withIdentifier: "mySegue", sender: self.button)
You need to create the property in ViewController2 and access it like this:
class ViewController2 {
var button:UIButton?
func showButtonOnVC1(){
guard let button = self.button else { return }
button.isHidden = false
}
}

Show Label Value on 2nd View - Swift

Since i'm new and a newbie on Swift can someone tell me how I can get the label value on screen 1 to show on screen 2. Thanks
Edit: So I tried the way you told me but for some reason the Label text did not change in View 2. Any help? Thanks
I am passing textFiled data to destViewController to show how segue performs when passing data.
Note: If you want to pass string data to your destVC.You can assign your string like var someValue: String
mainStoryBoard:
MainVC:
#IBOutlet weak var textField: UITextField!
//or you can assign string like var someValue: : String
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let viewController = segue.destination as! destVC
viewController.dataText = textField.text // someValue.text
}
DestVC:
#IBOutlet var label: UILabel!
var dataText = String()
override func viewDidLoad()
label.text = dataText
}
Output:
You already have a segue between ViewController1 and ViewController2 it seems, so now you just need to pass some date from one to another. This can be done in the function prepare(for segue: UIStoryboardSegue, sender: AnyObject?) which is called when you transition from ViewController1 to ViewController2.
The UIStoryboardSegue class has a property called source of type UIViewController and another property called destination which is also a UIViewController.
So, if you define a property on your ViewController2, like so:
var labelValue: String
Then you can pass a value to it in your prepareForSegue defined on ViewController1 like so:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let viewController2 = segue.destination as? YourViewController2 else {
return
}
viewController2.labelValue = theValueFromViewController1
}
Here is tutorial telling you a bit more (in Objective C though)
And this is also a fine introduction.
Hope that helps you.
I'll consider you have ViewController class VC1 (screen 1) & ViewController class VC2 (screen 2). I see from the screenshot that, you're already using a segue to go from the VC1 to VC2 view.
Now, declare a variable in your VC2 class let's call it labelValue,
class VC2 {
var labelValue: String?
...
}
Whenever you use a storyboard segue to move from one viewcontroller's view to other viewcontroller's view a method named,
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
will get called if it's overriden in the source viewController class (scene1 in your case). In this method you'd be able to access the destination view controller (scene 2) and it's properties. So for your case you can implement it like this,
class VC1 {
//I have assumed the below label will hold the value which you want to pass it to the scene 2.
var lblResult: UILabel!
......
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// You can access the VC2 instance from the segue
if let vc2 = segue.destinationViewController as? VC2 {
vc2.labelValue = lblResult.text
}
}
Now once you implement the prepareForSegue: method correctly as shown above, you'd be able to get the label's value from scene 1 to labelValue property of scene2.
Do learn more about segues here : More About Segues and Storyboards here.
================================
Edit:
Coming to your edited question, in your View2 viewController, you've declared labelNumber1 as String and not as UILabel, so whenever you pass the value to labelNumber1, the value will be containing in this variable and it will not be shown in the screen since it is just a normal string. So what you can do is, declare the labelNumber1 as UILabel
class View2: UIViewController {
#IBOutlet var labelNumber1: UILabel!
.....
}
and in your prepare for segue in ViewController1 make the following change,
class ViewController1: UIViewController {
.....
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// You can access the VC2 instance from the segue
if let view2 = segue.destinationViewController as? VC2 {
view2.labelNumber1.text = lblResult.text
}
}
However if you still want to keep labelNumber1 as String, and to verify whether the value has been passed from ViewController1 during segue, add below line to viewDidLoad() of View2 ViewController.
class View2: UIViewController {
.....
override func viewDidLoad() {
super.viewDidLoad()
print("labelNumber1: \(labelNumber1)")
}
}
And you can see the printed value of labelNumber1 in the console.
HTH :)