Swift: Unexpectedly found “nil” when trying to change property of objects in a function called from...












0















So, on my Storyboard I have an UIViewController with a Container View which it has an UITableViewController as embed. That's how it looks like: https://i.imgur.com/0yBWtbG.png



But now I want to set a picture taken by the camera, but the problem is: The UIImageView is inside the UITableViewController and the button for opening the camera is inside the UIViewController (you can see the camera icon on the screenshot I uploaded).



So, I tried this:



Code for the 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
}
}


Code for the UITableViewController:



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


However, if I try to call the function in the ViewDidLoad, it works fine:



override func viewDidLoad() {
showPicture()
}


The last few days, I posted a similar issue but I was trying to change a variable from another swift file through segues, and I was told to use the prepare for segue method but in this case I am just calling a function of another swift file and the obejcts belong to the same swift file. Would I need to use the prepare for segue method, too? Or what am I doing wrong?



Thank you in advance










share|improve this question





























    0















    So, on my Storyboard I have an UIViewController with a Container View which it has an UITableViewController as embed. That's how it looks like: https://i.imgur.com/0yBWtbG.png



    But now I want to set a picture taken by the camera, but the problem is: The UIImageView is inside the UITableViewController and the button for opening the camera is inside the UIViewController (you can see the camera icon on the screenshot I uploaded).



    So, I tried this:



    Code for the 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
    }
    }


    Code for the UITableViewController:



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


    However, if I try to call the function in the ViewDidLoad, it works fine:



    override func viewDidLoad() {
    showPicture()
    }


    The last few days, I posted a similar issue but I was trying to change a variable from another swift file through segues, and I was told to use the prepare for segue method but in this case I am just calling a function of another swift file and the obejcts belong to the same swift file. Would I need to use the prepare for segue method, too? Or what am I doing wrong?



    Thank you in advance










    share|improve this question



























      0












      0








      0








      So, on my Storyboard I have an UIViewController with a Container View which it has an UITableViewController as embed. That's how it looks like: https://i.imgur.com/0yBWtbG.png



      But now I want to set a picture taken by the camera, but the problem is: The UIImageView is inside the UITableViewController and the button for opening the camera is inside the UIViewController (you can see the camera icon on the screenshot I uploaded).



      So, I tried this:



      Code for the 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
      }
      }


      Code for the UITableViewController:



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


      However, if I try to call the function in the ViewDidLoad, it works fine:



      override func viewDidLoad() {
      showPicture()
      }


      The last few days, I posted a similar issue but I was trying to change a variable from another swift file through segues, and I was told to use the prepare for segue method but in this case I am just calling a function of another swift file and the obejcts belong to the same swift file. Would I need to use the prepare for segue method, too? Or what am I doing wrong?



      Thank you in advance










      share|improve this question
















      So, on my Storyboard I have an UIViewController with a Container View which it has an UITableViewController as embed. That's how it looks like: https://i.imgur.com/0yBWtbG.png



      But now I want to set a picture taken by the camera, but the problem is: The UIImageView is inside the UITableViewController and the button for opening the camera is inside the UIViewController (you can see the camera icon on the screenshot I uploaded).



      So, I tried this:



      Code for the 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
      }
      }


      Code for the UITableViewController:



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


      However, if I try to call the function in the ViewDidLoad, it works fine:



      override func viewDidLoad() {
      showPicture()
      }


      The last few days, I posted a similar issue but I was trying to change a variable from another swift file through segues, and I was told to use the prepare for segue method but in this case I am just calling a function of another swift file and the obejcts belong to the same swift file. Would I need to use the prepare for segue method, too? Or what am I doing wrong?



      Thank you in advance







      ios swift xcode






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 13:59









      Sh_Khan

      41.7k51225




      41.7k51225










      asked Nov 15 '18 at 12:09









      AdriánT95AdriánT95

      406




      406
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You create an instance of the child vc



          let CIV = CreateIssue()
          CIV.showImage()


          that not loaded from storyboard so all outlets are nil , so replace this



          let CIV = CreateIssue()


          with



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





          share|improve this answer



















          • 1





            Thank you so much! It worked! :)

            – AdriánT95
            Nov 15 '18 at 12:23











          • @AdriánT95 While this seems to fix your issue, it is a volatile fix which can possibly backfire. Instead of grabbing your CreateIssue from children and force casting it (never a good idea), you should rather keep a reference to it when you first create it (or add an outlet if it is set in storyboard/xib). It’s hard to tell more without seeing more of your code.

            – Losiowaty
            Nov 15 '18 at 23:39











          • @Losiowaty if you understands ! correctly you hadn't say that , it will never crash only if he changes the structure in storyboard which is similar to case that he has an outlet , so please don't stupidly think always that ! will cause a crash

            – Sh_Khan
            Nov 15 '18 at 23:44











          • I never said that using ! will always crash, I only said that it is not a good idea. Using if let is a safer way to achieve the same. And while I agree that it should not crash unless the storyboard is not changed, I do believe that code should be as resilient as possible. What if another embedded VC is added? Can you then guarantee the order of them in children array? Can you guarantee it won’t change with next version of iOS?

            – Losiowaty
            Nov 15 '18 at 23:52











          • @Losiowaty the developer isn't tipsy he should be aware of all changes and their impact to the current version

            – Sh_Khan
            Nov 15 '18 at 23:54



















          0














          One thing to keep in mind - A UIView, Tabbleview, and it's cells are all different classes / objects. They don't have any natural relation between one another even though they appear on the same user interface. A button in a tableview cell is a member of that Cell's class rather than the Table or the View which contains that Table.



          One question - do you need to have this button in a Tableview cell or can it simply exist as part of your UIView? It's unclear from your example if you need to repeat these cells / have multiple instances of them (the typically UITableViewCell use case) or if a single cell is all you are looking for.






          share|improve this answer
























          • Hi, thank you for your answer but I already fixed the issue. Well, I was not changing an object from a UIViewController. I mean, the objects I am trying to change belongs to the UITableViewController and I was trying to change them from the same swift file in a function and this function is called from the UIViewController. And I do need to have this button in the UIViewController because it is fixed in the top of the screen.

            – AdriánT95
            Nov 15 '18 at 12:33











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319205%2fswift-unexpectedly-found-nil-when-trying-to-change-property-of-objects-in-a-f%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You create an instance of the child vc



          let CIV = CreateIssue()
          CIV.showImage()


          that not loaded from storyboard so all outlets are nil , so replace this



          let CIV = CreateIssue()


          with



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





          share|improve this answer



















          • 1





            Thank you so much! It worked! :)

            – AdriánT95
            Nov 15 '18 at 12:23











          • @AdriánT95 While this seems to fix your issue, it is a volatile fix which can possibly backfire. Instead of grabbing your CreateIssue from children and force casting it (never a good idea), you should rather keep a reference to it when you first create it (or add an outlet if it is set in storyboard/xib). It’s hard to tell more without seeing more of your code.

            – Losiowaty
            Nov 15 '18 at 23:39











          • @Losiowaty if you understands ! correctly you hadn't say that , it will never crash only if he changes the structure in storyboard which is similar to case that he has an outlet , so please don't stupidly think always that ! will cause a crash

            – Sh_Khan
            Nov 15 '18 at 23:44











          • I never said that using ! will always crash, I only said that it is not a good idea. Using if let is a safer way to achieve the same. And while I agree that it should not crash unless the storyboard is not changed, I do believe that code should be as resilient as possible. What if another embedded VC is added? Can you then guarantee the order of them in children array? Can you guarantee it won’t change with next version of iOS?

            – Losiowaty
            Nov 15 '18 at 23:52











          • @Losiowaty the developer isn't tipsy he should be aware of all changes and their impact to the current version

            – Sh_Khan
            Nov 15 '18 at 23:54
















          0














          You create an instance of the child vc



          let CIV = CreateIssue()
          CIV.showImage()


          that not loaded from storyboard so all outlets are nil , so replace this



          let CIV = CreateIssue()


          with



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





          share|improve this answer



















          • 1





            Thank you so much! It worked! :)

            – AdriánT95
            Nov 15 '18 at 12:23











          • @AdriánT95 While this seems to fix your issue, it is a volatile fix which can possibly backfire. Instead of grabbing your CreateIssue from children and force casting it (never a good idea), you should rather keep a reference to it when you first create it (or add an outlet if it is set in storyboard/xib). It’s hard to tell more without seeing more of your code.

            – Losiowaty
            Nov 15 '18 at 23:39











          • @Losiowaty if you understands ! correctly you hadn't say that , it will never crash only if he changes the structure in storyboard which is similar to case that he has an outlet , so please don't stupidly think always that ! will cause a crash

            – Sh_Khan
            Nov 15 '18 at 23:44











          • I never said that using ! will always crash, I only said that it is not a good idea. Using if let is a safer way to achieve the same. And while I agree that it should not crash unless the storyboard is not changed, I do believe that code should be as resilient as possible. What if another embedded VC is added? Can you then guarantee the order of them in children array? Can you guarantee it won’t change with next version of iOS?

            – Losiowaty
            Nov 15 '18 at 23:52











          • @Losiowaty the developer isn't tipsy he should be aware of all changes and their impact to the current version

            – Sh_Khan
            Nov 15 '18 at 23:54














          0












          0








          0







          You create an instance of the child vc



          let CIV = CreateIssue()
          CIV.showImage()


          that not loaded from storyboard so all outlets are nil , so replace this



          let CIV = CreateIssue()


          with



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





          share|improve this answer













          You create an instance of the child vc



          let CIV = CreateIssue()
          CIV.showImage()


          that not loaded from storyboard so all outlets are nil , so replace this



          let CIV = CreateIssue()


          with



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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 12:16









          Sh_KhanSh_Khan

          41.7k51225




          41.7k51225








          • 1





            Thank you so much! It worked! :)

            – AdriánT95
            Nov 15 '18 at 12:23











          • @AdriánT95 While this seems to fix your issue, it is a volatile fix which can possibly backfire. Instead of grabbing your CreateIssue from children and force casting it (never a good idea), you should rather keep a reference to it when you first create it (or add an outlet if it is set in storyboard/xib). It’s hard to tell more without seeing more of your code.

            – Losiowaty
            Nov 15 '18 at 23:39











          • @Losiowaty if you understands ! correctly you hadn't say that , it will never crash only if he changes the structure in storyboard which is similar to case that he has an outlet , so please don't stupidly think always that ! will cause a crash

            – Sh_Khan
            Nov 15 '18 at 23:44











          • I never said that using ! will always crash, I only said that it is not a good idea. Using if let is a safer way to achieve the same. And while I agree that it should not crash unless the storyboard is not changed, I do believe that code should be as resilient as possible. What if another embedded VC is added? Can you then guarantee the order of them in children array? Can you guarantee it won’t change with next version of iOS?

            – Losiowaty
            Nov 15 '18 at 23:52











          • @Losiowaty the developer isn't tipsy he should be aware of all changes and their impact to the current version

            – Sh_Khan
            Nov 15 '18 at 23:54














          • 1





            Thank you so much! It worked! :)

            – AdriánT95
            Nov 15 '18 at 12:23











          • @AdriánT95 While this seems to fix your issue, it is a volatile fix which can possibly backfire. Instead of grabbing your CreateIssue from children and force casting it (never a good idea), you should rather keep a reference to it when you first create it (or add an outlet if it is set in storyboard/xib). It’s hard to tell more without seeing more of your code.

            – Losiowaty
            Nov 15 '18 at 23:39











          • @Losiowaty if you understands ! correctly you hadn't say that , it will never crash only if he changes the structure in storyboard which is similar to case that he has an outlet , so please don't stupidly think always that ! will cause a crash

            – Sh_Khan
            Nov 15 '18 at 23:44











          • I never said that using ! will always crash, I only said that it is not a good idea. Using if let is a safer way to achieve the same. And while I agree that it should not crash unless the storyboard is not changed, I do believe that code should be as resilient as possible. What if another embedded VC is added? Can you then guarantee the order of them in children array? Can you guarantee it won’t change with next version of iOS?

            – Losiowaty
            Nov 15 '18 at 23:52











          • @Losiowaty the developer isn't tipsy he should be aware of all changes and their impact to the current version

            – Sh_Khan
            Nov 15 '18 at 23:54








          1




          1





          Thank you so much! It worked! :)

          – AdriánT95
          Nov 15 '18 at 12:23





          Thank you so much! It worked! :)

          – AdriánT95
          Nov 15 '18 at 12:23













          @AdriánT95 While this seems to fix your issue, it is a volatile fix which can possibly backfire. Instead of grabbing your CreateIssue from children and force casting it (never a good idea), you should rather keep a reference to it when you first create it (or add an outlet if it is set in storyboard/xib). It’s hard to tell more without seeing more of your code.

          – Losiowaty
          Nov 15 '18 at 23:39





          @AdriánT95 While this seems to fix your issue, it is a volatile fix which can possibly backfire. Instead of grabbing your CreateIssue from children and force casting it (never a good idea), you should rather keep a reference to it when you first create it (or add an outlet if it is set in storyboard/xib). It’s hard to tell more without seeing more of your code.

          – Losiowaty
          Nov 15 '18 at 23:39













          @Losiowaty if you understands ! correctly you hadn't say that , it will never crash only if he changes the structure in storyboard which is similar to case that he has an outlet , so please don't stupidly think always that ! will cause a crash

          – Sh_Khan
          Nov 15 '18 at 23:44





          @Losiowaty if you understands ! correctly you hadn't say that , it will never crash only if he changes the structure in storyboard which is similar to case that he has an outlet , so please don't stupidly think always that ! will cause a crash

          – Sh_Khan
          Nov 15 '18 at 23:44













          I never said that using ! will always crash, I only said that it is not a good idea. Using if let is a safer way to achieve the same. And while I agree that it should not crash unless the storyboard is not changed, I do believe that code should be as resilient as possible. What if another embedded VC is added? Can you then guarantee the order of them in children array? Can you guarantee it won’t change with next version of iOS?

          – Losiowaty
          Nov 15 '18 at 23:52





          I never said that using ! will always crash, I only said that it is not a good idea. Using if let is a safer way to achieve the same. And while I agree that it should not crash unless the storyboard is not changed, I do believe that code should be as resilient as possible. What if another embedded VC is added? Can you then guarantee the order of them in children array? Can you guarantee it won’t change with next version of iOS?

          – Losiowaty
          Nov 15 '18 at 23:52













          @Losiowaty the developer isn't tipsy he should be aware of all changes and their impact to the current version

          – Sh_Khan
          Nov 15 '18 at 23:54





          @Losiowaty the developer isn't tipsy he should be aware of all changes and their impact to the current version

          – Sh_Khan
          Nov 15 '18 at 23:54













          0














          One thing to keep in mind - A UIView, Tabbleview, and it's cells are all different classes / objects. They don't have any natural relation between one another even though they appear on the same user interface. A button in a tableview cell is a member of that Cell's class rather than the Table or the View which contains that Table.



          One question - do you need to have this button in a Tableview cell or can it simply exist as part of your UIView? It's unclear from your example if you need to repeat these cells / have multiple instances of them (the typically UITableViewCell use case) or if a single cell is all you are looking for.






          share|improve this answer
























          • Hi, thank you for your answer but I already fixed the issue. Well, I was not changing an object from a UIViewController. I mean, the objects I am trying to change belongs to the UITableViewController and I was trying to change them from the same swift file in a function and this function is called from the UIViewController. And I do need to have this button in the UIViewController because it is fixed in the top of the screen.

            – AdriánT95
            Nov 15 '18 at 12:33
















          0














          One thing to keep in mind - A UIView, Tabbleview, and it's cells are all different classes / objects. They don't have any natural relation between one another even though they appear on the same user interface. A button in a tableview cell is a member of that Cell's class rather than the Table or the View which contains that Table.



          One question - do you need to have this button in a Tableview cell or can it simply exist as part of your UIView? It's unclear from your example if you need to repeat these cells / have multiple instances of them (the typically UITableViewCell use case) or if a single cell is all you are looking for.






          share|improve this answer
























          • Hi, thank you for your answer but I already fixed the issue. Well, I was not changing an object from a UIViewController. I mean, the objects I am trying to change belongs to the UITableViewController and I was trying to change them from the same swift file in a function and this function is called from the UIViewController. And I do need to have this button in the UIViewController because it is fixed in the top of the screen.

            – AdriánT95
            Nov 15 '18 at 12:33














          0












          0








          0







          One thing to keep in mind - A UIView, Tabbleview, and it's cells are all different classes / objects. They don't have any natural relation between one another even though they appear on the same user interface. A button in a tableview cell is a member of that Cell's class rather than the Table or the View which contains that Table.



          One question - do you need to have this button in a Tableview cell or can it simply exist as part of your UIView? It's unclear from your example if you need to repeat these cells / have multiple instances of them (the typically UITableViewCell use case) or if a single cell is all you are looking for.






          share|improve this answer













          One thing to keep in mind - A UIView, Tabbleview, and it's cells are all different classes / objects. They don't have any natural relation between one another even though they appear on the same user interface. A button in a tableview cell is a member of that Cell's class rather than the Table or the View which contains that Table.



          One question - do you need to have this button in a Tableview cell or can it simply exist as part of your UIView? It's unclear from your example if you need to repeat these cells / have multiple instances of them (the typically UITableViewCell use case) or if a single cell is all you are looking for.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 12:20









          Andrew LombardAndrew Lombard

          212114




          212114













          • Hi, thank you for your answer but I already fixed the issue. Well, I was not changing an object from a UIViewController. I mean, the objects I am trying to change belongs to the UITableViewController and I was trying to change them from the same swift file in a function and this function is called from the UIViewController. And I do need to have this button in the UIViewController because it is fixed in the top of the screen.

            – AdriánT95
            Nov 15 '18 at 12:33



















          • Hi, thank you for your answer but I already fixed the issue. Well, I was not changing an object from a UIViewController. I mean, the objects I am trying to change belongs to the UITableViewController and I was trying to change them from the same swift file in a function and this function is called from the UIViewController. And I do need to have this button in the UIViewController because it is fixed in the top of the screen.

            – AdriánT95
            Nov 15 '18 at 12:33

















          Hi, thank you for your answer but I already fixed the issue. Well, I was not changing an object from a UIViewController. I mean, the objects I am trying to change belongs to the UITableViewController and I was trying to change them from the same swift file in a function and this function is called from the UIViewController. And I do need to have this button in the UIViewController because it is fixed in the top of the screen.

          – AdriánT95
          Nov 15 '18 at 12:33





          Hi, thank you for your answer but I already fixed the issue. Well, I was not changing an object from a UIViewController. I mean, the objects I am trying to change belongs to the UITableViewController and I was trying to change them from the same swift file in a function and this function is called from the UIViewController. And I do need to have this button in the UIViewController because it is fixed in the top of the screen.

          – AdriánT95
          Nov 15 '18 at 12:33


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319205%2fswift-unexpectedly-found-nil-when-trying-to-change-property-of-objects-in-a-f%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()