If (Bools = true) not executing -Swift - swift

Attempting to execute a modal segue when multiple different Bool variables are all true (activated true through IBAction button push), however, nothing is happening- here is how they are all setup-
UIViewController {
// INITIAL TO CHECK WHICH BUTTONS HAVE BEEN PUSHED //
var 1Check = Bool()
// Checks //
#IBAction func 1(_ sender: AnyObject) {
1Check = true
}
and here is the execution-
viewDidLoad() {
super.viewDidLoad()
MoveOn()
}
func MoveOn(){
if (1Check == true && 2Check == true ...) {
self.performSegue(withIdentifier: "NewScreen", sender: nil)
}
}
what am I missing? Thanks!

The call to MoveOn() needs to be in a place where it will be called every time one of those checked values changes:
UIViewController {
// INITIAL TO CHECK WHICH BUTTONS HAVE BEEN PUSHED //
var 1Check = Bool()
// Checks //
#IBAction func 1(_ sender: AnyObject) {
1Check = true
MoveOn()
}
viewDidLoad() {
super.viewDidLoad()
}
func MoveOn(){
if (1Check == true && 2Check == true ...) {
self.performSegue(withIdentifier: "NewScreen", sender: nil)
}
}
}

Related

How to change variable value declared in a class from another class?

I have a 2 viewcontrollers, in vC1 a variable isShowListAsked : Bool = false is declared , on clicking map button it goes to vC2 . In vC2 there is a button named List.
I want that :
after clicking List button it go back to vC1 and vC1 variable value should changed to true. But it is still remains False.
Please help me .
Thanks in advance.
On clicking List button i am able to go back to vC1 but not able to set isShowListAsked = true . i tried get{} set {}.
In vC1 :
class vC1 : UIViewController
{
var isShowListAsked : Bool = false
var IsShowListAsked : Bool {
get {
return isShowListAsked
}
set{
isShowListAsked = newValue
}
}
}
Then after clicking In vC2 :
class vC2 : UIViewController
{
var vc = vC1()
#IBAction func mapListSegmentTapped(_ sender: Any) {
if mapListSegment.selectedSegmentIndex == 1
{
vc.IsShowListAsked = true
}
if mapListSegment.selectedSegmentIndex == 0
{
vc.IsShowListAsked = false
}
}
}
After going back i am checking variable value in viewWillappear()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print(" isshowListAsked = \(IsShowListAsked) ") // print false
}
Expected Result :
print(" isshowListAsked = \(IsShowListAsked) ") // print True
Actual Result :
print(" isshowListAsked = \(IsShowListAsked) ") // print false
Use Closures to solve your problem statement.
Create a closure in VC2 of type ((Bool)->())? and call it with the relevant true/false values in mapListSegmentTapped(_:) method, i.e.
class VC2: UIViewController {
var handler: ((Bool)->())?
#IBAction func mapListSegmentTapped(_ sender: Any) {
if mapListSegment.selectedSegmentIndex == 1 {
self.handler?(true)
} else if mapListSegment.selectedSegmentIndex == 0 {
self.handler?(false)
}
}
}
Now, set this closure when you're presenting the instance of VC2 from VC1 i.e.
class VC1 : UIViewController {
var isShowListAsked: Bool = false
func onTapMapButton(_ sender: UIButton) {
if let controller = self.storyboard?.instantiateViewController(withIdentifier: "VC2") as? VC2 {
controller.handler = {[weak self] (value) in
self?.isShowListAsked = value
print(self?.isShowListAsked)
}
self.present(controller, animated: true, completion: nil)
}
}
}

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

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

How to refer #IBAction Func to several button outlets

I have lots of buttons that when pressed run very similar code, im currently writing a function for each, is there a way to compact this to one function?
Heres some of the code:
#IBAction func b0(_ sender: UIButton) {
if pressedArray[0] && buttonsCanBePressed {
pressedArray[0] = false
b0.backgroundColor = notPressedColour
} else if buttonsCanBePressed {
b0.backgroundColor = pressedColour
pressedArray[0] = true
}
}
#IBAction func b1(_ sender: UIButton) {
if pressedArray[1] && buttonsCanBePressed {
pressedArray[1] = false
b1.backgroundColor = notPressedColour
} else if buttonsCanBePressed {
b1.backgroundColor = pressedColour
pressedArray[1] = true
}
}
#IBAction func b2(_ sender: UIButton) {
if pressedArray[2] && buttonsCanBePressed {
pressedArray[2] = false
b2.backgroundColor = notPressedColour
} else if buttonsCanBePressed {
b2.backgroundColor = pressedColour
pressedArray[2] = true
}
}
Yes, You can make only one IBAction function, and then drag the circle in the line number to all the buttons you want, and you can differentiate the buttons using sender.tag

Swift Segmented Control

I made a segmented control in swift that changes a boolean to either true or false; However, every time I select "selectedSegmentedIndex == 1" in the application, I get error "Thread 1: signal SIGABERT"
My code goes as flows:
#IBOutlet weak var translationType: UISegmentedControl!
var state = true
#IBAction func translation(_ sender: Any)
{
if translationType.selectedSegmentIndex == 0
{
state = ture
}
else if translationType.selectedSegmentIndex == 1
{
state = false
}
}
Any information would be greatly appreciated. Thanks.
At least using the sender parameter and the static type avoids the crash if translationType is not connected – which is most likely the case.
#IBAction func translation(_ sender: UISegmentedControl)
{
if sender.selectedSegmentIndex == 0
{
state = true
}
else if sender.selectedSegmentIndex == 1
{
state = false
}
}
or a bit shorter
#IBAction func translation(_ sender: UISegmentedControl)
{
state = sender.selectedSegmentIndex == 0
}
var state = true
#IBOutlet weak var translationType: UISegmentedControl!
#IBAction func translation(_ sender: UISegmentedControl)
{
if translationType.selectedSegmentIndex == 0
{
state = true
}
else if translationType.selectedSegmentIndex == 1
{
state = false
}
print(state)
print(translationType.selectedSegmentIndex)
}
make sure your outlet is connected! see connection inspector

How can I remove repetitive code in swift?

I have 2 sections and 6 lines of code that do the opposite things for an app that I have made for a project. The code was typed as it was taught to us, but in the project review I was told to get rid of the repetitive/like code. Can anyone help point me in the right direction with this? Thanks
#IBAction func stopRecording(_ sender: AnyObject) {
recordButton.isEnabled = true
stopRecordingButton.isEnabled = false
recordingLabel.text = "Tap to Record"
}
#IBAction func recordAudio(_ sender: AnyObject)
{
recordingLabel.text = "Recording in Progress"
stopRecordingButton.isEnabled = true
recordButton.isEnabled = false
}
You can also do this by didSet Observer.
var isRecording: Bool = false {
didSet {
recordButton.isEnabled = !isRecording
stopRecordingButton.isEnabled = isRecording
recordingLabel.text = isRecording ? "Recording in progress" : "Tap to Record"
}
}
And the actions become pretty simple like this.
#IBAction func stopRecording(_ sender: AnyObject) {
isRecording = false
}
#IBAction func recordAudio(_ sender: AnyObject) {
isRecording = true
}