[英]make public class between Today extension and ViewController in app
I'm trying to create Today app extension. 我正在尝试创建Today应用扩展。 I want to have one UIViewController for loading all data to app and use the same variables in this UIViewController for Today Extension and App.
我想要一个UIViewController来将所有数据加载到应用程序,并在该UIViewController中为Today Extension和App使用相同的变量。 For Example: I have UILabel for TodayAppExtension and for app and I want to connect Label text like:
例如:我有用于TodayAppExtension和app的UILabel,我想连接Label文本,例如:
my_label.text = "hello" my_label.text =“你好”
to both UILabels ... I tried make public class for do it like example: 到两个UILabel ...我尝试使它成为公共类,例如:
THIS IS CLASS FOR BOTH VIEWCONTROLLERS 这是两个视图控制器的类
import UIKit
导入UIKit
public class AppDataViewController: UIViewController {
公共类AppDataViewController:UIViewController {
@IBOutlet public var pacificLabel: UILabel! override public func viewDidLoad() { super.viewDidLoad() } override public func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } public func updateClocks() { var time: NSDate = NSDate() println("Time: \\(time)") var timeString : NSString = "Time: \\(time)" let formatter:NSDateFormatter = NSDateFormatter(); var timeZone = NSTimeZone(name: "UTC") formatter.timeZone = timeZone formatter.dateFormat = "HH:mm:ss" println("UTC Time: \\(formatter.stringFromDate(time))") timeZone = NSTimeZone(name: "US/Pacific") formatter.timeZone = timeZone formatter.dateFormat = "HH:mm:ss" println("PST Time: \\(formatter.stringFromDate(time))") self.pacificLabel.text = formatter.stringFromDate(time) }
}
}
How can I access to this self.pacificLabel.text from StoryBoard from App and StoryBoard from Extension ? 如何从App的StoryBoard和Extension的StoryBoard访问此self.pacificLabel.text? Always when I'm trying to call function updateClock() from ViewController I get this:
总是在尝试从ViewController调用函数updateClock()时得到此信息:
fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:解开Optional值时意外发现nil
You never created pacificLabel. 您从未创建过pacificLabel。
In your 在你的
override public func viewDidLoad() {
super.viewDidLoad()
}
You need to create a UILabel object. 您需要创建一个UILabel对象。
The error you're getting is because you declared it as an optional that WILL exist when you use it. 您收到的错误是因为您在使用它时将其声明为WILL存在的可选内容。 So you dont need to unwrap it.
因此,您无需拆开包装。 As such it expected pacificLabel to exist, which it didn't at the time you 'unwrapped it'.
因此,它希望pacificLabel存在,而在您“解开它”时并没有。
Create the UILabel object inside viewDidLoad and it will work. 在viewDidLoad内部创建UILabel对象,它将起作用。
If the issue is that you have this label existing in your storyboard, you need to control-drag that object from the view class (the view in the storyboard) to the script to create a reference to that object. 如果问题是情节提要中存在此标签,则需要控制该对象从视图类(情节提要中的视图)到脚本的拖拽,以创建对该对象的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.