UIActivityIndicator shows for a split second - ios

I am trying to show an spinning activity indicator for my app when the user taps on a button before they are presented with the next view - it kind of works, I see it for a split second
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewWillAppear(animated: Bool) {
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}

Refactor your code. You don't need to stop the activity indicator in viewWillAppear. You can hide it in viewDidLoad. Here's the code:
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
myActivityIndicator.hidden = true
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Make sure that the IBOutlet for the activity indicator and IBAction for getting events are correctly connected in Interface Builder.

I changed it and it doesn't work, until I go back in the navigation
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}

viewDidAppear is probably a more appropriate place to stop the animation and hide the spinner.

Related

Hierarchy Problem with presenting Viewcontrollers, can someone help me?

[![enter image description here][2]][2]i want to programm an App with two ViewControllers
The first one performs a segue to the second VC.
The Second VC has a Scrollview with different Views.
I did everything with Autolayout not with code
When i run the Application i get always the error "whose view is not in the window hierarchy!" and can't scroll to the bottom. I tried to find some solutions but nothing helped.
Hope someone can help me with the problem.
my first VC runs a normal performSegue("identifier",sender )
my code for the second VC is
import UIKit
class FragenViewController: UIViewController {
var fragen = [Question]()
var highscore = 0
var questionNumber = 0
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var answerButton1: UIButton!
#IBOutlet weak var answerButton2: UIButton!
#IBOutlet weak var answerButton3: UIButton!
#IBOutlet weak var answerButton4: UIButton!
#IBOutlet weak var forwardButton: UIButton!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var currentQuestionLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func answerButton_Tapped(_ sender: UIButton) {
}
}
the rest is with Main Storyboard Drag and drop
The Code from the first VC is
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var highscoreLabel: UILabel!
#IBOutlet weak var startButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Setup Layout
startButton.backgroundColor = UIColor.systemFill
startButton.layer.cornerRadius = 20
startButton.titleLabel?.font = UIFont(name: "Arial", size: 20)
startButton.layer.borderWidth = 2
startButton.tintColor = UIColor.systemBlue
highscoreLabel.font = UIFont(name: "Arial", size: 18)
}
#IBAction func start_Button(_ sender: Any) {
performSegue(withIdentifier: "startGame", sender: sender)
}
}

Not woking UIView ishidden after change UILabel text value in swift

I want to change uilabel value and show uiview that has hide like a popup windows.
When I touch a button, that code print "setPopupView 0".
but doesn't show settingView.
and I touch a button again.
print "setPopupView 0" and show settingView.
so When "settingLabelValueUnit text" has changed not show settingView,
but when "settingLabelValueUnit text" has not changed show settingView.
(It means "settingLabelValue text" is same as input value)
I don't know why.
Anyone can help me?
Thank you
here is my swift code.
class ViewController: UIViewController {
#IBOutlet weak var workoutScrollView: UIScrollView!
#IBOutlet weak var settingView: UIView!
#IBOutlet weak var settingLabelValueUnit: UILabel!
#IBOutlet weak var imgSettingBGcal: UIImageView!
#IBOutlet weak var imgSettingBGdis: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.imgSettingBGcal.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(calSettingClick)))
self.imgSettingBGdis.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(disSettingClick)))
}
func calSettingClick(sender: UITapGestureRecognizer) {
setPopupView(0)
}
func disSettingClick(sender: UITapGestureRecognizer) {
setPopupView(1)
}
func showPopup (_ settype:Int) {
switch settype {
case 0:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set1"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
case 1:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set2"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
}
}
}

Why viewWithTag is returning nil

activite1Label as the tag 1
class StatsViewController: UIViewController {
#IBOutlet weak var activite1Label: UILabel!
#IBOutlet weak var activite2Label: UILabel!
#IBOutlet weak var activite3Label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
activite1Label.text = activite[0]
activite2Label.text = activite[1]
activite3Label.text = activite[2]
miseAjourTotal()
}
func miseAjourTotal() {
let leLabel = view.viewWithTag(1) as! UILabel
print("leLabel: \(leLabel.text)")
}
}
Nothing in your code tells that the label has the tag 1. You should go to your storyboard and check if the label does have tag 1 or set the tag programmatically

Add the value of multiple sliders in swift then average them?

Im doing this wrong I but I'm not sure how to get it I could really use some help. I have 3 sliders (I may need more later) and basically they are 1-10 in value kinda for a rating system and i want them averaged to create an average score.. so this is what I have
#IBOutlet weak var speed: UISlider!
#IBAction func speedChange(sender: AnyObject) {
let speedValue = speed.value
}
#IBOutlet weak var body: UISlider!
#IBAction func bodyChange(sender: AnyObject) {
let bodyValue = body.value
}
#IBOutlet weak var details: UISlider!
#IBAction func detailsChange(sender: AnyObject) {
let detailsValue = details.value
}
#IBOutlet weak var score: UILabel!
func final() {
var thescore = (speed.value + body.value + details.value) / 3
score.text = "\(thescore)"
}
Only problem is it doesn't work. the + errors out, so I need to fix that, but even still i think my methodology is completely off.
It worked for me. Are you sure the outlets in the storyboard are linked to your Controller ?
Also, you may want to call your function final every time a slider changed its value, like this :
#IBOutlet weak var sliderA: UISlider!
#IBAction func sliderAchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var sliderB: UISlider!
#IBAction func sliderBchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var sliderC: UISlider!
#IBAction func sliderCchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
fonction()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func fonction() {
var thescore = (sliderA.value + sliderB.value + sliderC.value ) / 3
label.text = "\(thescore)"
}​
And I added a call to the function in viewDidLoad method to initilaize the label's text.

Generate buttons randomly (Swift)

I am making an app where you click a button, causing a new button to appear, and the button you clicked to disappears. I want the buttons to appear randomly, and I also want it to always be 4 buttons visible all the time. I can't really figure out how to do this, thanks!
Here's an image of how the start screen looks like:
I've been trying to do this using UIView's hidden property, but it doesn't work so well.
Heres my code so far:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var blankButton1: UIButton!
#IBOutlet weak var blankButton2: UIButton!
#IBOutlet weak var blankButton3: UIButton!
#IBOutlet weak var blankButton4: UIButton!
#IBOutlet weak var blankButton5: UIButton!
#IBOutlet weak var blankButton6: UIButton!
#IBOutlet weak var blankButton7: UIButton!
#IBOutlet weak var blankButton8: UIButton!
#IBOutlet weak var blankButton9: UIButton!
#IBOutlet weak var bluebutton1: UIButton!
#IBOutlet weak var bluebutton2: UIButton!
#IBOutlet weak var bluebutton3: UIButton!
#IBOutlet weak var bluebutton4: UIButton!
#IBOutlet weak var bluebutton5: UIButton!
#IBOutlet weak var bluebutton6: UIButton!
#IBOutlet weak var bluebutton7: UIButton!
#IBOutlet weak var bluebutton8: UIButton!
#IBOutlet weak var bluebutton9: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
bluebutton1.hidden = true
bluebutton2.hidden = true
bluebutton3.hidden = true
bluebutton4.hidden = true
bluebutton6.hidden = true
}
#IBAction func FirstHidden(sender: AnyObject) {
bluebutton1.hidden = true
bluebutton2.hidden = false
}
#IBAction func SecondHidden(sender: AnyObject) {
bluebutton2.hidden = true
bluebutton9.hidden = false
}
#IBAction func ThirdHidden(sender: AnyObject) {
bluebutton3.hidden = true
bluebutton8.hidden = false
}
#IBAction func FourthHidden(sender: AnyObject) {
bluebutton4.hidden = true
bluebutton5.hidden = false
}
#IBAction func FiftHidden(sender: AnyObject) {
bluebutton5.hidden = true
bluebutton6.hidden = false
}
#IBAction func SixthHidden(sender: AnyObject) {
bluebutton6.hidden = true
bluebutton4.hidden = false
}
#IBAction func SeventhHidden(sender: AnyObject) {
bluebutton7.hidden = true
bluebutton5.hidden = false
}
#IBAction func EightHidden(sender: AnyObject) {
bluebutton8.hidden = true
bluebutton3.hidden = false
}
#IBAction func NinethHidden(sender: AnyObject) {
bluebutton9.hidden = true
bluebutton7.hidden = false
}
}
Start by putting all the buttons in an array (use IBOutletCollection). Now you can use your random number function to index into the array.
Initially, hide all the buttons in the array.
Take a mutable copy of the array and iterate for how many buttons you want to show. On each iteration generate a random index, show that button and remove it from the mutable copy.
Repeat...

Resources