我有问题,我有几个视图控制器。一个称为PSACurrentGame
,另一个称为PSAEnterScores
。 PSACurrentGame
是PSAEnterScores
的父类。我正在尝试从PSACurrentGame
更改PSAEnterScores
中的标签。但是我已经用父类的东西想出来了。真正的问题是,虽然我有与变量链接的正确属性,但变量的文本不会改变。我确认lanbel响应viewDidLoad
方法中的更改,但它不会响应在子类中更改它。有谁知道为什么?
以下代码:
PSACurrentGame.h
:
@interface PSACurrentGame : UIViewController
@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 *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 *R1List2;
@property (strong, nonatomic) IBOutlet UILabel *R2List2;
@property (strong, nonatomic) IBOutlet UILabel *R3List2;
@property (strong, nonatomic) IBOutlet UILabel *ToList2;
- (void) updateScores;
@end
PSACurrentGame.m
:
@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
self.ToP1Score = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,50)];
[self.ToP1Score setTextColor:[UIColor redColor]];
[self.ToP1Score setFont:[UIFont fontWithName:@"Arial" size:30]];
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.
}
PSAEnterScores.h
:
@interface PSAEnterScores : PSACurrentGame
@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 *player1Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player2Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player3Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player4Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player5Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player6Lbl;
@end
PSAEnterScores.m
:
@interface PSAEnterScores ()
- (IBAction)ok:(id)sender;
@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.player1Lbl.text = global.player1Name;
self.player2Lbl.text = global.player2Name;
self.player3Lbl.text = global.player3Name;
self.player4Lbl.text = global.player4Name;
self.player5Lbl.text = global.player5Name;
self.player6Lbl.text = global.player6Name;
if ([self.player3Lbl.text isEqual:@""]) {
self.player3Lbl.hidden = YES;
self.player3Txt.hidden = YES;
}
if ([self.player4Lbl.text isEqual:@""]) {
self.player4Lbl.hidden = YES;
self.player4Txt.hidden = YES;
}
if ([self.player5Lbl.text isEqual:@""]) {
self.player5Lbl.hidden = YES;
self.player5Txt.hidden = YES;
}
if ([self.player6Lbl.text isEqual:@""]) {
self.player6Lbl.hidden = YES;
self.player6Txt.hidden = YES;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (IBAction)ok:(id)sender {
[self.ToP1Score setText:self.player1Txt.text];
self.R3P1Score.text = self.R2P1Score.text;
self.R3P1Phase.text = self.R2P1Phase.text;
self.R3P2Score.text = self.R2P2Score.text;
self.R3P2Phase.text = self.R2P2Phase.text;
self.R3P3Score.text = self.R2P3Score.text;
self.R3P3Phase.text = self.R2P3Phase.text;
self.R3P4Score.text = self.R2P4Score.text;
self.R3P4Phase.text = self.R2P4Phase.text;
self.R3P5Score.text = self.R2P5Score.text;
self.R3P5Phase.text = self.R2P5Phase.text;
self.R3P6Score.text = self.R2P6Score.text;
self.R3P6Phase.text = self.R2P6Phase.text;
//Round 1 -> Round 2
self.R2P1Score.text = self.R1P1Score.text;
self.R2P1Phase.text = self.R1P1Phase.text;
self.R2P2Score.text = self.R1P2Score.text;
self.R2P2Phase.text = self.R1P2Phase.text;
self.R2P3Score.text = self.R1P3Score.text;
self.R2P3Phase.text = self.R1P3Phase.text;
self.R2P4Score.text = self.R1P4Score.text;
self.R2P4Phase.text = self.R1P4Phase.text;
self.R2P5Score.text = self.R1P5Score.text;
self.R2P5Phase.text = self.R1P5Phase.text;
self.R2P6Score.text = self.R1P6Score.text;
self.R2P6Phase.text = self.R1P6Phase.text;
//Total -> Round 1
self.R1P1Score.text = self.ToP1Score.text;
self.R1P1Phase.text = self.ToP1Phase.text;
self.R1P2Score.text = self.ToP2Score.text;
self.R1P2Phase.text = self.ToP2Phase.text;
self.R1P3Score.text = self.ToP3Score.text;
self.R1P3Phase.text = self.ToP3Phase.text;
self.R1P4Score.text = self.ToP4Score.text;
self.R1P4Phase.text = self.ToP4Phase.text;
self.R1P5Score.text = self.ToP5Score.text;
self.R1P5Phase.text = self.ToP5Phase.text;
self.R1P6Score.text = self.ToP6Score.text;
self.R1P6Phase.text = self.ToP6Phase.text;
//Setting Total's score
//Setting Player 1's Score // This section is where the ToP1Score should be changed, but it isn't!
NSInteger curScore = [self.ToP1Score.text integerValue];
NSInteger curPhase = [self.ToP1Phase.text integerValue];
self.ToP1Score.text = [NSString stringWithFormat:@"%d", (curScore += [self.player1Txt.text integerValue])];
if ([self.player1Txt.text integerValue] < 50) {
self.ToP1Phase.text = [NSString stringWithFormat:@"%d", (curPhase += 1)];
}
}
- (void) updateScores {
}
@end
如果您还需要更多评论,我会添加。
编辑:我更改了顶部的文字,但我也会把它放在这里。标签现在响应其自己的类中的更改,但现在在子类中。子类只具有访问父类变量的只读权限吗?答案 0 :(得分:0)
我认为你有两件事可以改进:
1)使用IBOutletCollection而不是为每个接口控件创建一个;
2)使用共享数据源,并在其值发生变化时收到通知。然后,您可以在自己的ViewControllers中对控件进行更改,如下所示:
self.dataStore addObserver:self forKeyPath:@"gameScore" options:0 context:nil];
//...
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqual:@"gameScore"]) {
//update your IBOutletCollections...
}
}
答案 1 :(得分:0)
我在我的应用程序中使用此示例在视图控制器之间传输数据。因此,它可能会有所帮助:http://iphonedevsdk.com/forum/iphone-sdk-development/54859-sharing-data-between-view-controllers-and-other-objects-link-fixed.html