如何使用不同的按钮与不同的结果同样的方法?(How to use different buttons

2019-09-27 13:29发布

我已经从图像库中被加载之后要被显示的四个不同的图像中,两个在顶部,两个在底部的正方形。

每个图像视图关联到其个人的按钮,即会使用进入用户库中相同的方法来进行摄像。

我如何使用四种不同的按钮,用同样的方法来显示在适当的位置装的形象呢?

我可以做一个模型来执行这种行为?

谢谢

@IBOutlet weak var topLeftImage: UIImageView!
@IBOutlet weak var topRightImage: UIImageView!
@IBOutlet weak var bottomLeftImage: UIImageView!
@IBOutlet weak var bottomRightImage: UIImageView!

@IBOutlet weak var topLeftButton: UIButton!
@IBOutlet weak var topRightButtom: UIButton!
@IBOutlet weak var bottomLeftButton: UIButton!
@IBOutlet weak var bottomRightButton: UIButton!

@IBAction func loadTopLeftImage(_ sender: Any) {
    loadPhotoLibrary()
}

@IBAction func loadTopRightImage(_ sender: Any) {
    loadPhotoLibrary()
}

@IBAction func loadBottomLeftImage(_ sender: Any) {
    loadPhotoLibrary()
}

@IBAction func loadBottomRightImage(_ sender: Any) {
    loadPhotoLibrary()
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {


        topLeftImage.image = image

    }
    self.dismiss(animated: true, completion: nil)
}


func loadPhotoLibrary() {

        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
        imagePicker.allowsEditing = false

        self.present(imagePicker, animated: true)
}

Answer 1:

首先创建的子类UIImagePickerController ,并添加变量completionHandler类型的参数UIImage?

class YourPicker: UIImagePickerController {
    var completionHandler: ((UIImage?) -> ())?
}

然后添加参数,您loadPhotoLibrary功能,将代表UIImageView你想改变它的image 。 同时设定了完成处理程序:你想改变image一定的ImageView的

func loadPhotoLibrary(setImageView imageView: UIImageView) {

    let imagePicker = YourPicker() // don't forget to initialize subclass

    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
    imagePicker.allowsEditing = false

    imagePicker.completionHandler = { image in
        if let image = image { // if you just dismiss picker, you don't want to delete previous image
            imageView.image = image
        }
    }

    self.present(imagePicker, animated: true)
}

现在选择器的委托方法低垂picker作为YourPicker和呼叫完成与键值UIImagePickerController.InfoKey浇铸UIImage? (如果它的值是nil ,也没有关系,它只是不设置image的ImageView的)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    if let myPicker = picker as! YourPicker {
        myPicker.completionHandler!(info[UIImagePickerController.InfoKey.originalImage] as? UIImage)
    }
    self.dismiss(animated: true, completion: nil)
}

然后就在按钮的动作叫你loadPhotoLibrary方法是这样

@IBAction func loadTopLeftImage(_ sender: Any) {
    loadPhotoLibrary(setImageView: topLeftImage)
}
...


Answer 2:

1如果你使用的故事板,控制和所有的按钮拖动到同样的方法。 如果youre添加目标为每个按钮,显示全部的选择的相同方法。

2-添加标签:在故事板 - >你的按钮(S) - >给每一个唯一的标签中的代码添加button.tag = uniqueTag

3-在您的通用方法,你可以使用一个开关(sender.tag)为每个按钮



文章来源: How to use different buttons with a same method for different results?