从AppDelegate.swift将值分配给视图控制器 [英] Aassigning a value to a view controller from 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
中的代码移动到需要的位置.然后,您也可以直接设置标签,因为视图已加载.
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屋!