1

I'm not sure what I am missing here but I know it has to be simple. All I want to do is pass the updated UITextField to the other ViewControllers.

I have my important functions in ViewWillAppear so I don't think thats it. New to programming so I've never tried doing this before.

The code below:
The greyViewController is where the user inputs the website of choice and the Pink and Blue View Controllers are where the code is shown. The blue and pink view Controllers call the hexStringFromData function to convert the website to code. What am I missing? I feel like I'm missing an IF statement but not sure where I would put it.

I've experimented with putting one in the greyViewController to set the UITextfield back to the Singleton var aurl to no avail. I'm sorry if its bad etiquette to post so much code, but I don't really know what I'm doing so I wouldn't know how to explain what trying to accomplish without posting it.

import Foundation
import UIKit

var globalHttpUrl: NSURL!
var aurl = NSString(string: "http://")
var url = NSURL(string: "\(aurl)\(globalHttpUrl)")

class SneakyViewController : UIViewController {


override func viewWillAppear(animated: Bool) {

    super.viewWillAppear(animated)
    println(self.title)

    func hexStringFromData(data: NSData) -> String {
        var hexString = NSMutableString()
        var buffer = [UInt8](count:data.length, repeatedValue:0)
        data.getBytes(&buffer, length:data.length)
        for var i=0; i<buffer.count; i++ {
            if i % 4 == 0 && i != 0 {
                hexString.appendString(" ")
            }
            hexString.appendFormat("%02x", buffer[i])
        }
        return hexString
    }    
 }

func hexStringFromData(data: NSData) -> String {
    var hexString = NSMutableString()
    var buffer = [UInt8](count:data.length, repeatedValue:0)
    data.getBytes(&buffer, length:data.length)
    for var i=0; i<buffer.count; i++ {
        if i % 4 == 0 && i != 0 {
            hexString.appendString(" ")
        }
        hexString.appendFormat("%02x", buffer[i])
    }
    return hexString
}  
}

class PinkViewController : SneakyViewController {

@IBOutlet weak var machineCode: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

    let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in

        var myText = NSString(data: data, encoding: NSASCIIStringEncoding)

        dispatch_async(dispatch_get_main_queue()) {

            self.machineCode.text = myText

        }
    }

    //NSASCIIStringEncoding
    task.resume()

}

override func viewWillAppear(animated: Bool) {

    let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in

        var myText = NSString(data: data, encoding: NSASCIIStringEncoding)

        dispatch_async(dispatch_get_main_queue()) {

            self.machineCode.text = myText

        }
    }

    //NSASCIIStringEncoding
    task.resume()

}    
}

class GreyViewController : SneakyViewController, UITextFieldDelegate {

@IBOutlet var neoCodeView: UIView!

@IBOutlet weak var userInput: UITextField!

//var burl = NSString(string: "http://")

@IBAction func buttonPressed(sender: AnyObject) {
}

override func viewDidLoad() {
    super.viewDidLoad()

userInput.clearsOnBeginEditing.description
userInput.clearButtonMode = UITextFieldViewMode.WhileEditing  
}

@IBAction func goToPinkViewController(sender: AnyObject) {

    globalHttpUrl = NSURL(string: self.userInput.text)

}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    self.view.endEditing(true)

}

 func textFieldShouldReturn(textField: UITextField) -> Bool {
   userInput.resignFirstResponder()
    return true 
}

override func viewWillAppear(animated: Bool) {

   func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        self.view.endEditing(true)

    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        userInput.resignFirstResponder()
        return true
    }

    userInput.clearsOnBeginEditing.description
    userInput.clearButtonMode = UITextFieldViewMode.WhileEditing       

}

}

class BlueViewController : SneakyViewController {

@IBOutlet weak var humanCode: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

    let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in
        var length = data.length
        var chunkSize = 1024 * 10
        var offset = 0

        while offset < length {
            var currentChunkSize = length - offset>chunkSize ? chunkSize : length - offset
            var chunk = NSData(bytes: data.bytes + offset, length:currentChunkSize)
            offset += currentChunkSize
            var string = self.hexStringFromData(chunk)
            dispatch_sync(dispatch_get_main_queue()) {
                self.humanCode.text = self.humanCode.text + string
            }
        }
    }


    task.resume()

}

override func viewWillAppear(animated: Bool) {



    let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in

        var myText = NSString(data: data, encoding: NSASCIIStringEncoding)

        dispatch_async(dispatch_get_main_queue()) {

            self.humanCode.text = myText

        }
    }

    //NSASCIIStringEncoding
    task.resume()

}
}

1 Answer 1

0

I think the problem lies with your declaration of url. It will be instantiated when you first run the program, but won't be updated when globalHttpUrl is. I think if you modified your code like below, it would work.

var url: NSURL {
    get {
        return NSURL(string: "\(aurl)\(globalHttpUrl)")
    }
}
3
  • The get command is giving me an error even if I set the url to NSURL = NSURL () {} so I tried doing a didSet and while I don't get any errors, the ViewControllers do not display anything. Never done a didSet or get command before so maybe my syntax is screwy... Sep 18, 2014 at 21:17
  • Sorry, the = was incorrect syntax. I've update my solution and verified that it compiles
    – mtaylor
    Sep 18, 2014 at 22:54
  • Thank you very much killbros for your help! I did some additional reading on getter/setter so I won't forget this. Sep 19, 2014 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.