swift - 如何保存分段控制选择

标签 swift segmentedcontrol

我在我的项目中添加了分段控件来设置通知频率(例如每日、每周、ecc..)。我不明白如何保存用户选择以及如何设置此选择的通知。我有一个 AddController,用户可以在其中插入提醒,在这个 Controller 中我还想设置通知重复的频率。代码是:

@IBAction func salva(sender: UIButton) {
    if fieldNomeMedicina.text.isEmpty &&
        fieldData.text.isEmpty &&
        fieldDosaggio.text.isEmpty{
            //alertView che avverte l'utente che tutti i campi sono obbligatori
            //return

    }

    var therapy = PillsModel(nomeMedicinaIn: fieldNomeMedicina.text,
        dosaggioIn : fieldDosaggio.text,
        dataIn: fieldData.text
        )

    profilo.therapyArra.append(therapy)
    DataManager.sharedInstance.salvaArray()
    DataManager.sharedInstance.detail.pillsTable.reloadData()

    dismissViewControllerAnimated(true, completion: nil)

    let stringDate = fieldData.text//get the time string

    //format date
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy hh:mm" //format style. Browse online to get a format that fits your needs.
    var date = dateFormatter.dateFromString(stringDate)
    //date is your NSdate.


    var localNotification = UILocalNotification()
    localNotification.category = "FIRST_CATEGORY"
    localNotification.fireDate = date
    localNotification.alertBody = "Take your medicine:" + " " + fieldNomeMedicina.text + " " + fieldDosaggio.text
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1


    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)


}

@IBAction func frequencyControl(sender: UISegmentedControl) {

    if(segmentedControl.selectedSegmentIndex == 0)
    {
        notification.repeatInterval = 0;
    }
    else if(segmentedControl.selectedSegmentIndex == 1)
    {
        notification.repeatInterval = .CalendarUnitDay;
    }
    else if(segmentedControl.selectedSegmentIndex == 2)
    {
        notification.repeatInterval = .CalendarUnitWeekday;

    }
    else if(segmentedControl.selectedSegmentIndex == 3)
    {
        notification.repeatInterval = .CalendarUnitMonth;

    }
    else if(segmentedControl.selectedSegmentIndex == 4)
    {
        notification.repeatInterval = .CalendarUnitMinute;

    }

}


func drawAShape(notification:NSNotification){
    var view:UIView = UIView(frame:CGRectMake(10, 10, 100, 100))
    view.backgroundColor = UIColor.redColor()

    self.view.addSubview(view)

}

func showAMessage(notification:NSNotification){
    var message:UIAlertController = UIAlertController(title: "A Notification Message", message: "Hello there", preferredStyle: UIAlertControllerStyle.Alert)
    message.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

    self.presentViewController(message, animated: true, completion: nil)

}

我遇到错误:在函数频率控制中使用了无法解析的标识符“通知”。

最佳答案

您的问题是,您仅在用户单击按钮后才创建 localNotification (这是一个很好的设计选择)。这意味着您之前无法在其中存储信息,但这不是必需的 - 您可以随时询问 UISegmentedControl 其当前值是多少。

您基本上需要传输此代码块:

if(segmentedControl.selectedSegmentIndex == 0)
{
    notification.repeatInterval = 0;
}
...

salva函数内。当您执行此操作时,将 if 语句转换为 switch - 这样更简洁。它看起来像:

var localNotification = UILocalNotification()
switch segmentedControl.selectedSegmentIndex {
    case 0:
        localNotification.repeatInterval = 0;
    case 1:
        localNotification.repeatInterval = .CalendarUnitDay;
    ...
}

关于swift - 如何保存分段控制选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32036269/

相关文章:

swift - 无法以编程方式将 Storyboard 中的颜色集更改为 xcassets 目录中的颜色

iOS:本地化多行字符串文字

ios - SwiftUI也在ScrollView中创建自定义分段控件

swiftui - 带有 onTapGesture 的分段选取器不响应点击

ios - 删除特定对象 Realm Swift

ios - 从 UINavigationController 堆栈转出到 UITabBarController

ios - 如何使用 Swift 将 Payfort SDK 集成到 iOS

ios - 拖入自定义分段控件时意外调用cancelTracking

ios - 基本分段控制不起作用

ios - 根据当前显示的 UICollectionViewCell 更改 UISegmentedControl.selectedIndex 值