0

In my app I have two View Controllers embedded in Navigation Controller. And I want to have an option to get data from the SecondVC while only the FirstVC is opened. So it's like I don't want a user to see (or at least interact) with the SecondVC.

The point is

So user just taps on the button on the FirstVC (without opening SecondVC before) and I can get access to the data on the SecondVC. How can I get data from the next view controller (SecondVC) in a navigation controller without opening it? Or at least not letting user to interact with it.

For now my only ideas are:

  1. To push segue when user taps a button, get it into memory, load data and then dismiss it. But that doesn't look good when view controllers change so fast.

  2. To present next view controller modally offscreen, get data and dismiss it. So the user won't even see that another view controller opened.

Edit:

I need to get access not just to method or smth. I need to get to the @IBOutlet. To the subclass of UIView (working with CorePlot) to be exactly

bolt
  • 387
  • 2
  • 6
  • 16
  • Put common data and logic inside a singleton object (https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift/). So you can access it from both VCs – heyfrank Apr 10 '18 at 07:47
  • Choosing the same path, If I understood correctly, you have no intentions of displaying secondVC just want some data form it. What if you instantiate the view controller and get the data, do not present of push it. – Hussaan S Apr 10 '18 at 07:50
  • Check this answer. [Data transfer between two controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Rakesh Patel Apr 10 '18 at 07:50
  • do you mean, you want to get data from api before opening second VC? or you just wanna access methods of second VC and call them before going to that VC? – MRizwan33 Apr 10 '18 at 07:51
  • @MRizwan33 just wanna access methods of second VC and call them before going to that VC. I would say, even WITHOUT going (without presenting to the user) to the second vc – bolt Apr 10 '18 at 07:52
  • I think, the best way is use Manager, which includes all information. if you press button, you receive your data from manager, and if you need to go to another controller, you can get this data in it too – Sergey Hleb Apr 10 '18 at 07:57
  • if you just wanna use properties of second VC than instantiate it. if you wanna use methods than make a post notification and call that method. there are other ways too. – MRizwan33 Apr 10 '18 at 07:58
  • but if you need to use some methods, you need only create an object of second VC, and work with it – Sergey Hleb Apr 10 '18 at 07:59
  • another way is to make second VC as xib file give methods class level access than instantiate it and use all methods of it. – MRizwan33 Apr 10 '18 at 07:59
  • To instantiate the viewController and get the outlets, use https://stackoverflow.com/questions/24035984/instantiate-and-present-a-viewcontroller-in-swift – Vollan Apr 10 '18 at 08:06
  • Please, check my answer. There is a possible problem but @IBOutlets are called – bolt Apr 10 '18 at 08:33
  • https://stackoverflow.com/a/24049111/2933712 – erim kurt Apr 10 '18 at 08:39

3 Answers3

2

You should never call loadView() directly.

Instantiate your UIViewController and then call loadViewIfNeeded().

Calling this method loads the view controller’s view from its storyboard file, or creates the view as needed based on the established rules.

EDIT:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

// make sure that the value is an optional so that you can unset it later. This one is optional because we use optional casting (with as?)
var vc = storyboard.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController
vc?.loadViewIfNeeded()

// use the vc

// later unset vc variable
vc = nil
Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • Okay, I see, everything works and thats what I wanted. Though I still have one question. How can I remove View Controller from memory after calling it this way? – bolt Apr 10 '18 at 08:51
  • just unset your reference to the vc. I will update answer with example. – Scriptable Apr 10 '18 at 08:53
0

Solution:

You can create a function for it or just insert in viewDidLoad() etc.

Don't forget to change StoryboardID for the chosen view controller (it can be done in your storyboard file in Identity inspector).

    // You need to get to your Storyboard and instantiate the needed View Controller
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    var graphVC = storyboard.instantiateViewController(withIdentifier: "graphVCID") as? graphVC

    // Then call loadViewIfNeeded (don't call loadView()) to load @IBOutlet's etc
    graphVC?.loadViewIfNeeded()

    // My outlets are loaded with data when these functions are called
    graphVC?.viewDidLoad()
    graphVC?.viewWillAppear(false)

    // Unset the reference to VC
    graphVC = nil
bolt
  • 387
  • 2
  • 6
  • 16
0

for this, you must call your 2ed View controller with 'modalPresent' and background of 2edVC must clear. Then get your data from 2ed VC. with no change. for more information about modalPresent there is the link.

Bijender Singh Shekhawat
  • 3,934
  • 2
  • 30
  • 36