I am trying to create a simple application that consists of one Navigation controller with 2 screens controlled by a single UIViewController class. The app will allow the user to enter a number from 0 to 10 in a text field and when the user presses a button they will be taken to the 2nd screen showing if they guessed the randomly generated number. I get the "unexpectedly found nil while unwrapping an Optional value" error when I am accessing the text property of the label in the second screen. I dont uderstand why, I have connected the label with the class. Any suggetions?
This is my UI:
This is my Navigation controller class code:
class MyNavController: UINavigationController {
var ranNum:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
ranNum = (Int)(arc4random_uniform(10))
NSLog("random number: \(ranNum)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This is my Generic View controller class:
class GenericViewController: UIViewController {
#IBOutlet weak var inputTextField: UITextField!
#IBOutlet weak var outputLabel: UILabel!
var setThisLabel: String = "You Win!"
#IBAction func guessTheNumber(sender: AnyObject) {
var generatedRanNumber = (parentViewController as! MyNavController).ranNum
var userGuessNumer = inputTextField.text.toInt()
if generatedRanNumber == userGuessNumer {
outputLabel.text = "You Win!"
} else if generatedRanNumber < userGuessNumer {
outputLabel.text = "Think Less..."
} else {
outputLabel.text = "Think Big..."
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
change from
var generatedRanNumber = (parentViewController as! MyNavController).ranNum
to
var generatedRanNumber = (navigationController as! MyNavController).ranNum
also notice that arc4random_uniform(10) returns a number between 0 and 9. you tell the user to guess a number between 0 and 10.
Actually the problem is you are pushing from one instance of Generic View Controller to another without passing on the data. It is generally not recommended to use a generic subclass of UIViewController in this way. If you want your code to work as is, place the UILabel in the same view as your other UI and don't segue (i.e. only have one instance of GenericViewController). Or create two subclasses of UIViewController one called "GuessViewController" and the other called "AnswerViewController" and pass the guess value between them in prepareForSegue. GuessViewController would handle getting the string from the user and evaluate it. AnswerViewController would whether it's right or wrong. It is also generally not necessary to subclass UINavigationController. You could place that code in the viewDidLoad of GuessViewController.
Related
I am new to this board. Please, excuse my bad english in advance.
I am trying to send a string from a subview to his parent view. If I try to set that string to a label, my app crashes with the message "unexpectedly found nil while unwrapping an Optional value".
Example code from the subview:
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blueColor()
sendDataToVc("test")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func sendDataToVc(myString : String) {
let Vc = ViewController()
Vc.dataFromContainer(myString)
}
Example from the parent view:
class ViewController: UIViewController {
#IBOutlet weak var label1: UILabel!
var cacheStr1 : String!
var cacheStr2 : String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
label1.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func dataFromContainer(containerData : String){
label1.text = cacheStr1
}
#IBAction func changeLabel(sender: AnyObject) {
}
I have no more ideas what I am doing wrong. Thank you for your help.
The problem is this line:
let Vc = ViewController()
You are creating a new instance — a new ViewController instance. That's not what you want to do. You want to get a reference to an existing instance — the one that is your view controller's parent view controller, if that's what a View Controller is in relation to your TableViewController.
You better instance your ViewController form StoryBoard and define what you want to pass as property, and then set this property to the value that you need to show, and in the viewDidLoad of your ViewController update your view as you need
I have a viewController with a UISegmentedControl and a UIButton.
Within this viewController, I have two containers, each containing one viewController with a UITextField inside.
I want to save the values in the textField on the click of the button.
Here's the code I have written so far:
View Controller:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//
//
containerA.showView()
containerB.hideView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func buttonTapped(sender: UIButton) {
print(ContainerAViewController.sharedInstance.textFieldA)
}
#IBAction func segmentedControlValueChanged(sender: AnyObject) {
switch(sender.selectedSegmentIndex) {
case 0 : containerA.showView()
containerB.hideView()
case 1 : containerB.showView()
containerA.hideView()
default : containerA.showView()
containerB.hideView()
}
}
#IBOutlet weak var containerA: UIView!
#IBOutlet weak var containerB: UIView!
func hideView(view: UIView) {
view.userInteractionEnabled = false
view.hidden = true
}
func showView(view: UIView) {
view.userInteractionEnabled = true
view.hidden = false
}
}
extension UIView {
func hideView() -> UIView {
self.userInteractionEnabled = false
self.hidden = true
return self
}
func showView() -> UIView {
self.userInteractionEnabled = true
self.hidden = false
return self
}
}
ContainerAViewController:
class ContainerAViewController: UIViewController {
#IBOutlet weak var textFieldA: UITextField!
static let sharedInstance = ContainerAViewController()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
ContainerBViewController:
class ContainerBViewController: UIViewController {
#IBOutlet weak var textFieldB: UITextField!
static let sharedInstance = ContainerBViewController()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
When I tap the button, it gives me the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value
Can somebody please help?
You should not try to manipulate another view controller's views. That violates the principle of encapsulation, an important principle in object-oriented development.
You should give your child view controllers (ContainerAViewController and ContainerBViewController) string properties, and have the code for those view controllers set that string property when the user enters text into the view controllers' text fields.
Ignoring that design flaw, your code doesn't make sense. You show your CLASS as ContainerAViewController, and yet your buttonTapped method is trying to ask a ContainerAViewController singleton for the value of a text field. That makes no sense.
You want to have properties in your parent view controller that point to your child view controllers.
You should implement a prepareForSegue method in your parent view controller, and in that prepareForSegue method, look for the embed segues that fire when the child view controllers are loaded. When that happens you should set your properties that point to the child view controllers.
Im new to swift and I'm experimenting with classes and methods! I have been looking all over google and stackoverflow to find an answer! I have read multiple posts with the same problem but they still don't help me! I have written swift code for a bigger app but decided to write a small portion so I get the same idea. Im trying to update a UILabel's text with a method inside a class when a certain button is pressed. Im trying to change the text by MyLabel.text = "text" but its giving me the error of 'Instance member cannot be used on type "view controller"' Please help me find whats wrong with it and explain it! Thank you so much! Here is my code bellow:
class ViewController: UIViewController {
class Door {
var DoorLocked = false
func lockDoor() {
DoorLocked = true
MyLabel.text = "The door is locked!"
}
func unlockDoor() {
DoorLocked = false
MyLabel.text = "The door is unlocked!"
}
init() {
MyLabel.text = "This is a door!"
}
}
var DoorStatus = Door()
#IBOutlet weak var MyLabel: UILabel!
#IBAction func LockButton(sender: AnyObject) {
DoorStatus.lockDoor()
}
#IBAction func UnlockButton(sender: AnyObject) {
DoorStatus.unlockDoor()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
From a architectural point of view, I would argue that it does not make sense for the Door class to know of any labels on that specific ViewController. Further, the Swift language, does not allow you to access that label inside the nested class. Instead, consider doing something like:
class ViewController: UIViewController {
class Door {
var DoorLocked = false
func lockDoor() {
DoorLocked = true
}
func unlockDoor() {
DoorLocked = false
}
}
var DoorStatus = Door()
#IBOutlet weak var MyLabel: UILabel!
#IBAction func LockButton(sender: AnyObject) {
DoorStatus.lockDoor()
MyLabel.text = "The door is locked!"
}
#IBAction func UnlockButton(sender: AnyObject) {
DoorStatus.unlockDoor()
MyLabel.text = "The door is unlocked!"
}
override func viewDidLoad() {
super.viewDidLoad()
MyLabel.text = "This is a door!"
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The inner class Door doesn't know anything about MyLabel. Inner classes don't share variables with the class they're declared in, unlike other languages. It's pretty much like the Door class is declared at top level along-side ViewController. You need a good bit more background to separate a model & ViewController into separate classes and then make them communicate properly with a protocol/delegate pattern. Unless you're following a template for exactly how, first just do everything within ViewController. So declare your model variable doorLocked directly within ViewController, and update it along with changing the text of the label directly within the #IBAction.
Again caveat that this is just learning iOS & Swift at a basic level, then proper MVC design should come next.
Also, all variables should start with lower-case. Every time you start a variable with uppercase, it hurts the eyes because it looks like a class or other type rather than storage.
I'm developing simple app, where the user logins and can read articles from web. I recently added a code which sets a title at a second view while processing a segue, but my title at the second page is unfortunately nil. I have an object in storyboard connected properly to the variable in view controller, I checked this twice. I have no idea what to do, maybe I have unwrapped something not properly.
Code:
import UIKit
class MainViewController: UIViewController, UITabBarDelegate, UITabBarControllerDelegate, UIToolbarDelegate {
#IBOutlet var titleLabel: UILabel!
#IBOutlet var newsBar: UIToolbar!
#IBOutlet var accountBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
accountBar.delegate = self
newsBar.delegate = self
titleLabel.backgroundColor = UIColor(netHex: 0x00B0E4)
titleLabel.textColor = UIColor.whiteColor()
titleLabel.font = UIFont.boldSystemFontOfSize(16.0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let next: SectionViewController = segue.destinationViewController as! SectionViewController
switch segue.identifier! {
case "hardwareSectionSegue":
next.titleLabel.text = "Rubrika o hardwaru"
case "softwareSectionSegue":
next.titleLabel.text = "Rubrika o softwaru"
case "webSectionSegue":
next.titleLabel.text = "Rubrika o webových technologiích"
case "programmerSectionSegue":
next.titleLabel.text = "Rubrika o programování"
case "mobileSectionSegue":
next.titleLabel.text = "Rubrika o mobilních zažízeních"
default:
next.titleLabel.text = "Rubrika nenalezena"
next.titleLabel.backgroundColor = UIColor.redColor()
}
}
#IBAction func unwindSegue(unwindSegue: UIStoryboardSegue) {}
}
The exception occurs below the first case where I'm trying to assign a value to next.titleLabel.text. It says the titleLabel is nil, which it souldn't be.
The SectionViewController:
import UIKit
class SectionViewController: UIViewController {
#IBOutlet var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I'm pretty sure that it's caused by that type cast, but then how do I properly set the attribute, if the segue doesn't even know what type will the next view have?
I can see what you're trying to do, but I'm afraid iOS doesn't let you do things that way. In your prepareForSegue() method you're trying to modify the view controller that is being created by setting a text value directly.
iOS lazy loads as much as it can, which means at this point in your program the label hasn't actually been created – it really is nil. If you put a breakpoint on that line you should be able to run po next.titleLabel in the debugger window to see nil come back. If you run po next.view first it will force iOS to create the view and all its subviews, at which point running po next.titleLabel again will work as you expect.
If you try creating a template Master-Detail Application project in Xcode you'll see the correct solution: set a property in your new view controller, then transfer that value to your label in viewDidLoad().
Summary: When you're navigating from view controller A to view controller B, don't make A try to configure B's UI. Instead, send B the values it needs, and have B configure itself.
In my application I have a textbox that should be filled with a Double and the number should be saved into a variable but there's an error.
I dragged and dropped the textbox into ViewController.swift so it should be linked. I created a #IBOutlet. I called the textbox mmolText and the variable mmol.
I tried something like: var mmol = mmolText.text but it shows an error:
'ViewController.Type' does not have a member named 'mmolText'.
What's the problem? How can I solve it? Besides the type of the content of the textbox is a string but I should convert it into Double.
Here the code of ViewController.swift is:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var mmolText: UITextField!
var mmol = mmolText.text
#IBOutlet weak var mmolLabel: UILabel!
#IBOutlet weak var mgLabel: UILabel!
#IBAction func convertBM(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
It seems like we probably simply want mmol to exist as a convenient way for getting the text property out of the mmolText textfield, right? So why not use a computed property:
var mmol: String {
get {
return mmolText.text ?? ""
}
set {
mmolText.text = newValue
}
}
The get makes use of the nil coalescing operator. UITextField's text property hasn't been updated with the Objective-C nullability annotations yet, so we need to handle the case of it potentially returning nil.
If we want this to be readonly, we can simply omit the set part.
If we want this as a Double, we can modify the above computed property to look more like this:
var mmol: Double {
get {
return ((mmolText.text ?? "0") as NSString).doubleValue
}
set {
mmolText.text = String("%f", newValue)
}
}
And again, if we want this to be readonly, we can simply omit the set half. And of course, the format string can be played around with to get the string version of the double to show up exactly as you intend when using this set method.
class ViewController: UIViewController {
#IBOutlet weak var mmolText: UITextField!
var mmol: String!
override func viewDidLoad() {
super.viewDidLoad()
mmol = mmolText.text
}
}
This way it works. I can remember something like because at that stage, the properties can exist. Which means, it can be there or it isn't. That's why you can't do it like that.
Don't pin me on this explanation though, I'm not very sure.
mmolText is a property on self. You can't refer to it there because self has not been initialized yet.
You'll have to assign it within awakeFromNib, viewDidLoad, etc.