没有数据时,在今日窗口小部件中显示消息

时间:2014-09-05 22:39:51

标签: swift

我刚刚在我的应用程序中添加了一个今天的小部件,它似乎运行良好。我注意到的一件事是,在第一次打开应用程序之前,小部件会显示一条消息“无法加载”。

我想知道如果没有要显示的数据(因为应用程序从未使用过),我可以显示一条消息,其中显示的内容类似于"无法显示。点按此处启动APPNAME即可开始使用"?

这是我目前的代码:

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

    self.preferredContentSize = CGSizeMake(320,60)
}

@IBAction func launchApp(sender: AnyObject) {
    var url: NSURL = NSURL.URLWithString("APPNAME://")
    self.extensionContext.openURL(url, completionHandler: nil)
}

override func viewWillAppear(animated: Bool) {
    self.preferredContentSize = CGSizeMake(320,60)

    var currentBudgetCalculation = ""
    var defaults = NSUserDefaults(suiteName: "group.APPNAME")
    currentBudgetCalculation = defaults.stringForKey("currentBudgetWidget")!
    currentBudgetDisplay.text = "Current: \(currentBudgetCalculation)"

    var totalBudgetCalculation = ""
    totalBudgetCalculation = defaults.stringForKey("totalBudgetWidget")!
    totalBudgetDisplay.text = "Initial: \(totalBudgetCalculation)"

    var budgetName = defaults.stringForKey("name")
    budgetNameDisplay.text = budgetName

    var valueFormatter = NSNumberFormatter()
    valueFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    var doubleValue = valueFormatter.numberFromString(currentBudgetCalculation)?.doubleValue

    if doubleValue > 0 {
        currentBudgetDisplay.textColor = UIColor.greenColor()
    } else if doubleValue == 0 {
        currentBudgetDisplay.textColor = UIColor.orangeColor()
    } else if doubleValue < 0 {
        currentBudgetDisplay.textColor = UIColor.redColor()
    }
}

func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
    let newInsets = UIEdgeInsets(top: defaultMarginInsets.top, left: 0,
        bottom: 0, right: defaultMarginInsets.right)
    return newInsets
}

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

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) {
    // Perform any setup necessary in order to update the view.

    // If an error is encoutered, use NCUpdateResult.Failed
    // If there's no update required, use NCUpdateResult.NoData
    // If there's an update, use NCUpdateResult.NewData

    completionHandler(NCUpdateResult.NewData)
}

}

0 个答案:

没有答案