0

i have added a segmented control in my project to set a notification frequency (like daily, weekly, ecc..). I don't understand how to save the user choice and how to set notification on this choice. I have an AddController where user can insert reminders and in this controller i want also to set the frequency for notification repeat. The code is:

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

}

I have the error: use of unresolved identifier 'notification'in func frequencyControl.

Elis
  • 41
  • 1
  • 7

1 Answers1

0

Your problem is that you only create the localNotification after the user clicks the button (which is a good design choice). That means you can't store information in it before, but that isn't necessary - you can always ask the UISegmentedControl what its current value is.

You basically need to transfer this block of code:

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

inside the salva function. And while you're at it, convert the if statements to a switch - this is much cleaner. It would look like:

var localNotification = UILocalNotification()
switch segmentedControl.selectedSegmentIndex {
    case 0:
        localNotification.repeatInterval = 0;
    case 1:
        localNotification.repeatInterval = .CalendarUnitDay;
    ...
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • Hi Glorfindel, thanks for your reply. just a question: i have to remove the `@IBAction` in order to move the code inside the `salva` function? – Elis Aug 16 '15 at 15:00
  • You can remove the `@IBAction` - it has no function anymore. If you do so, be sure to remove the connection in the storyboard as well. – Glorfindel Aug 16 '15 at 16:31
  • Hi Glorfindel, thanks to your indications I set the frequency of the notifications, but I noticed that when I delete a reminder from the list , I get always notifications . How can I set a stop of notifications when a reminder is deleted from the list ? – Elis Aug 19 '15 at 16:57
  • You can cancel notifications with [`cancelLocalNotification:`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instm/UIApplication/cancelLocalNotification:) but you need a reference to it first. You can get all notifications with [`scheduledLocalNotifications`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instp/UIApplication/scheduledLocalNotifications). – Glorfindel Aug 19 '15 at 17:12
  • it is possible to set the cancellation of notifications when i delete the corresponding row of a reminder in the list? – Elis Aug 19 '15 at 18:24
  • Yes, but it is rather complicated. You need to save the identifier of the reminder in the userInfo dictionary of the local notification. Maybe [this](http://stackoverflow.com/questions/7688008/how-to-identify-a-particular-notification-in-ios-sdk) will help. If not, you should post the problems you have in a new question. – Glorfindel Aug 19 '15 at 20:00