I have a label that displays a random emoji, and then I have 4 UIButtons. Each one will display a random emoji, and one of the 4 will display the same emoji as the one on the label. Its a matching game.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var faceLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
var randomEmojiNum: UInt32 = arc4random_uniform(6) + 1
override func viewDidLoad() {
super.viewDidLoad()
faceLabel.text = genRanNum()
}
func genRanNum() -> String{
switch randomEmojiNum{
case 1: return "😀"
case 2: return "😅"
case 3: return "😉"
case 4: return "😁"
case 5: return "😂"
case 6: return "😄"
default: break
}
return "Default"
}
func correctAnswerGen() {
var correct: UInt32 = arc4random_uniform(4)
switch correct{
case 0: topLeftAnswer.titleLabel?.text = genRanNum()
case 1: topRightAnswer.titleLabel?.text = genRanNum()
case 2: bottomRightAnswer.titleLabel?.text = genRanNum()
case 3: bottomLeftAnswer.titleLabel?.text = genRanNum()
default: break
}
}
#IBOutlet weak var topLeftAnswer: UIButton!
#IBOutlet weak var topRightAnswer: UIButton!
#IBOutlet weak var bottomLeftAnswer: UIButton!
#IBOutlet weak var bottomRightAnswer: UIButton!
}
But every time I run it, it doesn't go as expected. Right now I'm assigning randomly one of the 4 UIButton outlets to be set to the same emoji as the one on the label, however none of the outlets set!
Whenever I run it looks like it is being selected. None of the buttons ever get set! I have a hunch it has something to do with the outlets being set and me editing the value after they are set. However, if i knew, i wouldn't be asking for help! Haha cheers!
Instead of topLeftAnswer.titleLabel?.text just use:
topLeftAnswer.setTitle("emoji", forState: UIControlState.Normal)
This is how I accomplished this:
class ViewController: UIViewController {
#IBOutlet weak var faceLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var topLeftAnswer: UIButton!
#IBOutlet weak var topRightAnswer: UIButton!
#IBOutlet weak var bottomLeftAnswer: UIButton!
#IBOutlet weak var bottomRightAnswer: UIButton!
var randomEmojiNum: UInt32 = arc4random_uniform(6) + 1
override func viewDidLoad() {
super.viewDidLoad()
faceLabel.text = genRanNum()
correctAnswerGen()
}
func genRanNum() -> String{
switch randomEmojiNum{
case 1: return "😀"
case 2: return "😅"
case 3: return "😉"
case 4: return "😁"
case 5: return "😂"
case 6: return "😄"
default: break
}
return "Default"
}
func correctAnswerGen() {
var correct: UInt32 = arc4random_uniform(4)
switch correct{
case 0: topLeftAnswer.setTitle(genRanNum(), forState: UIControlState.Normal)
case 1: topRightAnswer.setTitle(genRanNum(), forState: UIControlState.Normal)
case 2: bottomLeftAnswer.setTitle(genRanNum(), forState: UIControlState.Normal)
case 3: bottomRightAnswer.setTitle(genRanNum(), forState: UIControlState.Normal)
default: break
}
}
}
Update : Edited According to OP requirement.
Related
I have UISegmentedControl and I use it to switch between 2 UI views but I have problem with my views make the app to chatty with web APIs.each view will call its API to bring data for each view. I want to make the app parent view the load each view without user alpha of child
import UIKit
class UsersGroupsViewController: UIViewController {
#IBOutlet weak var usersView:UIView!
#IBOutlet weak var groupView:UIView!
#IBOutlet weak var segmentedControlViews: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let font = UIFont.systemFont(ofSize: 15)
segmentedControlViews.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
}
#IBAction func switchViews(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
usersView.alpha = 1
groupView.alpha = 0
} else {
usersView.alpha = 0
groupView.alpha = 1
}
}
}
instead of using alpha use isHidden
#IBOutlet weak var segmentedControlViews: UISegmentedControl!
#IBOutlet weak var usersView: UIView!
#IBOutlet weak var groupView: UIView!
#IBAction func indexChanged(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
usersView.isHidden = true
groupView.isHidden = false
case 1:
usersView.isHidden = false
groupView.isHidden = true
default:
break
}
I'm trying to get the int values from my labels and pass them to another label and add them together. In the code below, kicklabel, handballlabel, marklabel, etc., all need to add up and equal into fantasylabel. Based on how this code is set up, I'm not sure how to perform that.
I want to be able to add more lines of labels also, so that any amount of the array of labels add to a fantasy label.
Here's the code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var kickLabel1: UILabel!
#IBOutlet weak var handballLabel1: UILabel!
#IBOutlet weak var markLabel1: UILabel!
#IBOutlet weak var tackleLabel1: UILabel!
#IBOutlet weak var goalLabel1: UILabel!
#IBOutlet weak var pointLabel1: UILabel!
#IBOutlet weak var freeForLabel1: UILabel!
#IBOutlet weak var freeAgainstLabel1: UILabel!
#IBOutlet weak var fantasyLabel: UILabel!
var counters: [UILabel: Int] = [:]
override func viewDidLoad() {
super.viewDidLoad()
for label: UILabel in [kickLabel1,handballLabel1,markLabel1,tackleLabel1,goalLabel1,pointLabel1, freeForLabel1, freeAgainstLabel1, fantasyLabel] {
counters[label] = 0
for direction: UISwipeGestureRecognizerDirection in [.up, .down, .left, .right] {
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(didSwipe(_:)))
swipeGesture.direction = direction
label.addGestureRecognizer(swipeGesture)
label.isUserInteractionEnabled = true
label.isMultipleTouchEnabled = true
}
}
}
#objc func didSwipe(_ gestureRecognizer: UISwipeGestureRecognizer) {
guard let label = gestureRecognizer.view as? UILabel else { return }
debugPrint("\(gestureRecognizer.direction)")
switch gestureRecognizer.direction {
case .up:
counters[label] = counters[label]! + 10
print(counters)
case .down:
counters[label] = 0
print(counters)
case .left:
counters[label] = counters[label]! - 1
print(counters)
case .right:
counters[label] = counters[label]! + 1
print(counters)
default:
counters[label] = 0
}
label.text = "\(counters[label]!)"
}
}
At the end of your didSwipe you need to sum all of the Int values stored in your counters dictionary. Then update your fantasy label with that sum.
let sum = counters.reduce(0) { $0 + $1.value }
fantasyLabel.text = "\(sum)"
And you probably don't want a gesture on the fantasy label since that value is read-only based on the sum of the other data.
I would also get rid of the individual label outlets except for the fantasy label. Use an IBOutletCollect to store an array of UILabel.
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
}
}
}
I have a scene in my app that I want to do basic math between two slider bars and a segmented control that the user picks. I am trying to do the math between variables under the segmented controls, but Xcode is giving me a (!) stating
Use of unresolved identifier "xxxx"
with whatever variable I'm trying to add.
My code is:
import UIKit
class DopamineCalculator: UIViewController {
//slider outlets
#IBOutlet weak var slider: UISlider!
#IBOutlet weak var sliderone: UISlider!
//segmentoutlet
#IBOutlet weak var segmentoutlet: UISegmentedControl!
//LABELS
//weight label
#IBOutlet weak var weightlabel: UILabel!
//dosage label
#IBOutlet weak var dosagemg: UILabel!
//Drip Rate Answer Label
#IBOutlet weak var dripanswer: UILabel!
//ACTIONS!!
//weight slider action
#IBAction func weightslider(sender: UISlider) {
let weight = Int(sender.value)
//converts to kgs
let kgs = Int(sender.value) / Int(2.2)
weightlabel.text = "\(weight) lbs"
}
//Dosage Desired dosage slider
#IBAction func dosagedesired(sender: UISlider) {
let dosage = Int(sender.value)
dosagemg.text = "\(dosage) mg"
}
//CONCENTRATION OPTIONS
#IBAction func concentrationoption(sender: UISegmentedControl) {
switch segmentoutlet.selectedSegmentIndex
{
case 0:
dripanswer.text = (kgs) * (dosage) / 800
case 1:
dripanswer.text = "1600";
case 2:
dripanswer.text = "3200";
default:
break;
}
}
What I am trying to do is kgs x dosage / 800 respectively for each case. I can not find an adequate solution online to the unresolved identifier issue.
#IBOutlet ...
// Add variables to be reachable for all methods
var dosage : Int = 0
var kgs : Double = 0.0
var divisor : Double = 800.0
#IBAction func dosagedesired(sender: UISlider) {
// prepend self to variable as it is used
self.dosage = Int(sender.value)
dosagemg.text = "\(self.dosage) mg"
// adding the calculation here
let result = self.kgs * Double(self.dosage) / self.divisor
dripanswer.text = "\(result)"
}
#IBAction func weightslider(sender: UISlider) {
let weight = Int(sender.value)
// converts to kgs
// the same here
self.kgs = Int(sender.value) / Int(2.2)
// adding the calculation here
let result = self.kgs * Double(self.dosage) / self.divisor
dripanswer.text = "\(result)"
}
#IBAction func concentrationoption(sender: UISegmentedControl) {
switch segmentoutlet.selectedSegmentIndex
{
case 0:
self.divisor = 800.0
case 1:
self.divisor = 1600.0
case 2:
self.divisor = 3200.0
default:
self.divisor = 800.0
}
// finally the calculation
let result = self.kgs * Double(self.dosage) / self.divisor
dripanswer.text = "\(result)"
}
You should declare the variables kgs and dosage outside of any method i.e. at the class level, along with your outlets
//slider outlets
#IBOutlet weak var slider: UISlider!
#IBOutlet weak var sliderone: UISlider!
//segmentoutlet
#IBOutlet weak var segmentoutlet: UISegmentedControl!
//LABELS
//weight label
#IBOutlet weak var weightlabel: UILabel!
//dosage label
#IBOutlet weak var dosagemg: UILabel!
//Drip Rate Answer Label
#IBOutlet weak var dripanswer: UILabel!
// You should declare the variables here:
var kgs = 0
var dosage = 0
And when you use kgs and dosage inside your methods, remove the word let because you're not declaring a variable.
The reason you do this is that you can't access variables declared in a method in another method. In the concentrationoption method, you can't access dosage, which is defined in dosagedesired method.
I'm working on a project to generate random images from an array. I'm running into an issue. I will have one button on the screen name generage shapes. Once the user clicks on this button the generate shapes button becomes hidden and 4 buttons display 4 different shapes. I'm having an issue making these 4 buttons display with the image from my array. I tried a switch statement but it doesn't go through. I'm providing my code below.
import UIKit
class ShapesDetailViewController: UIViewController {
var random = arc4random_uniform(4)
#IBOutlet weak var titleBar: UINavigationItem!
#IBOutlet weak var displayMessageLabel: UILabel!
var myImages = [ "diamond.png", "decagon.png", "dodecagon.png", "hectagon.png", "heptagon.png", "octagon.png", "parallelogram.png", "pentagon.png", "rectangle.png", "rightTriangle.png", "square.png", "trapezoid.png", "triangle.png"]
//my image labels
#IBOutlet weak var buttonOne: UIButton!
#IBOutlet weak var buttonTwo: UIButton!
#IBOutlet weak var buttonThree: UIButton!
#IBOutlet weak var buttonFour: UIButton!
#IBOutlet weak var generateImage: UIButton!
override func viewDidLoad() {
self.buttonFour.hidden = true
self.buttonOne.hidden = true
self.buttonThree.hidden = true
self.buttonTwo.hidden = true
displayMessageLabel.hidden = true
}
#IBAction func generateRandomImages(sender: AnyObject) {
displayMessageLabel.hidden = false
buttonOne.hidden = false
switch(random){
case 0: buttonOne.imageView?.image = UIImage(named: "circle.png")
break
case 1: buttonOne.imageView?.image = UIImage(named: "decagon.png")
break
case 2: buttonOne.imageView!.image = UIImage(named: "diamond.png")
break
case 3: buttonOne.imageView!.image = UIImage(named: "hectagon.png")
break
default:
break;
}
generateImage.hidden = true
}
}
I think the problem is that you can't change a button's image by its image view property, you need to use the setter method for the image. Something like this:
buttonOne.setImage(UIImage(named: "circle"), forState: UIControlState.Normal)