使用UITextView

时间:2016-08-19 20:59:53

标签: swift uitextview

我正在创建一个应用程序,它将有几个部分连接到解析,所以我想我会尝试为解析函数创建一个自定义类。

此地址簿标签是第一次尝试。

我遇到了一些我认为会如此简单的事情,但经过10个小时的研究,我正转向这里。

这是ViewController

var addressUUID = NSUUID().UUIDString


class AddViewController : UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


@IBOutlet weak var addressImage : UIImageView!
@IBOutlet weak var nameField : UITextField!
@IBOutlet weak var lastNameField : UITextField!
@IBOutlet weak var phoneField : UITextField!
@IBOutlet weak var emailField : UITextField!
@IBOutlet weak var addressCityField : UITextField!
@IBOutlet weak var addressCountryField : UITextField!

@IBOutlet weak var nameFieldLabel : UILabel!
@IBOutlet weak var lastNameFieldLabel : UILabel!
@IBOutlet weak var phoneFieldLabel : UILabel!
@IBOutlet weak var emailFieldLabel : UILabel!
@IBOutlet weak var addressCityFieldLabel : UILabel!
@IBOutlet weak var addressCountryFieldLabel : UILabel!
@IBOutlet weak var doneButton: UIButton!

@IBOutlet weak var scrollView: UIScrollView!
var scrollViewHeight : CGFloat = 0

var person : Person?
var parse : ParseData?
// creating frame for keyboard to force scroll view up
var keyboard = CGRect()

@IBAction func addButtonPressed(sender : UIButton) {
    //NSLog("Button pressed")


    print ("\(nameField.text)")


    parse = ParseData.init(firstName: nameField.text!)

    // Saves data to Parse class, regardless of if new or updated record
    do {

        try parse!.setFirstName(nameField.text!)
        try parse!.setLastName(lastNameField.text!)
        try parse!.setPhone(phoneField.text!)
        try parse!.setEmail(emailField.text!)
        try parse!.setAddressCity(addressCityField.text!)
        try parse!.setAddressCountry(addressCountryField.text!)
        try parse!.setAddressImage(addressImage.image!)
        try parse!.setUUID(addressUUID)

    } catch let error as PersonValidationError {
        var errorMsg = ""

        switch(error) {
        case .InvalidFirstName:
            errorMsg = "Invalid first name"
        case .InvalidAddressCity:
            errorMsg = "Invalid City"
        case .InvalidEmail:
            errorMsg = "Invalid email address"
        case .InvalidPhone:
            errorMsg = "Invalid phone number"
        case .InvalidAddressImage:
            errorMsg = "Invalid Image"
        case .InvalidAddressCountry:
            errorMsg = "Invalid Country"
        }
        let alert = UIAlertController(title: "Error", message: errorMsg, preferredStyle: .Alert)

        alert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: nil))

        self.presentViewController(alert, animated: true, completion: nil)
    } catch {

    }

    if person == nil
    {
        parse!.saveAddressToParse()
    } else {
        parse!.updateAddressToParse()
    }

如上所述,错误是"尝试"

的第一行

奇怪的是发送给Person类的数据工作正常。 此外,当我在这个VC上使用Parse函数时,它可以工作(尽管有修改后的代码)

这是ParseData类

import Foundation
import Parse


enum ParseValidationError : ErrorType {
    case InvalidFirstName
    case InvalidAddressCity
    case InvalidPhone
    case InvalidEmail
    case InvalidAddressCountry
    case InvalidAddressImage
}

// class ParseData : PFObject, PFSubclassing

class ParseData : PFObject, PFSubclassing
{
    private(set) var firstName : String?
    private(set) var lastName : String?
    private(set) var addressCity : String?
    private(set) var addressCountry : String?
    private(set) var phone : String?
    private(set) var email : String?
    private(set) var uuid : String?
    private(set) var addressImageFile : UIImage?

    var person : Person?

    init?(firstName fn: String) {
        super.init()
        do {
            try setFirstName(fn)
        } catch {
            return nil
        }

    }
    static func parseClassName() -> String {
        return "ParseData"
    }

    func saveAddressToParse () {

        print ("saveToParse function begins")

        let savedAddressObject = PFObject(className: "addressBook")

        savedAddressObject["firstName"]          = self.firstName!
        savedAddressObject["lastName"]           = self.lastName!
        savedAddressObject["phone"]              = self.phone!
        savedAddressObject["email"]              = self.email!
        savedAddressObject["addressCity"]        = self.addressCity!
        savedAddressObject["addressCountry"]     = self.addressCountry!
        savedAddressObject["username"]           = PFUser.currentUser()!.username
        savedAddressObject["uuid"]               = addressUUID
        savedAddressObject["entryFrom"]          = "Divelog New"

        let addressBookImageData = UIImageJPEGRepresentation(self.addressImageFile!, 0.5)
        let addressBookImageFile = PFFile(name: "addressBookImage.jpg", data: addressBookImageData!)
        savedAddressObject ["addressBookImage"] = addressBookImageFile

        savedAddressObject.pinInBackground()
        savedAddressObject.saveEventually()
    }


    func updateAddressToParse () {

        print ("updateToParse function begins")

        let updateAddressQuery = PFQuery(className: "addressBook")
        updateAddressQuery.whereKey("uuid", equalTo: person!.uuid!)
        updateAddressQuery.getFirstObjectInBackgroundWithBlock {(objects: PFObject?, error: NSError?) -> Void in

            if error == nil {

                if let updateAddressObject = objects {

                    updateAddressObject.setValue(self.firstName!, forKey: "firstName")
                    updateAddressObject.setValue(self.lastName!, forKey: "lastName")
                    updateAddressObject.setValue(self.phone!, forKey: "phone")
                    updateAddressObject.setValue(self.email!, forKey: "email")
                    updateAddressObject.setValue(self.addressCity!, forKey: "addressCity")
                    updateAddressObject.setValue(self.addressCountry!, forKey: "addressCountry")
                    updateAddressObject.setValue("Divelog Update", forKey: "entryFrom")

                    let addressBookImageData = UIImageJPEGRepresentation(self.addressImageFile!, 0.5)
                    let addressBookImageFile = PFFile(name: "addressImage.jpg", data: addressBookImageData!)
                    updateAddressObject.setValue(addressBookImageFile!, forKey: "addressBookImage")

                    updateAddressObject.pinInBackground()
                    updateAddressObject.saveEventually()
                }
            }
        }
    }

    func setFirstName(fn : String) throws {
        firstName = fn
    }

    func setLastName(ln : String) throws {
        lastName = ln
    }

    func setPhone (ph : String) throws {
        phone = ph
    }

    func setEmail (em : String) throws {
        email = em
    }

    func setAddressCity(adc : String) throws {
        addressCity = adc
    }

    func setAddressCountry(ad : String) throws {
        addressCountry = ad
    }

    func setAddressImage(ai : UIImage) throws {
        addressImageFile = ai
    }

    func setUUID(ui : String) throws {
        uuid = ui
    }

}

需要添加到AppDelete:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    ParseData.registerSubclass()

    Parse.enableLocalDatastore()

以上是更正的答案,并且工作正常。

1 个答案:

答案 0 :(得分:1)

我无法看到,在哪里创建ParseData实例。我明白了,你用

声明了它
var parse : ParseData?

然后在addButtonPressed中使用它。但在两者之间,定义它的代码在哪里?类似的东西:

parse = ParseData()