2015-06-25 87 views
0

我想在我的項目中添加一個本地通知。在DetailViewController中,我有兩個標籤datehour如何添加通知?

是否可以添加基於此datehour的通知?如果是的話,你能解釋一下如何設置它嗎?

代碼:

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 any resources that can be 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 the view for the 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 any resources that can be 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 i 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) 
} 


} 

回答

1

您應該使用UILocalNotification。用你的日期和時間來創建一個NSDate對象,然後設置您的通知是這樣的:

let notification = UILocalNotification() 
notification.fireDate = ... // Add your date here 
notification.alertBody = "Alert alert alert!!!" 
UIApplication.sharedApplication().scheduleLocalNotification(notification) 

處理通知你的AppDelegate通過重寫didReceiveLocalNotification方法:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 
    println(notification.alertBody); // "Alert alert alert!!!" 
} 
+0

materik嗨,我已經添加了上面的代碼在我的AddPillsController之後'dismissViewControllerAnimated(true,completion:nil)' 但我不知道如何設置對象NSDate與字段的值** fieldOra **和** filedData **。我還想在'notification.alertBody'中加上藥物的名稱和劑量。你可以幫我嗎? – Elis

+0

岸上。你可以像下面這樣向你的''alertbody''添加劑量:''alertBody =「取這個藥量:\(劑量)ml」。然後檢查這個答案出一個方法來解析你的日期:http://stackoverflow.com/questions/5979462/problem-combining-a-date-and-a-time-into-a-single-nsdate –

+0

嗨,謝謝爲了您的幫助,我解決了日期問題,但是當我午餐應用程序時,我沒有看到用戶同意的通知消息。當我設置通知時,我看不到警報消息,但只有應用程序圖標中的徽章和通知中心中的消息。應用程序的打開/關閉導致用戶數據丟失。你可以幫我嗎? – Elis