1

I have an array players in a view controller SearchViewController, and I also have a multitude of other view controllers, which have text fields such as: textFieldOneand textFieldTwo.

How can I insert the text in textFieldOne to the players array from a view controller other than SearchViewController ?

2
  • Do other view controllers have a reference to the SearchViewController instance? Commented Jul 31, 2013 at 17:20
  • @BrianNickel how would I go about referencing it? Commented Jul 31, 2013 at 17:28

2 Answers 2

2

You're violating the MVC design pattern by having the players array in SearchViewController, and you can see how this complicates your life. If you followed the pattern, you'd have your players array in a separate model class. You should create an instance of this class then pass it around to the various view controllers that need to interact with it. If you use Key-Value Observing (KVO) on the model properties, all your view controllers can be notified when one of them changes it. So if view controller A adds a new player, view controller B could update its table view list of player names, for instance.

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

4 Comments

I'm not entirely sure what you mean, sorry I'm new to object oriented programming. Thanks for your advice, I'll try to read into your suggestion.
Read up on MVC (model/view/controller) and KVO. You're going to have to learn these things or your life in iOS will be difficult.
Thanks, I will do. It seems that this is a lot more complicated to code than I would've thought.
@user2412643 It is only complicated because you must change your design patter to match the MVC paradigm. Once your app is following the MVC design pattern, your problem is fairly simple to solve.
1

The simplest method to do this is using, NSNotification with userInfo.

Add the observer in the view controller, where the updates need to be made.

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateArray:) name:@"updateArray" object:nil];

Write the method,

-(void)updateArray:(NSNotification*)notif
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"updateArray" object:nil];

    NSDictionary *dictionary = [notif userInfo];
    [self.arrPlayers addObject:dictionary];
}

And then, post notification wherever, the updates need to be called from ->

    NSString *notificationName = @"updateArray";
    NSString *key = txtField.text;
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:orientation forKey:key];
    
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:dictionary];

TRY IT !!

3 Comments

This will work but it's not ideal because it doesn't promote separation of model and controller, and it tightly couples the two unrelated view controllers.
To communicate between two different ViewControllers, that may or may not connected to each other, what other solution do u suggest other than the use of NSNotification or Delegate methods..?
Generally it's not the view controllers that are connected to each other, but rather that they are both connected to data. eg: one view controller for adding a contact, one vc for showing it. If you separate the model properly then both view controllers can observe it and respond to changes. Read up on Key Value Observing (KVO), this is a great Apple feature.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.