首页 > 解决方案 > 在扩展中切换视图控制器

问题描述

所以我在我的视图控制器中有一个扩展,现在我要添加到扩展底部的是一个@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
                    
                        
            }
                    
        }

标签: swift

解决方案


您需要在主控制器声明中添加所有@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)
            }
        }
    }
}

推荐阅读