Trying to get me feet wet on my first app after watching 6 months of tutorials, ITS TIME!
I'm trying to setup a simple volleyball court with 6 players (buttons). I want to be able to control the button's images and have a simple array of 12 player images. I want to use the array for easy manipulation of rotating the floor lineups etc.
I'm getting stuck with trying to pass a UIImage array value to the button.setImage command. I realize I'm getting mixed up and confusing the UIImage with String etc. but not sure how to correct.
The above is not my exact code and may have bad syntax but its an example of what I'm trying. The button.setImage wants a different type than what I'm providing.
Everyone thank you for your help, I'm a old guy retired but very interested in SWIFT. I realize I'm showing my lack of SWIFT fundamentals here but as mentioned I've been binge watching tutorials too long and have to start at something.
Types are misaligned. The array contains instances of UIImage while the initializer for a UIImage.init(named:) expects a String. This is the reason you see that error message.
One other thing. For experimenting it's okay to use image literals, however for anything else you should rather use the real types.
Something like this should compile.
class YourViewController : UIViewController {
/* ... */
var vbPlayerName: [String] = ["Julia", "Alicia", "Alexandra"]
// The init of UIImage can fail, so it creates a an `Optional<UIImage>` or `UIImage?`
var vbPlayerPics: [UIImage?] = [
UIImage(named: "Image1"),
UIImage(named: "Image2"),
UIImage(named: "Image3")
]
override func viewDidLoad() {
super.viewDidLoad()
button_Pos4.setImage(vbPlayerPics[1], for: .normal)
}
/* ... */
}
I started from scratch just to get the operation correct:
import UIKit
class ViewController: UIViewController {
var vbPlayerName: [String] = ["Julia", "Alicia", "Alexandra"]
var vbPlayerImages: [UIImage?] = [
UIImage(named: "Image1"),
UIImage(named: "Image2"),
UIImage(named: "Image3"),
]
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
button.setImage(vbPlayerImages[0], for: .normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Thank you everyone for the help, the errors are gone but alas the images don't appear.
I tried Image1.jpg and Image1 with same results.
BTW, the 3 images Image1,Image2,Image3 are in assets..
It may require constrains/sizing etc... I'm on it! no errors is good.
You can just start the very first and easy step of debugging as you teach yourself all the basics.
Add something like this into your viewDidLoad method and see what it prints.
print(vbPlayerImages)
If all images are there then the images are correctly loaded from the asset catalog, otherwise there is something wrong with the names or the linked asset catalog. If they are loaded but do not appear then your image view has some issues. Use the view debugger in Xcode to inspect your view hierarchy.
PS: For better readability in the forums you can use Markdown to format your posts (especially for code samples). ;)
A debugging approach I would try is to put a UIImageView on the storyboard and make an outlet to the code. Then programmatically set the image property of that. This will help you determine if there is a problem with your images or how you are setting them in the buttons. As in computer repair, debugging is often about eliminating possibilities.
On a meta note, having a goal app is a great way to learn iOS development. Learning for its own sake, which I have done, can be demotivating.
To add on that, it can also be overwhelming to start with a whole App when learning the programming language itself. That is because there is much, much more into building apps than understanding the quirks of the language. I highly recommend you to read the Swift book side by side with your experiments in app development.