Generate random image over a button - ios

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)

Related

Cannot use instance member 'userCard1' within property initializer; property initializers run before 'self' is available error [duplicate]

This question already has answers here:
How to initialize properties that depend on each other
(4 answers)
Closed 2 years ago.
Trying to make a simple Blackjack app to get comfortable with using Xcode. I've never coded in Swift before and have a little experience in C++. Having an error I don't know how to fix. I'm having trouble applying other answers to similar questions to my situation.
I'm not totally sure what's causing the problem, let alone how to fix it :)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
var balance = 500
let suits = ["h","d","c","s"]
//user card declarations
#IBOutlet weak var userCard1: UIImageView!
#IBOutlet weak var userCard2: UIImageView!
#IBOutlet weak var userCard3: UIImageView!
#IBOutlet weak var userCard4: UIImageView!
#IBOutlet weak var userCard5: UIImageView!
#IBOutlet weak var userCard6: UIImageView!
#IBOutlet weak var userCard7: UIImageView!
#IBOutlet weak var userCard8: UIImageView!
#IBOutlet weak var userCard9: UIImageView!
#IBOutlet weak var userCard10: UIImageView!
//dealer card declarations
#IBOutlet weak var dealerCard1: UIImageView!
#IBOutlet weak var dealerCard2: UIImageView!
#IBOutlet weak var dealerCard3: UIImageView!
#IBOutlet weak var dealerCard4: UIImageView!
#IBOutlet weak var dealerCard5: UIImageView!
var win = false
var bet = 0
//bet label
#IBOutlet weak var betLabel: UILabel!
//bet slider
#IBAction func betSlider(_ sender: UISlider) {
betLabel.text = String(Int(sender.value))
}
#IBOutlet weak var betSliderValue: UISlider!
var userCardSlot = 3
var dealerCardSlot = 3
var userTotal = 0
var dealerTotal = 0
//user cards displayed
var userCardArray = [userCard1, userCard2, userCard3, userCard4, userCard5, userCard6, userCard7, userCard8, userCard9, userCard10]
//dealer cards displayed
var dealerCardArray = [dealerCard1, dealerCard2, dealerCard3, dealerCard4, dealerCard5]
//hit button
#IBAction func hitTapped(_ sender: Any) {
var cardNum = Int.random(in: 2...14)
var cardSuit = Int(arc4random()) % 4
userCardArray[userCardSlot].image = UIImage(named: "\(cardSuit)card\(cardNum)")
}
//stand button
#IBAction func standTapped(_ sender: Any) {
for userTotal in userCardArray.reversed() {
userTotal += cardNum
}
for dealerTotal in dealerCardArray {
dealerTotal += cardNum
}
while dealerTotal < 17 {
dealerCardSlot += 1
dealerCardArray[dealerCardSlot].image = UIImage(named: "\(cardSuit)card\(cardNum)")
}
if dealerTotal <= userTotal {
win = true
} else {
win = false
}
}
#IBAction func newRoundTapped(_ sender: Any) {
bet = Int(betSliderValue.value)
userCardArray[0].image = UIImage(named: "\(cardSuit)card\(cardNum)")
userCardArray[1].image = UIImage(named: "\(cardSuit)card\(cardNum)")
dealerCardArray[0].image = UIImage(named: "\(cardSuit)card\(cardNum)")
dealerCardArray[0].image = UIImage(named: "Red_back.jpg")
//prompt for hit or stand
if win == true {
balance += (bet * 2)
} else {
balance -= bet
}
}
}
Any help's appreciated!
You generally cannot use one property to initialize another outside of init, because when you do:
var b = a + 1
the compiler implicitly does:
var b = self.a + 1
but self is not yet available; it's only available during the init.
The only exception is a lazy property initializer:
lazy var b: Int = self.a + 1
In your case, you can move the initialization into init:
var userCardArray: [UIImageView]
init() {
self.userCardArray = [userCard1, userCard2, ... ]
}

switch between views ios using UISegmentedControl without using alpha?

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
}

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

UIBottom Text Field not setting

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.

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