Failed to set (titleText) user defined inspected property - custom-controls

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.

Related

Multiple UISwitches To Turn On/Off

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 {
}
}
}

Xcode 7 missing setter or instance variable

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!

Swift Adding Image to a Button

I want to add an image to a button, and when it is clicked, i want it to change. I'm very new to Swift so I know very little.
I have it like this:
//
// ViewController.swift
// Tip Calculator
//
// Created by ios4 on 10/16/15.
// Copyright (c) 2015 ios4. All rights reserved.
//
import UIKit
class ViewController: UIViewController
{
#IBOutlet weak var tipAmount: UILabel!
#IBOutlet weak var userBillTextField: UITextField!
#IBOutlet weak var tenOutlet: UIButton!
override func viewDidLoad()
{
tipAmount.text = " "
// var billDouble = (userBillTextField.text as NSString).doubleValue
super.viewDidLoad()
}
#IBAction func calculateButton(sender: UIButton)
{
var billDouble = NSString(string: userBillTextField.text).doubleValue
tipAmount.text = String( stringInterpolationSegment: billDouble * 0.15)
}
#IBAction func tenButton(sender: UIButton)
{
}
and I want the outlet called ten outlet to change to a different image called 10_selected_image from 10_unselected_image.
Any help?
Here are my instructions for this : https://s3.amazonaws.com/mmhighschool/MasterDocs/TipCalculator/TipCalculatorAppDirections.pdf
I'm currently working on stretch 3
You should be able to use set image for control state in your button action
button.setImage(image: UIImage(named: <image name>), forState: UIControlState.Normal)
If you need more info about how to make a "toggle button" this thread has a good answer to that
How to use a UIButton as a toggle switch
For me in Swift 5 is working like:
boton1.setBackgroundImage(UIImage(named: "image Name"), for: UIControl.State.normal)

Setting Label text in swift

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?

Changing image of UIButton via click - XCode 6 Swift

Im simply trying to make it so when I click on a UIButton (for which it currently shows the image of a shell), the image changes into something else (in this case, a coin).
This is what i tried so far and have not had any success. I cant find anything to do this for Swift.Thanks.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var lblOutput: UILabel!
#IBOutlet weak var lblWin: UILabel!
#IBOutlet weak var lblLost: UILabel!
#IBOutlet weak var lblWinsAmt: UILabel!
#IBOutlet weak var lblLossesAmt: UILabel!
let coin = UIImage(named: "penny_head") as UIImage;
override func viewDidLoad() {
super.viewDidLoad()
//imgShell1.hidden = true //doesnt work
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnStart(sender: UIButton) {
}
#IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
The way you're setting up the control is incorrect. Assuming you have a button property named btnShell (and it's the button you want to setup) change your viewDidLoad() method to:
override func viewDidLoad()
{
super.viewDidLoad()
btnShell.setImage(imgShell1, forState:.Normal);
btnShell.setImage(coin, forState:.Highlighted);
}
And then remove the setImage(_:forState:) call from the action method:
#IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
To permanently change the button image on tap, you have to use the .Normal enum case and not .Highlighted for the control state:
sender.setImage(coin,forState: UIControlState.Normal)
Setting the image for the .Highlighted state makes the new image appear only when the button is in that state, i.e. when it is tapped.
If you are looking to change the UIButton's background image permanently you have to use Antonio's method:
sender.setImage(coin,forState: UIControlState.Normal)
This won't change the UIKit's default highlighting when you tap the button. When you don't want the button to be highlighted when you touch it or have different appearances for different states, then you might be better off using an UIImageView.
In viewDidLoad():
imgView?.image = imgOne
imgView?.userInteractionEnabled = true
imgView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "changeImage:"))
The function that changes the image:
func changeImage(recognizer: UITapGestureRecognizer){
self.imgView?.image = imgTwo
}
You can also use isSelected.
button.setImage(image2, for: .normal)
button.setImage(image1, for: .selected)

Resources