swift - How to save segmented control choice -
i have added segmented control in project set notification frequency (like daily, weekly, ecc..). don't understand how save user choice , how set notification on choice. have addcontroller user can insert reminders , in controller want set frequency notification repeat. code is:
@ibaction func salva(sender: uibutton) { if fieldnomemedicina.text.isempty && fielddata.text.isempty && fielddosaggio.text.isempty{ //alertview che avverte l'utente che tutti 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 time string //format date var dateformatter = nsdateformatter() dateformatter.dateformat = "dd-mm-yyyy hh:mm" //format style. browse online format fits needs. var date = dateformatter.datefromstring(stringdate) //date nsdate. var localnotification = uilocalnotification() localnotification.category = "first_category" localnotification.firedate = date localnotification.alertbody = "take 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 error: use of unresolved identifier 'notification'in func frequencycontrol.
your problem create localnotification
after user clicks button (which design choice). means can't store information in before, isn't necessary - can ask uisegmentedcontrol
current value is.
you need transfer block of code:
if(segmentedcontrol.selectedsegmentindex == 0) { notification.repeatinterval = 0; } ...
inside salva
function. , while you're @ it, convert if
statements switch
- cleaner. like:
var localnotification = uilocalnotification() switch segmentedcontrol.selectedsegmentindex { case 0: localnotification.repeatinterval = 0; case 1: localnotification.repeatinterval = .calendarunitday; ... }
Comments
Post a Comment