I'm coding a program in Objective C that uses two different views to input data. One view merely displays the view (PSACurrentGame) and the other inputs the data (PSAEnterScores). I'm using a "prepareforsegue" method to pass data from PSAEnterScores to PSACurrentGame. The problem is the label isn't changing. Any idea why?
PSACurrentGame.m :
#import "PSACurrentGame.h"
#import "PSAGlobal.h"
@interface PSACurrentGame ()
@end
@implementation PSACurrentGame
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view
PSAGlobal *global = [PSAGlobal getInstance];
self.player1.text = global.player1Name;
self.player2.text = global.player2Name;
self.player3.text = global.player3Name;
self.player4.text = global.player4Name;
self.player5.text = global.player5Name;
self.player6.text = global.player6Name;
if ([self.player3.text isEqual: @""]) {
self.player3.hidden = YES;
self.R1P3Phase.hidden = YES;
self.R1P3Score.hidden=YES;
self.R2P3Phase.hidden = YES;
self.R2P3Score.hidden=YES;
self.R3P3Phase.hidden = YES;
self.R3P3Score.hidden=YES;
self.ToP3Phase.hidden = YES;
self.ToP3Score.hidden=YES;
}
if ([self.player4.text isEqual: @""]) {
self.player4.hidden = YES;
self.R1P4Phase.hidden = YES;
self.R1P4Score.hidden=YES;
self.R2P4Phase.hidden = YES;
self.R2P4Score.hidden=YES;
self.R3P4Phase.hidden = YES;
self.R3P4Score.hidden=YES;
self.ToP4Phase.hidden = YES;
self.ToP4Score.hidden=YES;
}
if ([self.player5.text isEqual: @""]) {
self.player5.hidden = YES;
self.R1P5Phase.hidden = YES;
self.R1P5Score.hidden=YES;
self.R2P5Phase.hidden = YES;
self.R2P5Score.hidden=YES;
self.R3P5Phase.hidden = YES;
self.R3P5Score.hidden=YES;
self.ToP5Phase.hidden = YES;
self.ToP5Score.hidden=YES;
}
if ([self.player6.text isEqual: @""]) {
self.player6.hidden = YES;
self.R1P6Phase.hidden = YES;
self.R1P6Score.hidden=YES;
self.R2P6Phase.hidden = YES;
self.R2P6Score.hidden=YES;
self.R3P6Phase.hidden = YES;
self.R3P6Score.hidden=YES;
self.ToP6Phase.hidden = YES;
self.ToP6Score.hidden=YES;
}
if ([self.player4.text isEqual: @""] && [self.player5.text isEqual:@""] && [self.player6.text isEqual:@""]) {
self.R1List2.hidden = YES;
self.R2List2.hidden = YES;
self.R3List2.hidden = YES;
self.ToList2.hidden = YES;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
PSACurrentGame.h:
#import <UIKit/UIKit.h>
@interface PSACurrentGame : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *player1;
@property (strong, nonatomic) IBOutlet UILabel *player2;
@property (strong, nonatomic) IBOutlet UILabel *player3;
@property (strong, nonatomic) IBOutlet UILabel *player4;
@property (strong, nonatomic) IBOutlet UILabel *player5;
@property (strong, nonatomic) IBOutlet UILabel *player6;
@property (strong, nonatomic) IBOutlet UILabel *R1P1Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P1Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P1Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P1Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P1Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P1Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP1Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP1Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P2Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P2Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P2Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P2Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P2Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P2Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP2Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP2Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P3Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P3Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P3Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P3Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P3Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P3Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP3Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP3Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P4Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P4Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P4Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P4Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P4Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P4Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP4Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP4Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P5Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P5Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P5Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P5Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P5Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P5Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP5Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP5Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P6Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P6Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P6Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P6Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P6Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P6Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP6Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP6Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1List2;
@property (strong, nonatomic) IBOutlet UILabel *R2List2;
@property (strong, nonatomic) IBOutlet UILabel *R3List2;
@property (strong, nonatomic) IBOutlet UILabel *ToList2;
@end
PSAEnterScores.m:
#import "PSAEnterScores.h"
#import "PSAGlobal.h"
#import "PSACurrentGame.h"
@interface PSAEnterScores ()
@end
@implementation PSAEnterScores
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
PSAGlobal *global = [PSAGlobal getInstance];
self.player1Name.text = global.player1Name;
self.player2Name.text = global.player2Name;
self.player3Name.text = global.player3Name;
self.player4Name.text = global.player4Name;
self.player5Name.text = global.player5Name;
self.player6Name.text = global.player6Name;
if ([self.player3Name.text isEqualToString:@""] == true) {
self.player3Name.hidden = true;
self.player3txt.hidden = true;
}
if ([self.player4Name.text isEqualToString:@""] == true) {
self.player4Name.hidden = true;
self.player4txt.hidden = true;
}
if ([self.player5Name.text isEqualToString:@""] == true) {
self.player5Name.hidden = true;
self.player5txt.hidden = true;
}
if ([self.player6Name.text isEqualToString:@""] == true) {
self.player6Name.hidden = true;
self.player6txt.hidden = true;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"enterScoresOk"] == true) {
PSACurrentGame *pass = (PSACurrentGame *)segue.destinationViewController;
//Moving scores from round two to round three.
pass.R3P1Score.text = pass.R2P1Score.text;
pass.R3P1Phase.text = pass.R2P1Phase.text;
pass.R3P2Score.text = pass.R2P2Score.text;
pass.R3P2Phase.text = pass.R2P2Phase.text;
pass.R3P3Score.text = pass.R2P3Score.text;
pass.R3P3Phase.text = pass.R2P3Phase.text;
pass.R3P4Score.text = pass.R2P4Score.text;
pass.R3P4Phase.text = pass.R2P4Phase.text;
pass.R3P5Score.text = pass.R2P5Score.text;
pass.R3P5Phase.text = pass.R2P5Phase.text;
pass.R3P6Score.text = pass.R2P6Score.text;
pass.R3P6Phase.text = pass.R2P6Phase.text;
//Moving scores from round 1 to round 2
pass.R2P1Score.text = pass.R1P1Score.text;
pass.R2P2Phase.text = pass.R1P1Phase.text;
pass.R2P2Score.text = pass.R1P2Score.text;
pass.R2P2Phase.text = pass.R1P2Phase.text;
pass.R2P3Score.text = pass.R1P3Score.text;
pass.R2P3Phase.text = pass.R1P3Phase.text;
pass.R2P4Score.text = pass.R1P4Score.text;
pass.R2P4Phase.text = pass.R1P4Phase.text;
pass.R2P5Score.text = pass.R1P5Score.text;
pass.R2P5Phase.text = pass.R1P5Phase.text;
pass.R2P6Score.text = pass.R1P6Score.text;
pass.R2P6Phase.text = pass.R1P6Phase.text;
//Setting Round 1 Scores and Phase
pass.R1P1Score.text = self.player1txt.text;
pass.R1P2Score.text = self.player2txt.text;
pass.R1P3Score.text = self.player3txt.text;
pass.R1P4Score.text = self.player4txt.text;
pass.R1P5Score.text = self.player5txt.text;
pass.R1P6Score.text = self.player6txt.text;
pass.R1P1Phase.text = pass.ToP1Phase.text;
pass.R1P2Phase.text = pass.ToP2Phase.text;
pass.R1P3Phase.text = pass.ToP3Phase.text;
pass.R1P4Phase.text = pass.ToP4Phase.text;
pass.R1P5Phase.text = pass.ToP5Phase.text;
pass.R1P6Phase.text = pass.ToP6Phase.text;
//Setting the total's score and phase.
//Setting Player 1's score and phase.
pass.ToP1Score.text = [NSString stringWithFormat:@"%d", ([pass.ToP1Score.text integerValue] + [self.player1txt.text integerValue])];
if ([pass.ToP1Score.text integerValue] >= 50) {
pass.ToP1Phase.text = [NSString stringWithFormat:@"%d", ([pass.ToP1Phase.text integerValue] + 1)];
}
//Setting Player 2's score phase.
pass.ToP2Score.text = [NSString stringWithFormat:@"%d", ([pass.ToP2Score.text integerValue] + [self.player2txt.text integerValue])];
if ([pass.ToP2Score.text integerValue] >= 50) {
pass.ToP2Phase.text = [NSString stringWithFormat:@"%d", ([pass.ToP2Phase.text integerValue] + 1)];
}
//Setting Player 3's Score
pass.ToP3Score.text = [NSString stringWithFormat:@"%d", ([pass.ToP3Score.text integerValue] + [self.player1txt.text integerValue])];
if ([pass.ToP3Score.text integerValue] >= 50) {
pass.ToP3Phase.text = [NSString stringWithFormat:@"%d", ([pass.ToP3Phase.text integerValue] + 1)];
}
//Setting Player 4's Score
pass.ToP4Score.text = [NSString stringWithFormat:@"%d", ([pass.ToP4Score.text integerValue] + [self.player4txt.text integerValue])];
if ([pass.ToP4Score.text integerValue] >= 50) {
pass.ToP4Phase.text = [NSString stringWithFormat:@"%d", ([pass.ToP4Phase.text integerValue] + 1)];
}
//Setting Player 5's Score
pass.ToP5Score.text = [NSString stringWithFormat:@"%d", ([pass.ToP5Score.text integerValue] + [self.player5txt.text integerValue])];
if ([pass.ToP5Score.text integerValue] >= 50) {
pass.ToP5Phase.text = [NSString stringWithFormat:@"%d", ([pass.ToP5Phase.text integerValue] + 1)];
}
//Setting Player 6's Score
pass.ToP6Score.text = [NSString stringWithFormat:@"%d", ([pass.ToP6Score.text integerValue] + [self.player6txt.text integerValue])];
if ([pass.ToP6Score.text integerValue] >= 50) {
pass.ToP6Phase.text = [NSString stringWithFormat:@"%d", ([pass.ToP6Phase.text integerValue] + 1)];
}
NSLog(@"Value of P1ToScore: %@", pass.ToP1Score.text);
}
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
@end
PSAEnterScores.h:
#import <UIKit/UIKit.h>
@interface PSAEnterScores : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *player1txt;
@property (weak, nonatomic) IBOutlet UITextField *player2txt;
@property (weak, nonatomic) IBOutlet UITextField *player3txt;
@property (weak, nonatomic) IBOutlet UITextField *player4txt;
@property (weak, nonatomic) IBOutlet UITextField *player5txt;
@property (weak, nonatomic) IBOutlet UITextField *player6txt;
@property (weak, nonatomic) IBOutlet UILabel *player1Name;
@property (weak, nonatomic) IBOutlet UILabel *player2Name;
@property (weak, nonatomic) IBOutlet UILabel *player3Name;
@property (weak, nonatomic) IBOutlet UILabel *player4Name;
@property (weak, nonatomic) IBOutlet UILabel *player5Name;
@property (weak, nonatomic) IBOutlet UILabel *player6Name;
@end
Log after one time inputing data:
2013-11-25 19:04:08.810 Phase 10 Scoring Application[34601:11303] Unknown class PSA in Interface Builder file.
2013-11-25 19:08:11.607 Phase 10 Scoring Application[34601:11303] Value of P1ToScore: (null)
(The Name of the program is Phase 10 Scoring Application)
I have no idea why it says Unknown class PSA in Interface Builder file.
I have no class named "PSA", it is merely the prefix for my classes.
prepareForSegue: is called before viewDidLoad:
so your IBOutlet
will be nil. Try setting the incoming value to a NSString
and then use that string to populate the UIlabel
on viewDidLoad:
For example
In your PSACurrentGame.h: // create a property @property(nonatomic,strong)NSString *gameName;
In your PSAEnterScores.m: modify your
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
PSACurrentGame *pass = (PSACurrentGame *)segue.destinationViewController;
pass.gameName=[NSString alloc]initWithString:pass.R2P1Score.text]
}
In your PSACurrentGame.m :
- (void)viewDidLoad
{
[super viewDidLoad];
// set text to your label
R3P1Score.text=gameName;
}