The process of switching an image tapped in the IF statement of SWIFT to another image.

Asked 2 years ago, Updated 2 years ago, 131 views

I want to switch to PAUSE image when I tap START image as attached image, but I can't.

追Additional
The build passes through the xcode, but when I tap the START image, it does not switch to the PAUSE image as shown in the image below.Even if you tap the START image, the color changes a little and it won't work.

The attached image is a design created with the Xcode storyboard.I write exactly the same as the textbook, but I don't know where the mistake is.I would appreciate it if you could let me know.

Enter a description of the image here

Code

import UIKit

classViewController:UIViewController {

    // startPause outlet to change the start to the pause image
    // Variable that moves this round start button
    @IBOutlet weak var startPauseOutlets:UIButton!

    // var to check the state of the startPauseButton to display the correct image
    var startPauseButtonStateCheck=true

   // @ IBAction func startPausePressed(_sender:AnyObject){
   @ IBAction func startPausePressed(_sender:Any){
       if startPauseButtonStateCheck{
           // In the teaching materials, it was indicated that it would be better to use ?, so ?, but it didn't work, so I started again with ! and it worked.But in the end, it moved, so I'll go through with ?.
           // Maybe the if statement below is strange, but both start and pause images do not appear.
           let pause = UIImage(named: "pauseButton") as UIImage?
           startPauseOutlets.setImage(pause, for:.normal)
           startPauseButtonStateCheck=false
       } else {
           let start = UIImage(named: "start") as UIImage?
           startPauseOutlets.setImage(start, for: .normal)
           startPauseButtonStateCheck=true
       }
    }
}

swift xcode swift4 optional

2022-09-30 19:26

2 Answers

In Xcode, is there a ○ or white circle to the left of @IBOutletweakvar startPauseOutlets:UIButton?

Open the interface design screen where start and pause are switched in Xcode, press the button on the upper right that looks like two ○s are connected, and set the user interface on the left screen and the source code on the right.

Then right-click ○ to the left of @IBOutletweakvar startPauseOutlets:UIButton and drag it straight to the start/pause button, then release your hand from the mouse button as the start/pause button changes color.

Then it will be a double circle with a black ● in の next to @IBOutletweakvar startPauseOutlets:UIButton.

Try running the program again in this state

The keyword @IBOutlet is only a declaration to refer to a user interface component, so which user interface component should I refer to?where

  • Specify the connection between the user interface part you want to reference from ○ next to the declaration by dragging it from the right click
  • Right-click the user interface part and drag it to the source code to specify what variable name the part should reference from the source code

If you do not do one of the , the value will not automatically enter after you have declared it.

The value of the variable startPauseOutlets is nil, so please check it out.

Finally, the most important information is that the value of the variable startPauseOutlets is nil, so rather than adding to the question what happened as a result of manipulating the application,
startPauseOutlets is more important for programming than nil during debugging, so I manipulated the application that I was able to ask, but
than
it only works in some way. What kind of debugging do you do and what seems to be the cause? How can you solve the cause?
If you ask me a question from this perspective, I think it will be easier to gather answers and give you more appropriate advice.


2022-09-30 19:26

I am very sorry for the late reply.It seems that there was a problem with how to capture the image.Once I deleted the image and pasted it again, it worked.


2022-09-30 19:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.