#IBOutlet weak var allStocksSelected: UIImageView!
#IBOutlet weak var shortStocksSelected: UIImageView!
#IBOutlet weak var longStocksSelected: UIImageView!
#IBOutlet weak var myStocksTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myStocksTableView.delegate = self
myStocksTableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
self.shortStocksSelected.isHidden = true
self.longStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
}
#IBAction func allStocksButtonPressed(_ sender: Any) {
print("ALL!")
self.stockTypeChanged(stockType: allStocksSelected)
}
#IBAction func shortStocksButtonPressed(_ sender: Any) {
print("SHORT!")
self.stockTypeChanged(stockType: shortStocksSelected)
}
#IBAction func longStocksButtonPressed(_ sender: Any) {
print("LONG!")
self.longStocksSelected.isHidden = false
self.shortStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
//stockTypeChanged(stockType: longStocksSelected)
}
#IBAction func addNewStockButtonPressed(_ sender: Any) {
}
#IBAction func signOutButtonPressed(_ sender: Any) {
}
private func stockTypeChanged(stockType: UIImageView) {
let stockTypes: [UIImageView] = [allStocksSelected, shortStocksSelected, longStocksSelected]
for (stock) in stockTypes {
if (stock == stockType) {
stock.isHidden = false
} else {
stock.isHidden = true
}
}
}
As shown from my code above, I am basically just trying to hide and show certain image views when buttons are pressed.
I am 100% sure that all of the buttons actions and IB outlets are properly connected, yet the image views are still not hiding and showing, and I cannot figure out why.
I was running into this same issue. Placing your isHidden code to execute on the main thread should solve the problem.
DispatchQueue.main.sync {
}
Related
I have stetted up switch in my First View Controller and assigned the following action to it :-
class ThirdViewController: UIViewController {
#IBOutlet weak var Image: UIImageView!
#IBOutlet weak var playerNum1Button: UIButton!
#IBOutlet weak var toggleSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
self.toggleSwitch.setOn(UserDefaults.standard.bool(forKey: "toggleState"), animated: true)
}
#IBAction func numPlayers1(_ sender: Any) {
performSegue(withIdentifier: "3to8segue", sender: self)
}
IBAction func toggleSwitch(_ sender: UISwitch) {
if (toggleSwitch.isOn == true) {
Image.image = UIImage(named: "Night")
}
else {
Image.image = UIImage(named: "background")
}
UserDefaults.standard.set(sender.isOn, forKey: "toggleState")
}
It is Able to change the image in the First View Controller. But, how can I get that switch to change the image in 2nd View controller as well when it is turned on? Thanks for the help!
Using Notification Center
Example:
In FirstViewController:
#IBAction func onPressButton(_ sender: UIButton) {
UserDefaults.standard.set(sender.isSelected, forKey: "pressedButton")
NotificationCenter.default.post(name: "changeImageBackground", object: nil)
}
In SecondViewController:
NotificationCenter.default.addObserver(target: self, selector: #selector(didChangeImageBackground), name: "changeImageBackground", object: nil)
#objc func didChangeImageBackground() {
let changed = UserDefaults.standard.bool(forKey: "pressedButton")
if changed {
// image when pressed
} else {
// image when haven't pressed
}
}
I have 2 UISwitch and a Button, in viewDidLoad, I set the button to be hidden and disabled, I want only my button to be not hidden if those 2 switch is in ON state, otherwise, I want my button to hide again. is there any method from UI Switch delegate that can be used ? how do I do that in Swift ?
here is the code I use
import UIKit
class AskingAuthorizationVC: UIViewController {
#IBOutlet weak var locationSwitch: DesignableSwitch!
#IBOutlet weak var notificationSwitch: DesignableSwitch!
#IBOutlet weak var nextButton: DesignableButton!
override func viewDidLoad() {
super.viewDidLoad()
// initial state
nextButton.isHidden = true
nextButton.isEnabled = false
notificationSwitch.isOn = false
locationSwitch.isOn = false
}
#IBAction func signUpButtonDidPressed(_ sender: Any) {
performSegue(withIdentifier: "toAuthenticationVC", sender: nil)
}
}
Hook both of the UISwitch-s as IBActions & IBOutlets
#IBAction func oweSwitch(_ sender: UISwitch) {
self.mybutton.isHidden = !(switch1.isOn && switch2.isOn)
}
I am trying to pass this UI Image View from one view controller to another on the same storyboard. I have already passed a UI TextField to a UI Label and UI Button. Now I need to do it with a UI Image View.
Here are my two view controllers.
import UIKit
class PhotoShareViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var contentTextView: UITextView!
#IBOutlet weak var thatTextField: UITextField!
#IBOutlet weak var thisTextField: UITextField!
var presenter: PhotoShareModuleInterface!
var image: UIImage!
#IBAction func thisUploadPhoto(_ sender: Any) {
if thisTextField.text != "" && thatTextField.text != ""
{
performSegue(withIdentifier: "segue", sender: nil)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var photoShareLabelViewController = segue.destination as! PhotoShareLabelViewController
photoShareLabelViewController.thisString = thisTextField.text!
photoShareLabelViewController.thatString = thatTextField.text!
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
imageView.image = image
}
override var prefersStatusBarHidden: Bool {
return true
}
#IBAction func didTapCancel(_ sender: AnyObject) {
presenter.cancel()
presenter.pop()
}
/* #IBAction func didTapDone(_ sender: AnyObject) {
guard let message = thatTextField.text, !message.isEmpty else {
return
}
guard let messageOne = thisTextField.text, !messageOne.isEmpty else {
return
}
presenter.finish(with: image, content:message)
presenter.dismiss()
}
}
*/
}
extension PhotoShareViewController: PhotoShareViewInterface {
var controller: UIViewController? {
return self
}
}
if someone could tell me how to send the image from one view controller into another that would be awesome!
You didn't show PhotoShareLabelViewController, but presumably it has:
var thisString: String?
Add
var thisImage: UIImage?
And then pass it along the same way as thisString
photoShareLabelViewController.thisImage = imageView.image
And pick it up in the viewDidLoad of PhotoShareLabelViewController (which is probably where you pick up thisString). Something like:
self.imageView.image = self.thisImage
But, using whatever variable name you are using.
In short, just copy what you are doing for thisString, but use a UIImage as the type instead of String.
NOTE: You are not passing a UIImageView from one VC to another (that is impossible). You are passing the image from an imageview from one VC to another.
Right now i have a purple and red score. I would like to use the 1 and 2 buttons to add the score for the colors. I know how to do this by creating just a sets of buttons to the specific color but doing this again and again will create problems. So what I am trying to do is if purple is selected and if and only if 1 is pressed then the total score of purple will = 1. I guess what I am trying is perform a switch statement for the buttons.
Before purple/red button is pressed.
After purple/red button is pressed.
import UIKit
class ViewController: UIViewController {
var purpleScore = 0
var redScore = 0
#IBOutlet var plus1: UIButton!
#IBOutlet var plus2: UIButton!
#IBOutlet var addRed: UIButton!
#IBOutlet var addPurlple: UIButton!
#IBOutlet var totalScorePurple: UILabel!
#IBOutlet var totalScoreRed: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
plus1.isHidden = true
plus2.isHidden = true
}
#IBAction func enteringRed(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func enteringPurple(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func plus1(_ sender: Any) {
}
#IBAction func plus2(_ sender: Any) {
}
}
If I understand correctly as the comment above, this should work
Create 2 Bool for red and purple to see weather they are checked or not, this is easiest way, else you can just check the redButton color got changed (if it does) or .selected = true
var redChecked = false
var purpleChecked = false
#IBAction func enteringRed(_ sender: Any) {
redChecked = !redChecked
//change color etc..
}
#IBAction func enteringPurple(_ sender: Any) {
purpleChecked = !purpleChecked
//change color etc..
}
#IBAction func plus1(_ sender: Any) {
if redChecked {
redScore+=1
}
if purpleChecked {
purpleScore+=1
}
}
#IBAction func plus2(_ sender: Any) {
if redChecked {
redScore+=2
}
if purpleChecked {
purpleScore+=2
}
}
I'm currently developing a counter app which every time you press a button it will count up. I have already done this and works 100%, what I'm trying to do is make a another button and when you press it, it shows the current number on the console but it only prints out 0 because that's the default variable I assigned the value to.
My whole class:
var counterNumber = 0
#IBOutlet weak var counterLabel: UILabel!
func initCount(){
counterNumber = 0
}
func numberUp(){
self.counterNumber++;
counterLabel.text = "\(self.counterNumber)"
}
#IBAction func CountUp(sender: UIButton) {
numberUp()
}
#IBAction func RestartButton(sender: UIButton) {
initCount()
}
#IBAction func printButton(sender: UIButton) {
self.numberUp();
print(self.counterNumber)
}
override func viewDidLoad() {
super.viewDidLoad()
initCount()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Just call method numberUp in printButton: action.
var counterNumber = 0//this is a variable of class
#IBOutlet weak var counterLabel: UILabel!
#IBAction func printButton(sender: UIButton) {
self.numberUp();
print(self.counterNumber)
}
func numberUp(){
self.counterNumber++;
counterLabel.text = "\(self.counterNumber)"
}
Use 'self' keyword to call instance variables.
class ViewController: UIViewController {
// Properties
var counterNumber:Int = 0 // Make this variable Global to Class
// IBOutlets Properties
#IBOutlet weak var counterLabel: UILabel!
#IBAction func printButton(sender: UIButton) {
self.numberUp()
self.counterLabel.text = "\(self.counterNumber)"
print("\n You Pressed ==> \(sender.title) button \(self.counterNumber) times")
}
func numberUp() {
self.counterNumber += 1
self.counterLabel.text = "\(counterNumber)"
}
}