This question already has answers here:
IBOutlet is nil, but it is connected in storyboard, Swift
(31 answers)
Closed 4 years ago.
I'm pulling out what little hair I have left!
I have a UIViewController with a matching .xib file. In the .xib, I have an IBOutlet that I made in interface builder called "titleText"
In a section of my code, I have the following lines:
let fooVC = FooViewController(nibName: "FooViewController", bundle: Bundle.main)
fooVC.titleText = "TEST"
The viewController "seems" to load fine, I get an object of the correct type, but the IBOutlets are not initialized.
Is there an additional step that I need to do to initialize any IBOutlets that I may have?
This has got to be something really simple, right?!
Outlets are nil until the view loads. So just make another variable and store title string like below
class FooViewController: UIViewController {
#IBOutlet titleText:UILable!
var titleString:String?
override func viewDidLoad() {
super.viewDidLoad()
titleText.text = titleString
}
}
Use:
let fooVC = FooViewController()
fooVC.titleString = "TEST"
Related
This question already has an answer here:
passing data between viewControllers under a tabController in swift, event sequence
(1 answer)
Closed last month.
I'm trying to pass data to other view on tabBar without performing a transition. But the variable I declared (named distance) turns nil even though I changed Its value.
First VC
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vcToSend = storyboard.instantiateViewController(withIdentifier: "listView") as! ListViewController
vcToSend.distance = "abc"
ListViewController
#IBOutlet weak var label: UILabel!
var distance:String?
override func viewDidLoad() {
super.viewDidLoad()
label.text = distance
}
When I tried to print the value of distance, it prints "nil". I checked the names of Storyboard Id and VC. Why I can't pass the data ?
Edit : I tried same code at another View outside the tabBar and it worked, but I still couldn't figured out to do with a tabBar view.
According to Apple Human interface guidelines, you should probably not be doing that.
In any case you would keep this behaviour, the tab bar component is responsible of the instantiation of the new view controller.
What you are doing is instantiating another, which is never shown.
What you can do is something similar to:
self.tabBarController?.viewControllers?.first(where: { $0 is ListViewController })?.distance = 18
I want to access UIElements (e.g Labels and TextFields, etc) of viewController without making IBOutlet connections.
e.g. I have a UITextField in viewController and I want to access it like viewControllerName.textFieldName.text = "something I will set here"
or as the same concept of Android findViewById("id of element")
I have used "Tags" but it does not meet my requirement.
You will create UITextField programmatically, and access the UITextFieldName.text = "Some" programmatically.
Create a label programmatically Check This link
Yes, you can access UIElements(e.g Labels and TextFeilds etc) of viewController without making IBOutlet connections. First give UIElement a Tag.
Then try this. hope this would help:
If you want to access it in the same viewController
let label = self.view.viewWithTag(4) as? UILabel
label?.text = "Hello there"
and if you want to access it from other viewController
in firstViewControler
import UIKit
internal weak var FirstViewController: ViewController?
//in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
FirstViewController = self
}
it will make it accessible in all other viewControllers
then in secondViewController
let newlabel = homeViewController?.view.viewWithTag(4) as? UILabel
newlabel?.text = "new change"
This question already has answers here:
Passing Image to another View Controller (Swift)
(4 answers)
Closed 5 years ago.
Normally, we link the image from storyboard to viewcontroller by IBOutlet by this code:
#IBOutlet private weak var resultImage: UIImageView!
we can update the image by calling this: resultImage.image = UIImage(named: "image"). It works ok
However, when i call this in the prepare for segue, it found nil:
let destinationDetail = segue.destination as! RestaurantViewController
destinationDetail.restaurantImage.image = UIImage(named: "image")
restaurantImage is an Outlet in RestaurantViewController: #IBOutlet weak var restaurantImage: UIImageView!
I dont understand why it found nil, please help!
Because view controlle haven't been set up yet. you can use IBOutlet after view is load.
In viewDidLoad or viewDidAppear of your destination viewController you will do something like this:
override func viewDidLoad() {
super.viewDidLoad()
resultImage.image = newImage
}
I have a UIViewController-class instantiated via Storyboard that contains a constant property. For testing, I want to replace/mock whatever the value of the view controller.
I can actually do this by subclassing and defining a new constant and by overriding the methods that use it. However, I do not know how to instantiate the ViewController, since it's not in the storyboard.
It's important that all views and all other functionality of the original ViewController is still present, of course. How to go about it?
If i understand you need to access a property from another ViewController outside your Storyboard without presenting it. Since you're using swift, all you need to do is instantiate the class itself i believe. For example if the ViewController that is not in the storyboard has a class named "SecondController", and the variable inside second controller is called "stringVar" then all you need to do is this:
var secondVC = SecondController()
secondVC.stringVar = "new string value"
Example:
//SecondVC
import UIKit
class SecondViewController: UIViewController {
var something:String! = "String Value";
}
//Main VC
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var secondVC = SecondViewController()
secondVC.something = "Another String Value"
println(secondVC.something)
}
}
I have a UIScrollView with a lot of different UIViewController.
In one of this ViewController I want to change the IBOutlet of another UIViewController. The error is : found nil while unwrapping an optional value.
But the textView is not "nil".
import UIKit
class WeirdViewController: UIViewController {
let third = ThirdViewController()
#IBAction func font1(sender: AnyObject) {
third.textView.text = "try"
}
}
According to your code you are creating new ThirdViewController
let third = ThirdViewController()
and in this case third.textView is nil.
What you need is to get reference to the existing object of ThirdViewController which is inside of UIScrollView and then change value of textView.text