0

Was wondering if anybody could help on a simple problem

I have a UITextView on one page, where I want to enter text and store it in a string. Then when the next page is accessed, I would like that text to be displayed on that page, either in a new UITextView or some other way.

In my PostViewController.swift file (the first page) to get the input string, I have

@IBOutlet weak var postTextView: UITextView!

I tried making a new UITextView on the other view controller to try and display the string, and had the subsequent code, but can't figure it out from here.

@IBOutlet weak var copyText: UITextView!

let text: NSString = PostViewController().postTextView.text
???copyText = text??? (not sure if this is even close to right)

Any ideas would be appreciated and apologies for the noob question

Tyrone
  • 5
  • 3

1 Answers1

0

In your PostViewController, implement prepareForSegue() to assign the value of its textfield to the variable in the second controller.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "showSecondVC" { // Make sure the segue has this identity in Interface Builder

            let vc = segue.destinationViewController as! SecondVC // Change that to the actual class name

            vc.textFromVC1 = postTextView.text
        }
    }

In your second ViewController:

class SecondviewController: UIViewController {

    var textFromVC1: String!

    @IBOutlet weak var myTextField: UITextField!

    override func viewDidLoad() {
        myTextField.text = textFromVC1
    }
}
Shades
  • 5,568
  • 7
  • 30
  • 48
  • Thanks for the reply. I tried implementing those suggestions into my project, but I'm getting an "unresolved identifier" on vc.postViewText = postTextView.text and I'm not sure why. Then I'm also not sure on when you said to put "var postViewText: String!" in my second VC...do I have to set this equal to "PostViewController().postTextView.text" so it is assigned the string? Thanks! – Tyrone Mar 07 '16 at 05:41
  • @Tyrone no, put the `var postViewText: String!` as like as `class SecondVC : ViewController { var postViewText: String! ` – Jacky Shek Mar 07 '16 at 07:34
  • Read this for more info: http://stackoverflow.com/questions/26207846/pass-data-through-segue/26208831#26208831 – Shades Mar 07 '16 at 15:17
  • @Tyrone Let me know if any more questions – Shades Mar 07 '16 at 17:08
  • @Shades I think I got it down now, except for adding it to the interface builder. What exactly would I do for that? – Tyrone Mar 07 '16 at 17:40
  • @Tyrone There are many tutorials teaching connecting textfields, buttons, etc to your code. Start here: http://stackoverflow.com/questions/32572305/iboutlet-and-ibaction-in-swift – Shades Mar 07 '16 at 18:04
  • @Shades Cool, I figured that out. But am now getting a signal SIGABRT on "let vc = segue.destinationViewController as! GradeViewController" when I am trying to load the 2nd page – Tyrone Mar 07 '16 at 19:04
  • @Shades Could not cast value of type 'UIViewController' (0x10b2cc418) to 'SocialAgent.GradeViewController' (0x1097f6c08). (lldb) – Tyrone Mar 07 '16 at 19:20
  • @Tyrone http://stackoverflow.com/questions/31440964/cant-cast-value-of-type-uiviewcontroller-to-patterndetailviewcontroller – Shades Mar 07 '16 at 19:22
  • @Shades yup that was it, my bad...however the text from the first one did not show in the second. Do I need an IBOutlet for the second textview? – Tyrone Mar 07 '16 at 19:38
  • @Tyrone Where are you trying to display it? If a textfield, then yes, add an IBOutlet, then assign the .text value as textFromVC1 – Shades Mar 07 '16 at 19:47
  • @Shades I added "@IBOutlet weak var testFromPost: UITextView!" (being textFromVC1), then assign it like this? "vc.textFromPost.text = postText.text" ? – Tyrone Mar 07 '16 at 19:52
  • @Tyrone See updated code. Make sure the IBOutlet is connected in Interface Builder – Shades Mar 07 '16 at 19:56