从 AppDelegate.swift 为视图控制器赋值 [英] Aassigning a value to a view controller from AppDelegate.swift

查看:33
本文介绍了从 AppDelegate.swift 为视图控制器赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从 AppDelegate.swift 为视图控制器赋值,但没有成功.

I try to assign a value to a view controller from AppDelegate.swift without success.

我的控制器名为DestinationsViewController,它在Main.storyboard 中的id 是destinationsID.DestinationsController 嵌入在导航控制器中.我要更改的对象名为标签".这是代码:

My controller is named DestinationsViewController, and its id in Main.storyboard is destinationsID. DestinationsController is embed in a Navigation Controller. The objet I want to change is named "label". This is the code:

if let destinationsViewController = storyBoard.instantiateViewControllerWithIdentifier("destinationsID") as? DestinationsViewController {
       if let label = destinationsViewController.label{
            label.text = "Super!"
        }
        else{
            println("Not good 2")
        }
    }
    else {
        println("Not good 1")
    }

不幸的是,我收到消息:不好 2".这不好:-(

Unfortunately, I get the message: "Not good 2". This is not good :-(

谢谢.

import UIKit

class DestinationsViewController: UIViewController {

@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }
}

推荐答案

好的,这是您的方法.但是,如果您更改故事板的结构,则这可能会中断.

Ok, here's how you can do it. However, if you change the structure of your storyboard then this may break.

首先,在 DestinationsViewController 中,您需要设置一个保存文本的变量,因为我们在呈现视图之前设置文本.因此,该标签尚不存在.当视图加载时,它会设置标签.

First, in DestinationsViewController, you need to set a variable that will hold the text because we're setting the text before the view is rendered. Therefore, the label will not exist yet. When the view loads, it'll set the label.

class DestinationsViewController: UIViewController {

@IBOutlet weak var label: UILabel!
var labelText = String()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    label.text = labelText
}

现在,在 AppDelegate 中,我们设置将在视图加载时设置标签的变量.

Now, in the AppDelegate, we set the variable that will set the label when the view loads.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    // assuming inital view is tabbar
    let tabBarController = self.window?.rootViewController as UITabBarController
    let tabBarRootViewControllers: Array = tabBarController.viewControllers!

    // assuming first tab bar view is the NavigationController with the DestinationsViewController
    let navView = tabBarRootViewControllers[0] as UINavigationController 
    let destinationsViewController = navView.viewControllers[0] as DestinationsViewController
    destinationsViewController.labelText = "Super!"

    return true
}

编辑

在重新阅读您的最后一条评论后,我意识到您希望在应用程序运行后的某个时间点设置标签.您可以在 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) 中移动代码 ->Bool 到你需要的地方.然后也可以直接设置label,因为view已经加载完毕.

EDIT

After re-reading your last comment, I realized you want to set the label at some point after the app has already been running. You can just move the code in func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool to where you need it. Then you can also set the label directly, because the view has been loaded.

// assuming inital view is tabbar
let tabBarController = self.window?.rootViewController as UITabBarController
let tabBarRootViewControllers: Array = tabBarController.viewControllers!

// assuming first tab bar view is the NavigationController with the DestinationsViewController
let navView = tabBarRootViewControllers[0] as UINavigationController 
let destinationsViewController = navView.viewControllers[0] as DestinationsViewController

if let label = destinationsViewController.label{
    label.text = "Super DUper!"
}
else{
    println("Not good 2")
}

这篇关于从 AppDelegate.swift 为视图控制器赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆