0

Object: change the label's text in ViewControllerB

(In the ViewControllerA.m)

- (IBAction)ReturnToMenu
{
    ViewControllerB *ViewControllerB =
    [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
    ViewControllerB.scorelabel.text=@"WHY IS IT NOT WORKING";
    [self presentViewController:GameOverViewController animated:YES completion:nil];
}

Well, this isn't working. I've exhausted all my options. This is such an elementary question, but please help me out.

P.S. @"Why is it not working" is just a temporary thing, a variable from ViewControllerA is supposed to take its place.

P.P.S. I tried out Passing Data between View Controllers, but it didn't work. I'm not sure what I'm doing wrong here

Community
  • 1
  • 1
Gyuhyeon Lee
  • 878
  • 1
  • 9
  • 20
  • Why didn't you comment in the previous question you'd asked? Did you try what I had suggested? – Neeku Dec 01 '13 at 13:11
  • Possible duplicate of your own question: http://stackoverflow.com/questions/20311142/how-to-access-variables-of-other-classes-in-objective-c-ios/20311255#20311255 – Neeku Dec 01 '13 at 13:12
  • @Neeku Sorry, It's just that the post was filled with negativity and I really didn't want to go back there :( And yes, as you can see, I tried exactly what you told me. It is not working. – Gyuhyeon Lee Dec 01 '13 at 13:13
  • It's not what I said. Just try making an instance of the ViewController, away from the storyboard. – Neeku Dec 01 '13 at 13:15
  • Just making an instance of the viewcontroller didn't seem to work. Doesn't it just make "ANOTHER" instance? That is, I am playing with another object's variable, not the actual view that I generated? – Gyuhyeon Lee Dec 01 '13 at 13:17
  • edit: Thanks, I think the problem wasn't in instantiating, but in accessing the label directly. It's not supposed to work for some reason? – Gyuhyeon Lee Dec 01 '13 at 13:21
  • Check out my answer. That way, you'll always have only one instance, and the property must be passed safely. – Neeku Dec 01 '13 at 13:36

3 Answers3

0

You should not try to manipulate another view controller's views directly. It's bad design, and in many cases (like yours) it doesn't work.

Instead of setting a label's text, add a string property "score". Set that after instantiating the VC.

Then, in "ViewController8"s viewWillAppear method, install the score property into the label.

Problem solved.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

Here I present a modal view controller from my main view controller to show locations which I previously got from foursquare api.

    LocationPickerVC *vc = (LocationPickerVC*)[self.storyboard instantiateViewControllerWithIdentifier:@"modal"];
    if([vc respondsToSelector:@selector(setLocations:) ])
            vc.locations = items;
    [self.formSheetController presentViewController:vc animated:YES completion:nil];

[EDIT]

Make sure you set storyboard id in your storyboard (for the code below it is "modal"). Also my LocationPickerVC has this interface.

    @interface LocationPickerVC : UIViewController<UITableViewDataSource, UITableViewDelegate>
    @property(strong, nonatomic) NSMutableArray *locations;
    @end
-1

OK, try this:

In your ViewController1.h that the property score is declared, add this:

+ (ViewController1 *) sharedController;

In your ViewController1.m add this method:

+ (ViewController1 *)sharedController
{
    static ViewController1* staticVar = nil;

    if (staticVar == nil)
        staticVar = [[ViewController1 alloc] init];
    return staticVar;
}

Now, where ever in other ViewControllers you need something from ViewController1, import the header, and then get the property like this:

[[ViewController1 sharedController] score]

Neeku
  • 3,646
  • 8
  • 33
  • 43
  • 1
    Sorry for the late reply, I was busy uploading it to the app store (although it might get rejected heh). Thanks, it worked! I didn't think about the class method, I probably should read a book from the start when I have the time. Have a nice day! – Gyuhyeon Lee Dec 01 '13 at 16:03
  • syntax error and type mismatch: `[[ViewController1 sharedController] score] = @"I hope it works this time!";` – vikingosegundo Dec 02 '13 at 20:48
  • @vikingosegundo Edit applied. – Neeku Dec 03 '13 at 07:32
  • this is still an syntax error `[[ViewController1 sharedController] score] = 100;` – vikingosegundo Dec 03 '13 at 09:26