2015-04-28 42 views
0

我正在尋找在我的應用程序中的筆記頁添加初始筆記。這是因爲當人們點擊註釋部分時,會有一些關於如何使用它的細節,而不僅僅是一個大的空白屏幕。我不知道在哪裏實施這個。你能幫忙嗎,下面是關於字典的頁面。添加初始筆記

import UIKit 
import MessageUI 

class DetailViewController: UIViewController, MFMailComposeViewControllerDelegate, UITextViewDelegate { 

@IBOutlet weak var tView: UITextView! 
@IBAction func BarButton(sender: UIBarButtonItem) { 
     let textToShare = "" 

     if let myWebsite = NSURL(string: "") 
     { 
      let objectsToShare = [textToShare, myWebsite] 
      let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) 

      self.presentViewController(activityVC, animated: true, completion: nil) 
     } 

    OpenMail() 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    tView.text = (allNotes[currentNoteIndex] as Note).note 
    tView.becomeFirstResponder() 
    // Set controller as swipe gesture recogniser, to allow keyboard dismissal for text box 
    var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "dismissKeyboard") 

    swipe.direction = UISwipeGestureRecognizerDirection.Down 

    self.view.addGestureRecognizer(swipe) 
    self.tView.delegate = self 

} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 
    if tView.text == "" { 
     allNotes.removeAtIndex(currentNoteIndex) 
    } 
    else { 
     (allNotes[currentNoteIndex] as Note).note = tView.text 
    } 
    Note.saveNotes() 
    noteTable?.reloadData() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func configuredMailComposeViewController() -> MFMailComposeViewController { 
    // Open mail controller on screen and prepare with preset values. 
    let mailComposerVC = MFMailComposeViewController() 
    var MessageText: String! 
    MessageText = tView.text 
    mailComposerVC.mailComposeDelegate = self 
    mailComposerVC.setToRecipients([""]) 
    mailComposerVC.setSubject("") 
    mailComposerVC.setMessageBody(MessageText, isHTML: false) 

    return mailComposerVC 
} 

func showSendMailErrorAlert() { 
    // Alert user to email error 
    let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK") 
    sendMailErrorAlert.show() 
} 

// MARK: MFMailComposeViewControllerDelegate Method 
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) { 
    controller.dismissViewControllerAnimated(true, completion: nil) 
} 


func OpenMail() { 
    //Function to open mail composer on screen 

    let mailComposeViewController = configuredMailComposeViewController() 
    if MFMailComposeViewController.canSendMail() { 
     self.presentViewController(mailComposeViewController, animated: true, completion: nil) 
    } else { 
     self.showSendMailErrorAlert() 
    } 

} 
func dismissKeyboard() { 
    // Dismiss keyboard for textfield 
    self.tView.resignFirstResponder() 

} 


} 

note.swift

import UIKit 

var allNotes:[Note] = [] 
var currentNoteIndex:NSInteger = -1 
var noteTable:UITableView? 

let KAllNotes:String = "notes" 


class Note: NSObject { 
var date:String 
var note:String 

override init() { 
    date = NSDate().description 
    note = "" 
} 

func dictionary() -> NSDictionary { 
    return ["note":note, "date":date] 
} 

class func saveNotes() { 
    var aDictionaries:[NSDictionary] = [] 
    for (var i:NSInteger = 0; i < allNotes.count; i++) { 
     aDictionaries.append(allNotes[i].dictionary()) 
    } 
    NSUserDefaults.standardUserDefaults().setObject(aDictionaries, forKey: KAllNotes) 
    //  aDictionaries.writeToFile(filePath(), atomically: true) 
} 

class func loadnotes() { 
    allNotes.removeAll(keepCapacity: true) 
    var defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults() 
    var savedData:[NSDictionary]? = defaults.objectForKey(KAllNotes) as? [NSDictionary] 
    //  var savedData:NSArray? = NSArray(contentsOfFile: filePath()) 
    if let data:[NSDictionary] = savedData { 
     for (var i:NSInteger = 0; i < data.count; i++) { 
      var n:Note = Note() 
      n.setValuesForKeysWithDictionary(data[i] as [NSObject : AnyObject]) 
      allNotes.append(n) 
     } 
    } 
} 

class func filePath() -> String { 
    var d:[String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] 
    if let directories:[String] = d { 
     var docsDirectory:String = directories[0] 
     var path:String = docsDirectory.stringByAppendingPathComponent("\(KAllNotes).notes") 
     return path; 
    } 
    return "" 
} 

}預先

由於 薩姆

回答

1

添加NSUserDefault布爾存儲初始音符是否應該被顯示,例如該應用程序已第一次啓動。然後相應地加載一個初始音符。添加音符或刪除初始音符時,請相應地更改布爾值,以便下次不顯示初始音符。

您還可以用初始註釋初始化數據庫。從代碼中不清楚註釋是如何保存的,但是這種方法可能依賴於上面的NSUserDefault方法,除了它可以在AppDelegate或其他東西中完成。

例如:

let InitialSetupComplete = "InitialSetupComplete" // Note: I would define this at the top of a file 
let defaults = NSUserDefaults.standardUserDefaults() 
if defaults.boolForKey(InitialSetupComplete) { 
    // Show initial note 
} 

// Later on when the note is deleted, or modified (or immediately after initial note loaded into the database, see below) 
defaults.setBool(true, forKey: InitialSetupComplete) 

會更容易/清潔劑只是在應用程序委託的初始注初始化數據庫(如內的applicationDidFinishLaunching調用),所以您的視圖控制器沒有算出這個。類似的代碼,除了在初始註釋被保存到數據庫之後馬上使用setBool。我對這個問題的數據庫一無所知,因此無法提供比此更詳細的示例。希望這可以幫助。

+0

謝謝Shim,仍然有一些問題已經將它添加到筆記VC中,並在應用程序委託中嘗試了它,但沒有看到最初的註釋。在這個鏈接是我關於筆記的其他一些快速文件。可以幫助你瞭解如何保存http://stackoverflow.com/questions/29831597/notes-will-not-delete – ShizukaNaHaji

+0

你保存所有的筆記在NSUserDefaults? – shim

+0

這是一件壞事嗎? – ShizukaNaHaji