简体   繁体   English

Swift 4,加载下一个ViewController

[英]Swift 4, load next ViewController

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. 我希望有一个选项可以在仅打开FirstVC的情况下从SecondVC获取数据。 So it's like I don't want a user to see (or at least interact) with the SecondVC. 所以这就像我不希望用户看到(或至少与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. 因此,用户只需点击FirstVC上的按钮(之前无需打开SecondVC),我就可以访问SecondVC上的数据。 How can I get data from the next view controller (SecondVC) in a navigation controller without opening it? 如何在不打开导航控制器的情况下从下一个视图控制器(SecondVC)获取数据? 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. 要在用户点击按钮时推动segue,请将其存储到内存中,加载数据然后将其关闭。 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. 我需要去@IBOutlet。 To the subclass of UIView (working with CorePlot) to be exactly 确切地说,是UIView的子类(使用CorePlot)

You should never call loadView() directly. 您永远不要直接调用loadView()

Instantiate your UIViewController and then call loadViewIfNeeded() . 实例化您的UIViewController,然后调用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

Solution: 解:

You can create a function for it or just insert in viewDidLoad() etc. 您可以为其创建函数,也可以仅插入viewDidLoad()等。

Don't forget to change StoryboardID for the chosen view controller (it can be done in your storyboard file in Identity inspector). 不要忘记为选定的视图控制器更改StoryboardID(可以在Identity inspector中的Storyboard文件中完成)。

    // 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

for this, you must call your 2ed View controller with 'modalPresent' and background of 2edVC must clear. 为此,您必须使用“ modalPresent”调用2ed View控制器,并且必须清除2edVC的背景。 Then get your data from 2ed VC. 然后从2ed VC获取数据。 with no change. 没有变化。 for more information about modalPresent there is the link . 有关modalPresent的更多信息,请参见链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift-在后台加载ViewController - Swift - Load ViewController in background Swift:实例化ViewController时加载XIB - Swift: Load XIB when instantiating viewcontroller 在推送通知Swift 2 AppDelegate上加载特定的viewController - Load a specific viewController on push notification Swift 2 AppDelegate Swift 2-无法在单独的ViewController中加载UIWebView - Swift 2 - Cannot load UIWebView in separate ViewController 使用Swift在Facebook登录后加载另一个ViewController - Load Another ViewController after Facebook Login with Swift 如何在Swift3中将肥皂响应传递给下一个viewController - How to pass soap response to next viewController in swift3 从图库中选择图像后如何打开下一个视图控制器(swift) - How to open next viewcontroller after choosing image from gallery (swift) Swift - 无法将值成功传递给下一个显示的ViewController - Swift - Cannot pass value successfully to the ViewController that is presented next segue之后无法从下一个ViewController上的UIImagePickerController加载图像 - Fails to load image from UIImagePickerController on next ViewController after segue 如何在Swift中将下一个ViewController中的UIlabel的颜色更改为当前ViewController中的选定颜色 - How to change the color of a UIlabel in the next ViewController, to the selected color in the current ViewController in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM