jeudi 25 juin 2015

Access UITextField's text content (swift)

I'm trying to access the content of UITextFields that's nested in a UITableView when a "save" button is pressed. However, debugging (as shown further below) shows that I can access anything about the texfield except its text.....Whatever I type inside it before pressing the button has no impact... I Feel like I should use the UITextField Delegate but don't understand neither why nor how... Can anyone help ?

This is the code i'm using (in Swift)

@IBAction func saveNewGuestButton(sender: UIBarButtonItem) {
    let entity : NSEntityDescription = NSEntityDescription.entityForName("Guest", inManagedObjectContext: self.managedObjectContext)!
    // Initialize Record
    let record : Guest = Guest(entity:entity, insertIntoManagedObjectContext:managedObjectContext)
    // Populate Record
    for i in 0..<3
    {
        let indexPath : NSIndexPath = NSIndexPath(forRow: i, inSection: 0)
        let cell = tableView(self.tableView, cellForRowAtIndexPath: indexPath) as! newGuestProtoCell
        if i==0
        {
            record.guestFirst = cell.newGuestFirstNameTextField.text
            println("indexPath :\(indexPath), recorded : \(record.guestFirst) and TextField : \(cell.newGuestFirstNameTextField.text) + \(cell.newGuestFirstNameTextField). test : \(cell.newGuestFirstNameTextField.defaultTextAttributes) and \(cell.newGuestFirstNameTextField.attributedText) and \(cell.newGuestFirstNameTextField.placeholder) and \(cell.newGuestFirstNameTextField.tag)")
        }
        if i==1
        {
            record.guestLast = cell.newGuestLastNameTextField.text

            println("indexPath :\(indexPath), recorded Last : \(record.guestLast), recorded first : \(record.guestFirst) and textfield : \(cell.newGuestLastNameTextField.text)")
            //newGuest["lastName"] = cell.newGuestLastNameTextField.text
        }
        if i==2 && guestImage != nil
        {

            record.guestImage = UIImagePNGRepresentation(guestImage) as NSData
            println("indexPath :\(indexPath), recorded image: \(record.guestImage.superclass) and UIImageView's image : \(cell.guestImageIV.image)")
        }
    }

The console prints the following, which proves that the problems is not in my tableview, my "for" loop to access it nor the textfield itself (or it's "outlet") :

indexPath : {length = 2, path = 0 - 0}, recorded : and TextField : + >. test : [NSParagraphStyle: Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 4, Tabs ( 28L, 56L, 84L, 112L, 140L, 168L, 196L, 224L, 252L, 280L, 308L, 336L ), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0, NSShadow: NSShadow {0, -1} color = {(null)}, NSFont: font-family: ".HelveticaNeueInterface-Regular"; font-weight: normal; font-style: normal; font-size: 14.00pt, NSColor: UIDeviceRGBColorSpace 0 0 0 1] and Optional() and Optional("First Name") and 0 indexPath : {length = 2, path = 0 - 1}, recorded Last : , recorded first : and textfield : indexPath : {length = 2, path = 0 - 2}, recorded image: Optional(NSMutableData) and UIImageView's image : Optional( size {1500, 1001} orientation 0 scale 1.000000)

See more ..

Aucun commentaire:

Enregistrer un commentaire