我想在app实用程序文件中设置用户默认值,并在我想要的任何视图控制器中调用它们。这是今天的小部件:
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
@IBOutlet weak var BitCount: UILabel!
@IBOutlet weak var upCost: UILabel!
var Bits:Double = defualts.double(forKey: "bits")
var bitsPerClick:Double = defualts.double(forKey: "bitsPerClick")
var upgradePrice:Double = defualts.double(forKey: "upgradeCost")
//Click bit
@IBAction func Click(_ sender: Any) {
AppUtility().bitClick()
BitCount.text = "\(Bits)"
}
//Click Upgrade
@IBAction func Upgrade(_ sender: Any) {
AppUtility().upgradeClick()
upCost.text = "Cost \(upgradePrice)"
BitCount.text = "\(Bits)"
}
以下是app实用程序:
import Foundation
import UIKit
let defualts = UserDefaults.standard
var Bits:Double = defualts.double(forKey: "bits")
var bitsPerClick:Double = defualts.double(forKey: "bitsPerClick")
var upgradePrice:Double = defualts.double(forKey: "upgradeCost")
class AppUtility: UIViewController {
public func setDefualts() {
defualts.set(0, forKey: "bits")
defualts.set(1, forKey: "bitsPerClick")
defualts.set(10, forKey: "upgradeCost")
}
public func upgradeClick() {
if Bits >= upgradePrice {
Bits = Bits - upgradePrice
bitsPerClick = bitsPerClick + 1
upgradePrice = upgradePrice * 1.5
}
}
public func bitClick() {
Bits = Bits + bitsPerClick
}
}
当我运行代码时,我的所有标签都显示为空白,因此我假设用户默认值未设置或未正确调用
答案 0 :(得分:1)
您将值存储为整数,但将其读作双精度数。
更改
defualts.set(1.0, forKey: "bitsPerClick")
到
var bitsPerClick:Int = defualts.integer(forKey: "bitsPerClick")
或将它们视为整数
<div class="modal-dialog modal-dialog-centered" style="width: 80em">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="color: white">×</button>
<h4 class="modal-title"><?php echo Yii::t('webposModule.strings','cusRelation.emailTemplate')?></h4>
</div>
<div class="modal-body clearfix" style="width: 80em; overflow-y: auto; max-height: 20%">
<div class="clearfix">
<div class="form-group">
<i><?php echo Yii::t('webposModule.strings','cusRelation.birthdayPackage.chooseList'); ?></i>
<?php
$this->widget('zii.widgets.grid.CGridView',array(
'id' => 'oc-list-email-template',
'dataProvider'=> $dataProvider,
'summaryText' =>'',
'htmlOptions' => array('style' => 'overflow-y: auto; max-height: 40%'),
'emptyText' => Yii::t ('webposModule.strings', 'cusRelation.birthdayPackage.promoList.empty'),
'columns' => array(
array('name'=> 'emailid',
'value'=>'$data["id"]',
'htmlOptions' => array('style' => 'display:none;'),
'headerHtmlOptions'=>array('style' => 'display:none;'),
'filterHtmlOptions'=>array('style' => 'display:none;'),
),
array(
'header' => '',
'id' => 'chkbox',
'name' => 'checkBox',
'selectableRows' => 2,
'class' => 'CCheckBoxColumn',
),
array(
'header'=> Yii::t('webposModule.strings','cusRelation.templateName'),
'name'=> 'templateName',
'value'=>'$data["template_name"]',
'htmlOptions' => array('style'=> 'cursor: pointer'),
),
array(
'header'=> Yii::t('webposModule.strings','cusRelation.subject'),
'name'=> 'subject',
'value'=>'$data["subject"]',
'htmlOptions' => array('style'=> 'cursor: pointer'),
),
),
));
?>
</div>
</div>
<div class="clearfix">
<div class="col-md-3">
<?php echo CHtml::button(Yii::t ('webposModule.strings', 'cusRelation.apply'), array('id'=>'apply-email-template','class'=>'btn btn-default btn-top-rv oc-apply-email-template btn-block','data-dismiss'=>'modal')); ?>
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
试试这个
/// Global Struct
struct globalValues {
static var firstValue : String?
static var secondValue : String?
}
/// Set Value
globalValues.firstValue = "1"
globalValues.secondValue = "2"
/// Get Values
print(globalValues.firstValue ?? "")
print(globalValues.secondValue ?? "")
/// When App is closed Struct will be cleared
/// Save Data for reference in UserDefaults
/// Now in App Delegate which is called for
/// first time Just Assign
/// Userdefaults values to Struct
/// So you can use it -- like
globalValues.firstValue = UserDefaults.standard.value(forKey: "firstValue") as? String
globalValues.secondValue = UserDefaults.standard.value(forKey: "secondValue") as? String
因此,您不需要将任何内容从一个类传递到另一个类,这将节省您的时间和一些代码(编译时间)