gpt4 book ai didi

ios - 从 TextBox 到 Widget 的文本?

转载 作者:行者123 更新时间:2023-11-29 01:29:37 26 4
gpt4 key购买 nike

所以我正在制作一个iOS应用程序,它允许您在文本框中输入文本,然后有一个按钮将在标签上显示“文本”,并且标签上的“文本”将显示在通知中心,在小部件上,但是出了问题,因为每次我运行应用程序时它都会失败并将我扔到 AppDelegate.Swift 中并突出显示这个特定部分:“class AppDelegate: UIResponder,UIApplicationDelegate {"

出现错误“Thread 1: signal SIGABRT”

我不知道还能做什么;(请帮忙!!

这是 ViewController.Swift 代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet var UserInput: UITextField!

@IBOutlet var TextDisplay: UILabel!

@IBAction func DisplayButton(sender: AnyObject) {

let Text:String = UserInput.text!

if let Text = Int(Text) {

print("Computer says no!")

} else {
TextDisplay.text = (Text)

}



}


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


let SharedDefaults = NSUserDefaults(suiteName: "group.St33v3n.TextOnWidget")

SharedDefaults?.setObject(UserInput, forKey: "StringKey")

SharedDefaults?.synchronize()

}

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


}

这是 TodayView Controller 代码(来自小部件的代码):

import UIKit

import NotificationCenter

class TodayViewController: UIViewController, NCWidgetProviding {


@IBOutlet var TextToWidget: UILabel!


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view from its nib.

}

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

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {


let ShareDefaults = NSUserDefaults(suiteName:"group.St33v3n.TextOnWidget")

self.TextToWidget.text = ShareDefaults?.objectForKey("StringKey") as? String

completionHandler(NCUpdateResult.NewData)
}

}

最佳答案

行内

SharedDefaults?.setObject(UserInput, forKey: "StringKey")

您传递的是 UITextField 对象,而不是文本。你应该使用

SharedDefaults?.setObject(UserInput.text, forKey: "StringKey")

我不确定这是否是导致崩溃的原因,但无论如何都需要更正 ;)

关于ios - 从 TextBox 到 Widget 的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33580374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com