Tuesday, June 28, 2022
HomeiOS Developmentios - For loop error with button creation in swift

ios – For loop error with button creation in swift


I’m making a reminiscence sport in swift the place the person can choose the quantity of playing cards which are going to be accessible on the display screen. This choice is made on the menu view controller and the information is handed to the sport view controller. I exploit a for loop to create the buttons that symbolize the playing cards of the sport and add every button to an array.

However, when initiating the sport, totally different numbers of playing cards seem incorrectly relying on the quantity of playing cards that the participant selected (e.g., if 8 playing cards are chosen, 8 playing cards seem. However when 10 playing cards are chosen, 9 playing cards seem).

Button creation perform:

func createCardButtons(){
    
    let initialPos = Int(screenWidth/50)
    var xPos = Int(screenWidth/20)
    var yPos = Int(screenHeight/20)
    let dimension = Int(screenWidth/5)
    let cardBack = #imageLiteral(resourceName: "card_back")

    for _ in 0...cardsNum! {
        
        if xPos < Int(screenWidth - dimension) {
            
            let button = UIButton(body: CGRect(x: xPos, y: yPos, width: dimension, top: dimension))
            button.addTarget(self, motion: #selector(cardPressed), for: .touchUpInside)
            button.setImage(cardBack, for: .regular)
            view.addSubview(button)
            
            buttonsArr.append(button)
            
            xPos += dimension + initialPos
        } else {
            xPos = Int(screenWidth/20)
            yPos += dimension + initialPos
        }
    }
    
    print(buttonsArr)
}

Print message for 8 playing cards :

[<UIButton: 0x157509950; frame = (18 40; 75 75); opaque = NO; layer = <CALayer: 0x6000029dba60>>, 
 <UIButton: 0x157509c10; frame = (100 40; 75 75); opaque = NO; layer = <CALayer: 0x6000029dae60>>, 
 <UIButton: 0x157509ed0; frame = (182 40; 75 75); opaque = NO; layer = <CALayer: 0x6000029db960>>, 
 <UIButton: 0x15750a190; frame = (264 40; 75 75); opaque = NO; layer = <CALayer: 0x6000029dacc0>>, 
 <UIButton: 0x15750a450; frame = (18 122; 75 75); opaque = NO; layer = <CALayer: 0x6000029dbc20>>, 
 <UIButton: 0x15750a710; frame = (100 122; 75 75); opaque = NO; layer = <CALayer: 0x6000029daca0>>, 
 <UIButton: 0x15750a9d0; frame = (182 122; 75 75); opaque = NO; layer = <CALayer: 0x6000029dbb00>>, 
 <UIButton: 0x15750ac90; frame = (264 122; 75 75); opaque = NO; layer = <CALayer: 0x6000029dbcc0>>]

Print message for 10 playing cards:

[<UIButton: 0x15851ba30; frame = (18 40; 75 75); opaque = NO; layer = <CALayer: 0x600002057bc0>>, 
 <UIButton: 0x15851bcf0; frame = (100 40; 75 75); opaque = NO; layer = <CALayer: 0x600002043300>>, 
 <UIButton: 0x15851bfb0; frame = (182 40; 75 75); opaque = NO; layer = <CALayer: 0x60000204fba0>>, 
 <UIButton: 0x15851c270; frame = (264 40; 75 75); opaque = NO; layer = <CALayer: 0x60000204a340>>, 
 <UIButton: 0x15851c530; frame = (18 122; 75 75); opaque = NO; layer = <CALayer: 0x60000204a1a0>>, 
 <UIButton: 0x15851c7f0; frame = (100 122; 75 75); opaque = NO; layer = <CALayer: 0x60000206be80>>, 
 <UIButton: 0x15851cab0; frame = (182 122; 75 75); opaque = NO; layer = <CALayer: 0x600002061840>>, 
 <UIButton: 0x15851cd70; frame = (264 122; 75 75); opaque = NO; layer = <CALayer: 0x600002063a40>>, 
 <UIButton: 0x15851d030; frame = (18 204; 75 75); opaque = NO; layer = <CALayer: 0x600002063420>>]

If cardsNum = 8 provides 8 objects to the array, why cardsNum = 10 provides solely 9?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments