0

So I've been searching for a solution that I can understand and apply to my project, but no luck.

I have a popup window(viewcontroller1) with a UItextfield. In this field, the user should enter a text that appends into an array in another view controller(viewcontroller2) I've tried something with prepare(for segue) but that will also transfer the user back to viewcontroller1, right?

So how would I append and save this UItextField data to the array in viewcontroller2, without automatically moving the user when he/she hits enter?

5 Answers 5

1

You can make use of delegates or notifications to achieve this.

You need to call the delegate function as the user finishes editing(may be a "save" Button will give you the callback that that the user has finished entering). For more information on delegate refer this.

Sign up to request clarification or add additional context in comments.

Comments

1

Make your array variable as 'static'.

static var arrayElements = [String]()

You can modify this array from anywhere in your project by using

viewcontroller2.arrayElements

Comments

1

This can be done using NotificationCenter. In your ViewController2 add observer (Swift 3 Version):

func viewDidLoad () {

  NotificationCenter.default.addObserver(self, selector: #selector(arrayUpdateNotificationReceived :), name: "TextFieldInputChangedNotification", object: nil)
}

func arrayUpdateNotificationReceived (notification : Notification) {
      yourArray.append(notification.object);
}

In ViewControlller1 :

When user clicks add on popup :

@IBAction func yourButtonAction(sender : UIButton )  {

   let inputText = inputTextField.text;

   notificationCenter.post(name: "TextFieldInputChangedNotification", object: inputText)
}

Don't forget to remove observer in your ViewController 2 :

deinit {
        NotificationCenter.default.removeObserver(self)
    }

3 Comments

I'm sure that is correct but I'm trying to do this in swift and if I'm not mistaken that's objective C? Thank you anyway!
Here's a swift 3 version :)
Thank you for translating this to swift, seems like im using swift 4 and the somewhere in the NotificationCenter the code is depricated and doesnt work. But anyways thank you! :)
0

Unless you need to use the data entered into the UITextField immediately in ViewController1, you can use prepare(for segue). This function doesn't actually start the segue, but it is called by the system automatically when a segue is about to happen either by selecting a UI element that has been connected to a segue in InterfaceBuilder of by reaching a piece of code, which calls self.perform(segue).

Comments

0

Once you enter the text from the textfield, do you want the user to be taken to ViewController2 or remain in ViewController1?

If you are looking to take the user to the 2nd view controller after entering this data then you can pass this string in a segue to the 2nd view controller. To do this, in the ViewController2.h file, add a new property to the 2nd view controller as a string. When you do your performSegueWithIdentifier pass it the string you entered as a sender, and in the prepare for segue, set viewController2 as your segue.destinationViewController, and then you can set viewController2.string to the sender.

If you are just looking to have it appended, I suggest creating a new class of a shared instance or singleton object. This instance or object would have an array that would maintain it's values irrespective of which view controller you've entered. You can have a look at this question's preferred answer, might help: Objective-C Defining a Global Array for use by several ViewControllers

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.