2015-04-28 55 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