【问题标题】:How to save segmented control choice如何保存分段控制选择
【发布时间】:2015-08-16 22:42:47
【问题描述】:

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

@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)

}

我有错误:在 func frequencyControl 中使用未解析的标识符“通知”。

【问题讨论】:

    标签: swift segmentedcontrol


    【解决方案1】:

    您的问题是您仅在用户单击按钮后才创建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;
        ...
    }
    

    【讨论】:

    • 您好 Glorfindel,感谢您的回复。只是一个问题:我必须删除 @IBAction 才能将代码移动到 salva 函数中?
    • 您可以删除@IBAction - 它不再有任何功能。如果这样做,请务必同时删除情节提要中的连接。
    • 嗨 Glorfindel,感谢您的指示,我设置了通知的频率,但我注意到当我从列表中删除提醒时,我总是收到通知。从列表中删除提醒时,如何设置停止通知?
    • 您可以使用cancelLocalNotification: 取消通知,但您需要先引用它。您可以通过scheduledLocalNotifications 获取所有通知。
    • 当我删除列表中相应行的提醒时,可以设置取消通知吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2012-02-16
    相关资源
    最近更新 更多