swift - 在扩展中切换视图控制器
问题描述
所以我在我的视图控制器中有一个扩展,现在我要添加到扩展底部的是一个@IBAction buttonTapped,所以我的用户可以点击该按钮,然后检查照片是否已拍摄,然后转到下一个视图控制器,但它不允许我添加它说“只有实例方法可以声明@IBAction”的@IBAction
我该怎么做才能让我的用户点击一个按钮来检查图像是否被拍摄,然后将我的用户带到下一个视图控制器
extension BackroundCheckViewController: UIImagePickerControllerDelegate,
UINavigationControllerDelegate {
//cancel
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
//pick an image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo
info: [UIImagePickerController.InfoKey : Any]) {
//get the image the selected
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.ProfilePictureImageView.image = pickedImage
picker.dismiss(animated: true, completion: nil)
//upload to firebase
PhotoService.savePhoto(image: pickedImage)
}
// navigate to somewhere else
}
}
解决方案
您需要在主控制器声明中添加所有@IBOutlet
和@IBAction
,您不能在扩展中添加它们。
class BackroundCheckViewController: UIViewController {
//...
@IBAction func buttonTapped(_ sender: UIButton) {
print("Got the action here")
if ProfilePictureImageView.image != nil {
print("User has picked an image navigating to somewhere..")
let myViewController = MyViewController()
if let navigationController = navigationController {
navigationController.pushViewController(myViewController, animated: true)
} else {
present(myViewController, animated: true)
}
}
}
}
推荐阅读
- r - 如何进行非随机抽样
- sql - Redshift:错误:目标表必须是等值连接谓词的一部分
- ruby-on-rails - Carrierwave on Rails 5.1 上传器实例
- windows - 在用户选择的目录中批量复制文件
- blueprism - 如何通过 Blue prism 从 Excel 文件的页脚读取和写入数据?
- gulp - Gulp 监视任务,带有链式 onchange 的多个文件夹
- windows - 通过批量上传 png 损坏图像
- excel - 从 FTP 服务器上的文件中获取 Excel 表
- ubuntu - IBM MQ 和 Kafka 连接器问题(提供的主机名无效)
- javascript - 在 React 中实例化 Vanilla JS 类