Can UIImagePickerController be used directly on story board?












1















I have an impression that ViewController usage are similar: on story board, we drag an UIViewController onto scene, then change its class type, e.g. to UIImagePickerController. (I want to make a dedicated scene for picking images)



But later I find that UIImagePickerController won't work if I directly subclass:



class TestUIImagePickerController: UIImagePickerController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
self.sourceType = .photoLibrary
self.delegate = self
// self.present(self, animated: true) // either comment it out or not, both way won't work.
}


But it works only if I put an UIViewController on storyboard, then construct an UIImagePickerController programmatically:



class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()

let picker = UIImagePickerController()
picker.sourceType = .photoLibrary
picker.delegate = self
self.present(picker, animated: true)
}


May I know whether I missed anything in the first usage example? And is it a must to create UIImagePickerController programmatically then present it via an agent view controller (UIVIewController)?










share|improve this question





























    1















    I have an impression that ViewController usage are similar: on story board, we drag an UIViewController onto scene, then change its class type, e.g. to UIImagePickerController. (I want to make a dedicated scene for picking images)



    But later I find that UIImagePickerController won't work if I directly subclass:



    class TestUIImagePickerController: UIImagePickerController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    override func viewDidLoad() {
    super.viewDidLoad()
    self.sourceType = .photoLibrary
    self.delegate = self
    // self.present(self, animated: true) // either comment it out or not, both way won't work.
    }


    But it works only if I put an UIViewController on storyboard, then construct an UIImagePickerController programmatically:



    class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    override func viewDidLoad() {
    super.viewDidLoad()

    let picker = UIImagePickerController()
    picker.sourceType = .photoLibrary
    picker.delegate = self
    self.present(picker, animated: true)
    }


    May I know whether I missed anything in the first usage example? And is it a must to create UIImagePickerController programmatically then present it via an agent view controller (UIVIewController)?










    share|improve this question



























      1












      1








      1








      I have an impression that ViewController usage are similar: on story board, we drag an UIViewController onto scene, then change its class type, e.g. to UIImagePickerController. (I want to make a dedicated scene for picking images)



      But later I find that UIImagePickerController won't work if I directly subclass:



      class TestUIImagePickerController: UIImagePickerController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

      override func viewDidLoad() {
      super.viewDidLoad()
      self.sourceType = .photoLibrary
      self.delegate = self
      // self.present(self, animated: true) // either comment it out or not, both way won't work.
      }


      But it works only if I put an UIViewController on storyboard, then construct an UIImagePickerController programmatically:



      class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
      override func viewDidLoad() {
      super.viewDidLoad()

      let picker = UIImagePickerController()
      picker.sourceType = .photoLibrary
      picker.delegate = self
      self.present(picker, animated: true)
      }


      May I know whether I missed anything in the first usage example? And is it a must to create UIImagePickerController programmatically then present it via an agent view controller (UIVIewController)?










      share|improve this question
















      I have an impression that ViewController usage are similar: on story board, we drag an UIViewController onto scene, then change its class type, e.g. to UIImagePickerController. (I want to make a dedicated scene for picking images)



      But later I find that UIImagePickerController won't work if I directly subclass:



      class TestUIImagePickerController: UIImagePickerController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

      override func viewDidLoad() {
      super.viewDidLoad()
      self.sourceType = .photoLibrary
      self.delegate = self
      // self.present(self, animated: true) // either comment it out or not, both way won't work.
      }


      But it works only if I put an UIViewController on storyboard, then construct an UIImagePickerController programmatically:



      class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
      override func viewDidLoad() {
      super.viewDidLoad()

      let picker = UIImagePickerController()
      picker.sourceType = .photoLibrary
      picker.delegate = self
      self.present(picker, animated: true)
      }


      May I know whether I missed anything in the first usage example? And is it a must to create UIImagePickerController programmatically then present it via an agent view controller (UIVIewController)?







      ios swift uiimagepickercontroller






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 2:19









      rmaddy

      245k27324388




      245k27324388










      asked Jan 2 at 2:16









      modellermodeller

      1,85211227




      1,85211227
























          1 Answer
          1






          active

          oldest

          votes


















          3














          self.present(self, animated: true) 


          you can not self present self, use another ViewController to present UIImagePickerController



          UIImagePickerController can be used on Story board, in your code, for example



          class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
          override func viewDidLoad() {
          super.viewDidLoad()
          let sb = UIStoryboard(name: "ImagePickerStoryboard", bundle: nil)
          let picker = sb.instantiateViewControllerWithIdentifier("ImagePicker") as! TestUIImagePickerController
          self.present(picker, animated: true)
          }


          keep in mind that UIImagePickerController is subclass of UINavigationController






          share|improve this answer


























          • So, not all ViewControllers are equal (in terms of usage): i.e. UIViewController can be used directly by placement on Story board, but some view controller, e.g. UIImagePickerController, MUST use a proxy viewcontroller to present it. Am I right?

            – modeller
            Jan 2 at 2:34











          • update my answer, init TestUIImagePickerController(subclass of UIImagePickerController) from storyboard

            – Ethan
            Jan 2 at 2:53













          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%2f54000500%2fcan-uiimagepickercontroller-be-used-directly-on-story-board%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          self.present(self, animated: true) 


          you can not self present self, use another ViewController to present UIImagePickerController



          UIImagePickerController can be used on Story board, in your code, for example



          class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
          override func viewDidLoad() {
          super.viewDidLoad()
          let sb = UIStoryboard(name: "ImagePickerStoryboard", bundle: nil)
          let picker = sb.instantiateViewControllerWithIdentifier("ImagePicker") as! TestUIImagePickerController
          self.present(picker, animated: true)
          }


          keep in mind that UIImagePickerController is subclass of UINavigationController






          share|improve this answer


























          • So, not all ViewControllers are equal (in terms of usage): i.e. UIViewController can be used directly by placement on Story board, but some view controller, e.g. UIImagePickerController, MUST use a proxy viewcontroller to present it. Am I right?

            – modeller
            Jan 2 at 2:34











          • update my answer, init TestUIImagePickerController(subclass of UIImagePickerController) from storyboard

            – Ethan
            Jan 2 at 2:53


















          3














          self.present(self, animated: true) 


          you can not self present self, use another ViewController to present UIImagePickerController



          UIImagePickerController can be used on Story board, in your code, for example



          class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
          override func viewDidLoad() {
          super.viewDidLoad()
          let sb = UIStoryboard(name: "ImagePickerStoryboard", bundle: nil)
          let picker = sb.instantiateViewControllerWithIdentifier("ImagePicker") as! TestUIImagePickerController
          self.present(picker, animated: true)
          }


          keep in mind that UIImagePickerController is subclass of UINavigationController






          share|improve this answer


























          • So, not all ViewControllers are equal (in terms of usage): i.e. UIViewController can be used directly by placement on Story board, but some view controller, e.g. UIImagePickerController, MUST use a proxy viewcontroller to present it. Am I right?

            – modeller
            Jan 2 at 2:34











          • update my answer, init TestUIImagePickerController(subclass of UIImagePickerController) from storyboard

            – Ethan
            Jan 2 at 2:53
















          3












          3








          3







          self.present(self, animated: true) 


          you can not self present self, use another ViewController to present UIImagePickerController



          UIImagePickerController can be used on Story board, in your code, for example



          class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
          override func viewDidLoad() {
          super.viewDidLoad()
          let sb = UIStoryboard(name: "ImagePickerStoryboard", bundle: nil)
          let picker = sb.instantiateViewControllerWithIdentifier("ImagePicker") as! TestUIImagePickerController
          self.present(picker, animated: true)
          }


          keep in mind that UIImagePickerController is subclass of UINavigationController






          share|improve this answer















          self.present(self, animated: true) 


          you can not self present self, use another ViewController to present UIImagePickerController



          UIImagePickerController can be used on Story board, in your code, for example



          class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
          override func viewDidLoad() {
          super.viewDidLoad()
          let sb = UIStoryboard(name: "ImagePickerStoryboard", bundle: nil)
          let picker = sb.instantiateViewControllerWithIdentifier("ImagePicker") as! TestUIImagePickerController
          self.present(picker, animated: true)
          }


          keep in mind that UIImagePickerController is subclass of UINavigationController







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 2:50

























          answered Jan 2 at 2:24









          EthanEthan

          353310




          353310













          • So, not all ViewControllers are equal (in terms of usage): i.e. UIViewController can be used directly by placement on Story board, but some view controller, e.g. UIImagePickerController, MUST use a proxy viewcontroller to present it. Am I right?

            – modeller
            Jan 2 at 2:34











          • update my answer, init TestUIImagePickerController(subclass of UIImagePickerController) from storyboard

            – Ethan
            Jan 2 at 2:53





















          • So, not all ViewControllers are equal (in terms of usage): i.e. UIViewController can be used directly by placement on Story board, but some view controller, e.g. UIImagePickerController, MUST use a proxy viewcontroller to present it. Am I right?

            – modeller
            Jan 2 at 2:34











          • update my answer, init TestUIImagePickerController(subclass of UIImagePickerController) from storyboard

            – Ethan
            Jan 2 at 2:53



















          So, not all ViewControllers are equal (in terms of usage): i.e. UIViewController can be used directly by placement on Story board, but some view controller, e.g. UIImagePickerController, MUST use a proxy viewcontroller to present it. Am I right?

          – modeller
          Jan 2 at 2:34





          So, not all ViewControllers are equal (in terms of usage): i.e. UIViewController can be used directly by placement on Story board, but some view controller, e.g. UIImagePickerController, MUST use a proxy viewcontroller to present it. Am I right?

          – modeller
          Jan 2 at 2:34













          update my answer, init TestUIImagePickerController(subclass of UIImagePickerController) from storyboard

          – Ethan
          Jan 2 at 2:53







          update my answer, init TestUIImagePickerController(subclass of UIImagePickerController) from storyboard

          – Ethan
          Jan 2 at 2:53






















          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%2f54000500%2fcan-uiimagepickercontroller-be-used-directly-on-story-board%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







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory