简体   繁体   English

在应用程序中的Today扩展和ViewController之间公开类

[英]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.

相关问题 点击扩展程序(今天的扩展程序)后是否可以打开特定的viewcontroller(包含应用程序)? - is it possible to open specific viewcontroller(containing app) after tap on extension(today extension)? 从Today扩展中打开特定的ViewController - Opening specific ViewController from Today Extension 当主应用程序未打开时,Swift 如何从今天的扩展中打开特定的视图控制器 - Swift how to open specific viewcontroller from today extension when main app is not open 今日扩展(应用组) - Today Extension (App Groups) 在Swift中的主应用程序和Today小部件/扩展之间共享UI元素 - Sharing UI elements between main App and Today widget/extension in Swift 在Swift中的应用和今日扩展之间共享核心数据数据库 - Share Core Data database between app and today extension in Swift 无法在应用程序和今日扩展之间共享数据 - Can't share data between app and today extension (iOS Swift)在应用程序和Today Extension之间共享EKEventStore和访问 - (iOS Swift) Share EKEventStore and access between app and Today Extension 我们可以在Today- Widget-Extension中共享App类吗 - Can we share App class in Today- Widget-Extension iOS Swift Today Extension:从容器应用程序导入类? - iOS Swift Today Extension: import class from container app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM