my UIswitch value is nil even when its on I have to turn swich off and then back on for it to set value - swift

using UIswitch to set value
my variable is static because im using them in diffrent swift file
so when I run program and click registration button it prints nil
even tho its on (button stays the way it was left when closing app)
I have to toggle it and then click registration button for it to print optional(true)
what can i do so user dont have to togggle everytime they open app or when it shows on when app opened but value is nil
also I just want it to print true/false (how do i unwrap)
class FirstViewController: UIViewController, UITextFieldDelegate {
static var FirstColor: Bool!
#IBAction func home(_ sender: RoundButton) {
}
#IBAction func Registration(_ sender: RoundButton) {
print(FirstViewController.FirstColor)
}
#IBAction func ColorSwitch(_ sender: UISwitch) {
if sender.isOn{
FirstViewController.FirstColor = true
}else{FirstViewController.FirstColor = false }
}
}

If you want to persist the switch status you need to store it in UserDefaults. Don't add a static property in FirstViewController. Create a separate class like this with a computed property
class Color {
static var firstColor: Bool {
get { return UserDefaults.standard.bool(forKey: "firstColor") }
set { UserDefaults.standard.set(newValue, forKey: "firstColor") }
}
}
In FirstViewController's viewDidLoad get last status and update
override func viewDidLoad() {
super.viewDidLoad()
mySwitch.isOn = Color.firstColor
}
In Switch's action change it
#IBAction func ColorSwitch(_ sender: UISwitch) {
sender.isOn = !sender.isOn
Color.firstColor = sender.isOn
}

You could try to use the value of the isOn property directly. Does this help? Also I suggest that you use better names for your handlers.
class FirstViewController: UIViewController, UITextFieldDelegate
{
static var FirstColor: Bool!
#IBAction func registrationTapped(_ sender: RoundButton)
{
print(FirstViewController.FirstColor)
}
#IBAction func colorChanged(_ sender: UISwitch)
{
FirstViewController.FirstColor = sender.isOn
}
}

Related

How do you create a simple login screen in Xcode?

I am making an app that uses a login screen I made. The input is this Text Field that you type in your username. How do I make it respond to different usernames? For example the username is UK. How do I make it respond to if it is or is not 'UK'? I am a complete beginner to Xcode so could you please walk me through the steps please?
I tried using this if(Username!.text, isEqual: "Hello") that I saw from a video, but an error pops up saying:
'(String?, isEqual: String)' is not convertible to 'Bool'.
I have no idea what it means and how to get around it.
import UIKit
class ViewController: UIViewController {
#IBOutlet var ftest: UILabel!
#IBOutlet var Username: UITextField?
#IBOutlet var Password: UITextField!
#IBAction func SignIn(_ sender: Any) {
if(Username!.text, isEqual: "Hello")
}
//this code above is the code I tried to do based on a video and it did not work.
override func viewDidLoad() {
super.viewDidLoad()
}
}
I want it to print "Hi" or something. I just want it to react to:
If the username is 'UK' and
If the username is not 'UK'.
You can just simply use == as follow
#IBAction func SignIn(_ sender: Any) {
if(Username!.text == "Hello" || Username!.text == "UK") {
print("Hi")
}
}
|| means OR
&& means AND
Hope this help!
You can do it in this way
#IBAction func SignIn(_ sender: Any) {
if Username!.text == "UK"
{
print("Hi")
}
else
{
// do your stuff
}
}
Moreover you can also use a variable to check whether a string contained in your textfield or not like this:
var Str = "UK"
if Str.contains(self.Username!.text!) {
print("Hi")
}
Extension way:
extension String {
func contains(find: String) -> Bool{
return self.range(of: find) != nil
}
}
var value = self.Username!.text // You can put value as of your textfield.text and then check
print(value.contains("UK")) // true
print(value.contains("THIS")) // false
if value.contains("UK") == true
{
print("Hi")
}
else
{
// do your stuff
}

RxSwift - make one UI element hidden/not hidden according to other element

Im using RxSwift and RxCocoa in my project.
I have some UITextField named "lastNameTF", and there is a UILabel name "lastNameTitle".
I wanna know if there is any way to set the isHidden value of lastNameTitle always be equal to isHidden value of lastNameTF using RxSwift.
I believe you can use KVO as described here -
https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md#kvo
It is super easy to use KVO. Here is an example of exactly what you are trying to do, just without using RxSwift (don't know what that is...)
Here is the gist of it
class ViewController: UIViewController {
private var lastNameTextFieldHiddenContext = 0
private var lastNameObservingView:UIView? = nil
#IBOutlet weak var lastNameLabel: UILabel!
#IBOutlet weak var lastNameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// add the observer
lastNameTextField.addObserver(
self,
forKeyPath: "hidden",
options: [.new],
context: &self.lastNameTextFieldHiddenContext
)
}
/// function will be called whenever an added observer is triggered
override func observeValue(
forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?
) {
// make sure it is our text field isHidden observer
if context == &self.lastNameTextFieldHiddenContext {
// get the new value that was set
if let newValue = change?[NSKeyValueChangeKey.newKey] as? Bool {
// do what needs to be done when the observer is triggered
self.lastNameLabel.isHidden = newValue
}
}
}
deinit {
// remove the observer
if let view = self.lastNameObservingView {
view.removeObserver(self, forKeyPath: "hidden")
self.lastNameObservingView = nil
}
}
#IBAction func showHideButtonAction(_ sender: Any) {
self.lastNameTextField.isHidden = !self.lastNameTextField.isHidden
}
}
If you still need a simple RxSwift approach please try this:
// Controls are visible by default (isHidden = false)
let isControlHidden = BehaviorRelay<Bool>(value: false)
override func viewDidLoad() {
super.viewDidLoad()
let isHiddenDriver = self.isControlHidden.asDriver()
isHiddenDriver
.drive(self.lastNameTitle.rx.isHidden)
.disposed(by: disposeBag)
isHiddenDriver
.drive(self.lastNameTF.rx.isHidden)
.disposed(by: disposeBag)
}
Since you need both control visibilities bound to each other, you can use a Subject or Relay to achieve that, in this case isControlHidden. So, if you want to show/hide the, you just emit a new signal:
#IBAction func hide(_ sender: Any) {
self.isControlHidden.accept(true)
}
#IBAction func show(_ sender: Any) {
self.isControlHidden.accept(false)
}

How to set up a NSComboBox using prepare for segue or init?

I am really struggling to pass the contents of one array from a view controller to another to set up the contents of a nscombobox. I have tried everything I can think of, prepare for segue, init; but nothing seems to work.
the program flow is as follows: the user enter a number into a text field and based on it an array with the size of the number is created. Once the user presses a button the next VC appears that has a combo box and inside that combo box those numbers need to appear. All my attempts result in an empty array being passed. Could someone please take a bit of time and help me out. Im sure I'm doing a silly mistake but cannot figure out what.
Code listing below:
Class that take the user input. At this stage I'm trying to pass the contents of the array in the next class as I gave up on prepare for segue because that one crashes because of nil error. Please note that prepare for segue is uncommented in the code listing just for formatting purposes here. Im my program it is commented out as I am using perform segue at the moment.
Any solution would be nice please. Thank you.
import Cocoa
class SetNumberOfFloorsVC: NSViewController {
//MARK: - Properties
#IBOutlet internal weak var declaredNumber: NSTextField!
internal var declaredFloorsArray = [String]()
private var floorValue: Int {
get {
return Int(declaredNumber.stringValue)!
}
}
//MARK: - Actions
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction private func setNumberOfFloors(_ sender: NSButton) {
if declaredNumber.stringValue.isEmpty {
let screenAlert = NSAlert.init()
screenAlert.messageText = "Please specify the number of floors!"
screenAlert.addButton(withTitle: "Got it!")
screenAlert.runModal()
} else if floorValue == 0 || floorValue < 0 {
let screenAlert = NSAlert.init()
screenAlert.messageText = "Please input a correct number of floors!"
screenAlert.addButton(withTitle: "Got it!")
screenAlert.runModal()
} else {
for i in 0...floorValue - 1 {
declaredFloorsArray.append(String(i))
}
print("\(declaredFloorsArray)")
let declareNumberOfRoomsVC = SetNumberOfRoomsForFloorVC(boxData: declaredFloorsArray)
declareNumberOfRoomsVC.boxData = declaredFloorsArray
performSegue(withIdentifier: "set number of rooms", sender: self)
}
}
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if segue.identifier == "set number of rooms" {
if let addRoomsVC = segue.destinationController as? SetNumberOfRoomsForFloorVC {
addRoomsVC.floorBox.addItems(withObjectValues: declaredFloorsArray)
}
}
}
}
this is the class for the next VC with the combo box:
import Cocoa
class SetNumberOfRoomsForFloorVC: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {
//MARK: - Properties
#IBOutlet internal weak var floorBox: NSComboBox!
#IBOutlet private weak var numberOfRoomsTxtField: NSTextField!
internal var boxData = [String]()
//MARK: - Init
convenience init(boxData: [String]) {
self.init()
self.boxData = boxData
}
//MARK: - Actions
override func viewDidLoad() {
super.viewDidLoad()
floorBox.usesDataSource = true
floorBox.dataSource = self
floorBox.delegate = self
print("\(boxData)")
}
#IBAction private func setRoomsForFloor(_ sender: NSButton) {
}
//MARK: - Delegates
func numberOfItems(in comboBox: NSComboBox) -> Int {
return boxData.count
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
return boxData[index]
}
}
First you should remove the following code.
let declareNumberOfRoomsVC = SetNumberOfRoomsForFloorVC(boxData: declaredFloorsArray)
declareNumberOfRoomsVC.boxData = declaredFloorsArray
I assume you think that the viewController you created here is passed to prepareForSegue. However the storyboard instantiates a new viewController for you.
After that you need to set your declaredFloorsArray as the the boxData of the new viewController in prepareForSegue and you should be good to go.

Swift function textfield got focus OSX

Currently I am having multiple textfields in a view. If the user taps at one of them there should be a function responding to the event. Is there a way on how to do react (if a textfield got the focus)? I tried it with the NSTextFieldDelegate method but there is no appropriate function for this event.
This is how my code looks at the moment:
class ViewController: NSViewController, NSTextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let textField = NSTextField(frame: CGRectMake(10, 10, 37, 17))
textField.stringValue = "Label"
textField.bordered = false
textField.backgroundColor = NSColor.controlColor()
view.addSubview(textField)
textField.delegate = self
let textField2 = NSTextField(frame: CGRectMake(30, 30, 37, 17))
textField2.stringValue = "Label"
textField2.bordered = false
textField2.backgroundColor = NSColor.controlColor()
view.addSubview(textField2)
textField2.delegate = self
}
func control(control: NSControl, textShouldBeginEditing fieldEditor: NSText) -> Bool {
print("working") // this only works if the user enters a charakter
return true
}
}
The textShouldBeginEditing function only handles the event if the user tries to enter a character but this isn't what I want. It has to handle the event if he clicks on the textfield.
Any ideas, thanks a lot?
Edit
func myAction(sender: NSView)
{
print("aktuell: \(sender)")
currentObject = sender
}
This is the function I want to call.
1) Create a subclass of NSTextField.
import Cocoa
class MyTextField: NSTextField {
override func mouseDown(theEvent:NSEvent) {
let viewController:ViewController = ViewController()
viewController.textFieldClicked()
}
}
2) With Interface building, select the text field you want to have a focus on. Navigate to Custom Class on the right pane. Then set the class of the text field to the one you have just created.
3) The following is an example for ViewController.
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func textFieldClicked() -> Void {
print("You've clicked on me!")
}
}
4) Adding text fields programmatically...
import Cocoa
class ViewController: NSViewController {
let myField:MyTextField = MyTextField()
override func viewDidLoad() {
super.viewDidLoad()
//let myField:MyTextField = MyTextField()
myField.setFrameOrigin(NSMakePoint(20,70))
myField.setFrameSize(NSMakeSize(120,22))
let textField:NSTextField = NSTextField()
textField.setFrameOrigin(NSMakePoint(20,40))
textField.setFrameSize(NSMakeSize(120,22))
self.view.addSubview(myField)
self.view.addSubview(textField)
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func textFieldClicked() -> Void {
print("You've clicked on me!")
}
}
I know it’s been answered some while ago but I did eventually find this solution for macOS in Swift 3 (it doesn’t work for Swift 4 unfortunately) which notifies when a textfield is clicked inside (and for each key stroke).
Add this delegate to your class:-
NSTextFieldDelegate
In viewDidLoad() add these:-
imputTextField.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: Notification.Name.NSTextViewDidChangeSelection, object: nil)
Then add this function:-
func textDidChange(_ notification: Notification) {
print("Its come here textDidChange")
guard (notification.object as? NSTextView) != nil else { return }
let numberOfCharatersInTextfield: Int = textFieldCell.accessibilityNumberOfCharacters()
print("numberOfCharatersInTextfield = \(numberOfCharatersInTextfield)")
}
Hope this helps others.

How to know which NSCombobox selector calling the Delegate

I have the following code written in SWIFT for OS X App, the code is working fine (NSComboBox are select able only, not editable)
I have these two IBOutlet projNewProjType and projNewRouter, when I change the the selection of either of the NSComboBox, I can see the correct selected Index value and String value but how to check that the returned Index value is from projNewProjType NOT projNewRouter in the comboBoxSelectionDidChange()
import Cocoa
class NewProjectSetup: NSViewController, NSComboBoxDelegate {
let comboxProjValue: [String] = [“No”,”Yes”]
let comboxRouterValue: [String] = ["No","Yes"]
#IBOutlet weak var projNewProjType: NSComboBox!
#IBOutlet weak var projNewRouter: NSComboBox!
#IBAction func btnAddNewProject(sender: AnyObject) {
print(“Add New Button Pressed!”)
}
#IBAction func btnCancel(sender: AnyObject) {
self.dismissViewController(self)
}
override func viewDidLoad() {
super.viewDidLoad()
addComboxValue(comboxProjValue,projNewProjType)
addComboxValue(comboxRouterValue,projNewRouter)
self.projNewProjType.selectItemAtIndex(0)
self.projNewRouter.selectItemAtIndex(0)
self.projNewProjType.delegate = self
self.projNewRouter.delegate = self
}
func comboBoxSelectionDidChange(notification: NSNotification) {
let comboBox: NSComboBox = (notification.object as? NSComboBox)!
print("comboBox comboBox: \(comboBox)")
/* This printed “<NSComboBox: 0x6000001e1a00>”*/
print("comboBox objectValueOfSelectedItem: \(comboBox.objectValueOfSelectedItem)")
/* This printed the correct selected String value */
print("comboBox indexOfSelectedItem: \(comboBox.indexOfSelectedItem)")
/* This printed the correct selected Index value */
}
/* Add value to Combo box */
func addComboxValue(myVal:[String],myObj:AnyObject){
let myValno: Int = myVal.count
for i in 0..<myValno{
myObj.addItemWithObjectValue(myVal[i])
}
}
}
You already know the addresses of your two NSComboBox outlets and you know the address of which NSComboBox caused that notification to trigger, so why not do something like:
func comboBoxSelectionDidChange(notification: NSNotification) {
let comboBox: NSComboBox = (notification.object as? NSComboBox)!
if comboBox == self.projNewProjType
{
print("selection changed via self.projNewProjType")
}
if comboBox == self.projNewRouter
{
print("selection changed via self.projNewRouter")
}
You can set identifiers to your NSComboBoxes in IB. Select your combo box and choose identity inspector and name identifier. Then you are able to do like this:
if comboBox.identifier == "someIdentifier" {
// Do something
}