Home > other >  How to put UILabel and UIImage into the class method
How to put UILabel and UIImage into the class method

Time:11-20


import UIKit
class ViewController: UIViewController {
    
    
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var chargeLabel: UILabel!
    @IBOutlet weak var bigDogLabel: UILabel!
    @IBOutlet weak var cafeImageView: UIImageView!
    
    
    override func viewDidLoad() {
    }
    
    
    
    struct DogCafe{
        var name: String
        var charge: Int
        var bigDog: Bool
        var cafeImage: UIImage
        
        
        init(name: String, charge: Int, bigDog: Bool, cafeImage: UIImage) {
            self.name = name
            self.charge = charge
            self.cafeImage = cafeImage
            self.bigDog = bigDog
        }
        
        
        
        func message() {
            nameLabel.text = "카페 \(name) 입니다"
            chargeLabel.text = "입장료는 \(charge) 입니다"
            cafeImageView.image = cafeImage
            if bigDog == true {
                bigDogLabel.text = "대형견 출입이 가능한 매장입니다"
            } else {
                bigDogLabel.text = "대형견 출입이 불가능한 매장입니다"
            }
        }
    }
    
    
    
    
    @IBAction func ohdodokButton(_ sender: UIButton) {
        let dogCafe = DogCafe(name: "카페 오도독", charge: 3500, bigDog: true, cafeImage: #imageLiteral(resourceName: "ohdodok"))
        dogCafe.message()
    }
    
    
    
    @IBAction func meltingButton(_ sender: UIButton) {
        let dogCafe2 = DogCafe(name: "멜팅", charge: 5000, bigDog: false, cafeImage: #imageLiteral(resourceName: "melting"))
        dogCafe2.message()
    }
    
    
    
    
    
}
  func message() {
            nameLabel.text = "카페 \(name) 입니다"
            chargeLabel.text = "입장료는 \(charge) 입니다"
            cafeImageView.image = cafeImage
            if bigDog == true {
                bigDogLabel.text = "대형견 출입이 가능한 매장입니다"
            } else {
                bigDogLabel.text = "대형견 출입이 불가능한 매장입니다"
            }

Instance member 'nameLabel' of type 'ViewController' cannot be used on instance of nested type 'ViewController.DogCafe'

Instance member 'chargeLabel' of type 'ViewController' cannot be used on instance of nested type 'ViewController.DogCafe'

Instance member 'cafeImageView' of type 'ViewController' cannot be used on instance of nested type 'ViewController.DogCafe'

Instance member 'bigDogLabel' of type 'ViewController' cannot be used on instance of nested type 'ViewController.DogCafe'

CodePudding user response:

you can use like below

@IBAction func ohdodokButton(_ sender: UIButton) {
    let dogCafe = DogCafe(name: "카페 오도독", charge: 3500, bigDog: true, cafeImage: #imageLiteral(resourceName: "ohdodok"))
    setCurrentDogAttributes(currentDog: dogCafe)
}

@IBAction func meltingButton(_ sender: UIButton) {
    let dogCafe2 = DogCafe(name: "멜팅", charge: 5000, bigDog: false, cafeImage: #imageLiteral(resourceName: "melting"))
    setCurrentDogAttributes(currentDog: dogCafe)
}

  func setCurrentDogAttributes(currentDog:DogCafe){
    nameLabel.text=currentDog.name
   //Set your Values Here
}

CodePudding user response:

Your approach is wrong. You need to read up on separation of concerns. I'd also suggest reading up on the MVC (Model View Controller) design pattern, which is a pretty common way to do development on iOS.

In your code, DogCafe is a model object. It contains data that you want your app to manipulate and display.

A model object should not know anything about how it is being used. It just holds data.

Your view controller should take the model object and display it to its views. One way to handle that is to add a display(cafe:) method to your view controller:

class ViewController: UIViewController {

    // The rest of your view controller variables and functions would go here...

    func display(cafe: DogCafe) {
        nameLabel.text = "카페 \(cafe.name) 입니다"
        chargeLabel.text = "입장료는 \(cafe.charge) 입니다"
        cafeImageView.image = cafe.cafeImage
        if cafe.bigDog == true {
            bigDogLabel.text = "대형견 출입이 가능한 매장입니다"
        } else {
            bigDogLabel.text = "대형견 출입이 불가능한 매장입니다"
        }
    }
}
  • Related