swift - How to add a notification? -
i want add local notification in project. in detailviewcontroller
, have 2 labels date
, hour
.
is possible add notification based on date
, hour
? if yes, can explain how set it?
code:
detailviewcontroller :
import uikit class detailviewcontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate { // mark: - outlet @iboutlet var imageprofilo: uiimageview! @iboutlet var labelnome: uilabel! @iboutlet var pillstable: uitableview! // mark: - variabili var profilo: profilomodel! // mark: - metodi standard del controller override func viewdidload() { super.viewdidload() pillstable.datasource = self pillstable.delegate = self imageprofilo.layer.cornerradius = 30 datamanager.sharedinstance.detail = self if let test = profilo { //title = profilo!.nome labelnome.text = profilo!.nome imageprofilo.image = profilo!.immagine if profilo!.immagine.size.width > profilo!.immagine.size.height { imageprofilo.image = uiimage(cgimage: profilo!.immagine.cgimage, scale: 1.0, orientation: uiimageorientation.right) } else { imageprofilo.image = profilo!.immagine } } else { if !datamanager.sharedinstance.storage.isempty { profilo = datamanager.sharedinstance.storage[0] //title = profilosos.nome labelnome.text = profilo.nome imageprofilo.image = profilo.immagine if profilo.immagine.size.width > profilo.immagine.size.height { imageprofilo.image = uiimage(cgimage: profilo.immagine.cgimage, scale: 1.0, orientation: uiimageorientation.right) } else { imageprofilo.image = profilo.immagine } } else { return } } } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // mark: uitableview func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return profilo.therapyarra.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! pillscell var terapia = profilo.therapyarra[indexpath.row] cell.nomemedicina.text = terapia.nomemedicina cell.data.text = terapia.data cell.ora.text = terapia.ora cell.dosaggio.text = terapia.dosaggio return cell } // mark: - azioni // mark: - metodi // mark: - navigazione override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "terapia" { var cell = sender as! uitableviewcell if let indexpath = self.pillstable.indexpathforrowatpoint(cell.center) { var controller = segue.destinationviewcontroller as! pillsviewcontroller controller.therapy = profilo.therapyarra[indexpath.row] } } else if segue.identifier == "addtherapy" { var controller = segue.destinationviewcontroller as! addpillscontroller controller.profilo = profilo } } }'
pillscell :
'import uikit class pillscell: uitableviewcell { @iboutlet var nomemedicina: uilabel! @iboutlet var ora: uilabel! @iboutlet var data: uilabel! @iboutlet var dosaggio: uilabel! override func awakefromnib() { super.awakefromnib() // initialization code } override func setselected(selected: bool, animated: bool) { super.setselected(selected, animated: animated) // configure view selected state } }
pillsmodel :
import uikit class pillsmodel: nsobject, nscoding { var nomemedicina :string! var data :string! var ora :string! var dosaggio :string! init(nomemedicinain:string, datain:string, orain:string, dosaggioin:string) { nomemedicina = nomemedicinain ora = orain data = datain dosaggio = dosaggioin } internal required init(coder adecoder: nscoder) { self.nomemedicina = adecoder.decodeobjectforkey("nomemedicina") as! string self.ora = adecoder.decodeobjectforkey("ora") as! string self.data = adecoder.decodeobjectforkey("data") as! string self.dosaggio = adecoder.decodeobjectforkey("dosaggio") as! string } func encodewithcoder(encoder: nscoder) { encoder.encodeobject(self.nomemedicina, forkey: "nomemedicina") encoder.encodeobject(self.ora, forkey: "ora") encoder.encodeobject(self.data, forkey: "data") encoder.encodeobject(self.dosaggio, forkey: "dosaggio") } }
addpillscontroller :
import uikit class addpillscontroller: uiviewcontroller, uitextfielddelegate, cameramanagerdelegate { @iboutlet var fieldnomemedicina: uitextfield! @iboutlet var fielddata: uitextfield! @iboutlet var fieldora: uitextfield! @iboutlet var fielddosaggio: uitextfield! var profilo: profilomodel! override func viewdidload() { super.viewdidload() fieldnomemedicina.delegate = self fielddata.delegate = self fieldora.delegate = self // vista accessoria per la tastiera var keyboardtoolbar = uitoolbar(frame: cgrectmake(0, 0, self.view.bounds.size.width, 44)) keyboardtoolbar.barstyle = uibarstyle.blacktranslucent keyboardtoolbar.backgroundcolor = uicolor.redcolor() keyboardtoolbar.tintcolor = uicolor.whitecolor() var flex = uibarbuttonitem(barbuttonsystemitem: uibarbuttonsystemitem.flexiblespace, target: nil, action: nil) var save = uibarbuttonitem(title: "fatto", style: uibarbuttonitemstyle.done, target: fielddosaggio, action: "resignfirstresponder") keyboardtoolbar.setitems([flex, save], animated: false) fielddosaggio.inputaccessoryview = keyboardtoolbar } func textfieldshouldreturn(textfield: uitextfield) -> bool { textfield.resignfirstresponder() // chiudere la tastiera nei campi di testo return true } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func annulla(sender: uibutton) { dismissviewcontrolleranimated(true, completion: nil) // chiude una modal } @ibaction func salva(sender: uibutton) { if fieldnomemedicina.text.isempty && fielddata.text.isempty && fieldora.text.isempty && fielddosaggio.text.isempty{ //alertview che avverte l'utente che tutti campi sono obbligatori return } var therapy = pillsmodel(nomemedicinain: fieldnomemedicina.text, datain: fielddata.text, orain: fieldora.text, dosaggioin : fielddosaggio.text) profilo.therapyarra.append(therapy) datamanager.sharedinstance.salvaarray() datamanager.sharedinstance.detail.pillstable.reloaddata() dismissviewcontrolleranimated(true, completion: nil) } }
you should use uilocalnotification
. use date , time create nsdate
object , setup notification this:
let notification = uilocalnotification() notification.firedate = ... // add date here notification.alertbody = "alert alert alert!!!" uiapplication.sharedapplication().schedulelocalnotification(notification)
handle notification in appdelegate overriding didreceivelocalnotification
method:
func application(application: uiapplication, didreceivelocalnotification notification: uilocalnotification) { println(notification.alertbody); // "alert alert alert!!!" }
Comments
Post a Comment