qwertyuioas8
qwertyuioas8

Reputation: 5

How to pass a label through segue?

I have a utility application for iOS that I am building. It's an anagram game that tracks how many times the user guesses, guesses correctly, and guesses incorrectly. I keep track of how many times the guesses are (in)correct and want to display the data on the flipside view controller. However, with the following code, nothing is changing when the segue occurs. What am I doing wrong?

Here is my AnagramsMainViewController.m:

#import "AnagramsMainViewController.h"


@interface AnagramsMainViewController ()

@end

@implementation AnagramsMainViewController

@synthesize jumbledWord, jumbledWordLabel, unjumbledWord, unjumbledWordLabel, textField, statusLabel, timesGuessed, timesGuessedCorrect, timesGuessedIncorrect;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(AnagramsFlipsideViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showAlternate"]) {
         AnagramsFlipsideViewController *controller = [segue destinationViewController];
        controller.timesGuessedCorrectLabel.text = [NSString stringWithFormat:@"Times guessed correct: %d", timesGuessedCorrect];
        controller.timesGuessedIncorrectLabel.text = [NSString stringWithFormat:@"Times guessed incorrect: %d", timesGuessedIncorrect];
        controller.totalTimesGuessedLabel.text = [NSString stringWithFormat:@"Total times guessed: %d", timesGuessed];
        [[segue destinationViewController] setDelegate:self];
    }
}

Here is my AnagramsFlipsideViewController.h

#import <UIKit/UIKit.h>

@class AnagramsFlipsideViewController;

@protocol AnagramsFlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(AnagramsFlipsideViewController *)controller;
@end

@interface AnagramsFlipsideViewController : UIViewController

@property (weak, nonatomic) id <AnagramsFlipsideViewControllerDelegate> delegate;
@property (strong, nonatomic) IBOutlet UILabel *totalTimesGuessedLabel;
@property (strong, nonatomic) IBOutlet UILabel *timesGuessedCorrectLabel;
@property (strong, nonatomic) IBOutlet UILabel *timesGuessedIncorrectLabel;


- (IBAction)done:(id)sender;

@end

(I have synthesized these labels in AnagramsFlipsideViewController.m)

Upvotes: 0

Views: 241

Answers (1)

Duncan C
Duncan C

Reputation: 131408

What you're doing wrong is trying to set the contents of views in another view controller. Don't do that.

It violates the encapsulation of the other view controller, so it's a bad idea from an Object Oriented Design perspective, and it also sometimes just doesn't work (like in this case.)

Instead, create NSString or NSInteger properties for the values you need to pass (e.g. totalTimesGuessed, timesGuessedCorrect, timesGuessedInCorrect, and set THOSE in your prepareForSegue method.

Then in your AnagramsFlipsideViewController's viewWillAppear method, take those string values and install them in your labels.

Upvotes: 1

Related Questions