xcode - 1 个 UIImagePickerController 和 2 个按钮

标签 xcode swift uibutton uiimagepickercontroller

我有一个 UIImagePickerController 和 2 个按钮。当我点击按钮 1 时,我想设置按钮 1 的图像。当我点击按钮 2 时,我想设置按钮 2 的图像。我已经使用 UIImagePickerController 成功设置了按钮 1 的图像,但未能成功使用按钮 2。这是我的代码:

var whichButton: Int = 0

func displayImagePicker(){

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false

    self.presentViewController(imagePicker, animated: true, completion: nil)

}

@IBAction func chooseImageOne(sender: AnyObject) {

    displayImagePicker()

    whichButton = 1

}

@IBAction func chooseImageTwo(sender: AnyObject) {

    displayImagePicker()

    whichButton = 2

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    self.dismissViewControllerAnimated(true, completion: nil)

    if whichButton == 1 {

        self.imageOne.setImage(image, forState: UIControlState.Normal)

    } else if whichButton == 2 {

        self.imageTwo.setImage(image, forState: UIControlState.Normal)

    }
}

正如你所看到的,我尝试使用一个 var 来跟踪我正在点击的按钮。我不确定我在 didFinishPickingImage 函数中所做的是否正确。我对 swift 很陌生,如果有人能阐明一些观点,那就太好了!提前致谢

最佳答案

我亲自在 xcode 上检查了你的代码,它是正确的。请查看您是否已将 IBOutlet 赋予 self.imageTwo 按钮,并检查 IBAction,即 imageTwo 按钮的 touchupInside。其他一切都很好

关于xcode - 1 个 UIImagePickerController 和 2 个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34851820/

相关文章:

iphone - 将 @selector 传递给我的 UIbutton 但单击事件没有响应

ios - 如何检查 UIButton 是否有目标?

ios - Swift 中的 AudioBufferList

ios - DynamoDB 如何对 bool 值使用过滤器表达式

ios - 当按钮是 iOS 中自定义单元格的 subview 时,如何动态更改按钮文本?

ios - 如何选择 Swift 编译器版本

ios - 定义一个可以在运行时在 View 之间切换的 Storyboard

Swift:在 View Controller 内的 UILabel 中使用 Int

ios - iphone 加速度计的重力读数与用户加速度读数?

xcode - 如何水平框UIButtons?