所以,在我的情节提要上,我有一个带有容器视图的UIViewController,其中嵌入了一个UITableViewController。这就是它的样子:https://i.imgur.com/0yBWtbG.png

但是现在我想设置由相机拍摄的照片,但是问题是:UIImageView在UITableViewController内,打开相机的按钮在UIViewController内(您可以在上传的屏幕快照中看到相机图标)。

所以,我尝试了这个:

UIViewController的代码:

@IBAction func openCamera(_ sender: UIButton) {
    let image = UIImagePickerController()
    image.delegate = self

    image.sourceType = UIImagePickerController.SourceType.camera
    image.allowsEditing = false

    self.present(image, animated: true)
    {

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

    let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)

    if let image = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage
    {
        takenPhoto = image //takenPhoto is a global variable
        let CIV = CreateIssue()
        CIV.showImage()
    }
    else
    {

    }

    self.dismiss(animated: true, completion: nil)
}
fileprivate func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: [UIImagePickerController.InfoKey: Any]) -> [String: Any] {
    return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, value)})
}


fileprivate func convertFromUIImagePickerControllerInfoKey(_ input: UIImagePickerController.InfoKey) -> String {
    return input.rawValue
}
}


UITableViewController的代码:

public func showPicture() {
  ivPicture.isHidden = false //here I get Unexpectedly found nil after taking a picture from the camera
  ivPicture.image = takenPhoto
}


但是,如果我尝试在ViewDidLoad中调用该函数,则效果很好:

override func viewDidLoad() {
  showPicture()
}


最近几天,我发布了一个类似的问题,但是我试图通过segues从另一个swift文件中更改一个变量,并被告知使用prepare for segue方法,但是在这种情况下,我只是在调用另一个swift文件的函数和对象属于同一swift文件。我也需要使用“准备缝制”方法吗?还是我做错了什么?

先感谢您

最佳答案

您创建子vc的实例

let CIV = CreateIssue()
CIV.showImage()


那不是从情节提要加载的,所以所有出口都为零,所以更换它

let CIV = CreateIssue()




let CIV =  self.children[0] as! CreateIssue

关于ios - Swift:在尝试更改从另一个swift文件调用的函数中的对象属性时,意外地发现“nil”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53319205/

10-14 20:26
查看更多