添加初始注释

时间:2015-04-28 15:24:58

标签: ios swift

我正在寻找在我的应用中添加注释页面的初始注释。这是因为当人们点击笔记部分时,会有一些关于如何使用它的细节,而不仅仅是一个大空屏幕。我不知道在哪里实现这个。你能帮忙吗,下面是它谈论词典的页面。

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 个答案:

答案 0 :(得分: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)

使用app delegate中的初始注释(例如,在applicationDidFinishLaunching中调用)初始化数据库会更容易/更清晰,因此您的视图控制器不必解决这个问题。类似的代码,除了你将初始笔记保存到数据库后立即使用setBool。我对这个问题没有任何关于你的数据库的知识,所以无法真正提供比这更详细的例子。希望这会有所帮助。