我在Objective C中编写了一个程序,它使用两个不同的视图来输入数据。一个视图仅显示视图(PSACurrentGame),另一个视图输入数据(PSAEnterScores)。我正在使用“prepareforsegue”方法将数据从PSAEnterScores传递到PSACurrentGame。问题是标签没有变化。知道为什么吗?
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
一次输入数据后记录:
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)
(该计划的名称是第10阶段评分申请)
我不知道为什么会说Unknown class PSA in Interface Builder file.
我没有名为“PSA”的班级,它只是我班级的前缀。
答案 0 :(得分:1)
prepareForSegue:在viewDidLoad:
之前调用,因此您的IBOutlet
将为零。尝试将传入值设置为NSString
,然后使用该字符串填充UIlabel
上的viewDidLoad:
例如
在您的PSACurrentGame.h中: //创建一个属性 @property(非原子,强)NSString * gameName;
在您的PSAEnterScores.m中:修改您的
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
PSACurrentGame *pass = (PSACurrentGame *)segue.destinationViewController;
pass.gameName=[NSString alloc]initWithString:pass.R2P1Score.text]
}
在您的PSACurrentGame.m中:
- (void)viewDidLoad
{
[super viewDidLoad];
// set text to your label
R3P1Score.text=gameName;
}