I just have a strange trouble. I written a test app, that searches min & max of array values. In first times apps works normally, but only had a warning: "Array MinMax[554:5057] Failed to connect (exitNow) outlet from (Array_MinMax.ViewController) to (NSButton): missing setter or instance variable"
Now, several days later, i started app again, enter some values, pushed button and app instantly crush with same error:
2016-06-15 12:11:40.910 Array MinMax[829:18846] Failed to connect (exitNow) outlet from (Array_MinMax.ViewController) to (NSButton): missing setter or instance variable
(lldb)
Here is the code:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var inputArrayValues: NSTextField!
#IBOutlet weak var minLabel: NSTextField!
#IBOutlet weak var maxLabel: NSTextField!
#IBAction func exitNow(sender: AnyObject) {
NSApplication.sharedApplication().terminate(self)
}
#IBAction func arrayMinMax(sender: AnyObject) {
let someData: String? = inputArrayValues.stringValue
let separators = NSCharacterSet(charactersInString: " ,;:|")
let parts = someData!.componentsSeparatedByCharactersInSet(separators)
let intArray = parts.map{Double($0)}
let minArray = intArray.minElement({$0 < $1}) // - stops on that line (thread 1: breakpoint 1.1)
let maxArray = intArray.maxElement({$0 < $1})
let minString = String(minArray!)
let maxString = String(maxArray!)
minLabel.stringValue = "The minimal value is \(minString)!"
maxLabel.stringValue = "The maximal value is \(maxString)!"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
What to do with that, i can't understand! Is it a bug of Xcode 7.3.1??
Already solved this trouble. Just recreated the button using "NSButton" instead of "Any Object". Then, error goes away!
Related
I have a UIViewController, "MainViewController"-
class MainViewController: UIViewController {
#IBOutlet weak var segmentControl: UISegmentedControl!
#IBOutlet weak var userNameTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var controlButton: RoundedCornerButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func switchBetweenSignInAndRegisterOption(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0{
self.controlButton.text = "Sign In"
}
else{
self.controlButton.text = "Register"
}
}
}
The action method- switchBetweenSignInAndRegisterOption(_:) attached to the segmentControl object and when its selectedSegmentIndex changes, the text property of my RoundedCornerButton is updated. The following is the code for RoundedCornerButton-
class RoundedCornerButton: UIButton {
var text: String = "Sign In"{
didSet{
setNeedsLayout()
}
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.borderWidth = 2.0
self.layer.cornerRadius = (self.bounds.size.width * 0.025)
self.layer.borderColor = UIColor.green.cgColor
self.setTitle(text, for: .normal)
self.titleLabel?.minimumScaleFactor = 0.50
}
}
It works in terms of functionality but it shows the following error in the console. I don't understand what might have gone wrong.
[ProjectName][12943:1296641] Failed to set (titleText) user defined inspected property on ([ProjectName].RoundedCornerButton):
[<[ProjectName].RoundedCornerButton 0x7f8a13d0bd90> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key titleText.
Any help would be greatly appreciated.
I found the issue though I have no idea, how it stayed in my storyboard because I removed the #IBInspectable from my CustomButton's code.
When I searched and couldn't find the titleText anywhere in my code and even in the storyboard user interface, I went to the source code of my storyboard by right clicking on it and select the source code option. To my utter surprise, there was this following code snippet-
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="titleText" value="Register"/>
</userDefinedRuntimeAttributes>
Apparently even though I removed the #IBInspectable from my code, it stayed in my storyboard. I removed this part and now it works fine.
I am new to swift and attempting to place 5 UISwitches on one View Controller. I would like each one to turn the other 4 (or other 1) switches off. I am having a pretty rough go of figuring this one out.. Each one I have named with a label of switch1, switch2, etc. through 5. However, when i type switch1 into the ViewController.swift the variable is not recognized. I assumed I'd be able to type switch1.enabled = false or something to that effect but I only get an error indicating switch1 is not recognized. Help!
You are doing wrong, if you want use switch in controller you must create outlet in view controller.
e.g.
class SwitchDemo: UIViewController{
#IBOutlet weak var switch1: UISwitch!
#IBOutlet weak var switch2: UISwitch!
#IBOutlet weak var switch3: UISwitch!
#IBOutlet weak var switch4: UISwitch!
#IBOutlet weak var switch5: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
}
// Create Switch Toggle Action
#IBAction func onClickSwitch1(_ sender: Any) {
if self.contactSwitch.isOn {
self.switch2.isOn = false
self.switch3.isOn = false
self.switch4.isOn = false
self.switch5.isOn = false
}else {
}
}
}
import UIKit
class ViewController: UIViewController {
#IBOutlet var Text1: UITextField!
#IBOutlet var Text2: UITextField!
#IBOutlet var Text3: UITextField!
#IBOutlet var Label: UILabel!
let array = ["Frodo", "sam", "wise", "gamgee"]
let randomIndex = Int(arc4random_uniform(UInt32(array.count))) //Problem is here
#IBAction func button(_ sender: Any) {
Label.text = array[randomIndex]
}
}
You didn't ask any question so I assume you have problem with accessing count of array.
In general you can't access any of instance members until init is completed.
There are (at least) three solutions for your problem.
First is to override all init required by UIViewController and set randomIndex there.
Second is to use lazy for randomIndex:
lazy var randomIndex = {
Int(arc4random_uniform(UInt32(array.count)))
}()
Third is to use some model (class/struct) for those data and inject it to ViewController
You cannot run the line to get the random index on the top level of the class.
It must be located within a function / method:
#IBAction func button(_ sender: Any) {
let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
Label.text = array[randomIndex]
}
And please conform to the naming convention that variable names start with a lowercase letter (text1, label).
So I have been messing around with saving and calling from NSUserDefaults and I have made progress but I keep on getting this error.
"Expression resolves to an unused function"
The program is supposed to save a string from a textfield so that if you close the program and open it the string will still be their. Unless, you press save again and then the string (Label) will change.
The problem prevents me from running the program so I have not been able to test the new code that I wrote.
Here is the source code feel free to try it. All of this is written in swift.
//
// ViewController.swift
// Help
//
// Created by Lucas on 9/30/15.
// Copyright (c) 2015 Lucas. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet var Savedlbl: UILabel!
#IBOutlet var Textfield: UITextField!
#IBOutlet var Label: UILabel!
var current = ""
var Saved = ""
override func viewDidLoad() {
super.viewDidLoad()
let currentDefault = NSUserDefaults.standardUserDefaults()
Savedlbl.text = currentDefault.valueForKey("saved") as? String
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func Set(sender: AnyObject) {
setall()
}
func setall()
{
current = Textfield.text!
Label.text = Textfield.text!
Savedlbl.text = Textfield.text!
let currentDefault = NSUserDefaults.standardUserDefaults()
Savedlbl.text = currentDefault.valueForKey("saved") as? String
currentDefault.setValue(Saved, forKey: "saved")
currentDefault.synchronize
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The line:
currentDefault.synchronize
should be:
currentDefault.synchronize()
My first question here. I'm making the jump from Delphi and Pascal to Xcode and Swift and feeling totally overwhelmed by the change.
I'm simply attempting to change the text of a label when a button is clicked. Here is my code from ViewController.swift
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var Label1: NSTextField!
#IBOutlet weak var ChangeText: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func ButtonPressed(sender: AnyObject) {
Label1.stringValue = "Hello World"
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
When I run the application as soon as I click the button the application hangs.
I've also tried exchanging stringValue with text but NSTextField does not use this. I can't seem to make the label show up as UITextField.
What am I doing wrong?