0

我有控制器:

@interface BallsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation BallsViewController
@synthesize viewBoard;

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
    self = [super initWithNibName:nibName bundle:nibBundle];
    if (self)
    {
      //  ScoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 300, 30)];
      //  [self.view addSubview:ScoreLabel];
        // add other stuff you need to initialize ...
    }
    return self;
}

- (id)init
{
    // since we don't wanna re-implement allocation and instantiation for every
    // initializer, we call the 'designated initializer' with some default values,
    // in this case the default nibName and bundle are nil.
    return [self initWithNibName:nil bundle:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.viewBoard Draw:@"Fields"];
    // Do any additional setup after loading the view, typically from a nib.
    self.label.text = @"lll";
}

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

- (IBAction)NewGame:(id)sender {
        self.label.text = @"aaa";
    self.viewBoard.canDrawBall = true;
    [self.viewBoard Draw:@"Fields"];
   // }
   // else
   // {
   //     self.InfoLabel.text = @"End game";
   // }


  }

-(void)UpdateScore:(int)score
{
    NSLog(@"%@", self.label);
    NSString *scoreStr =[NSString stringWithFormat:@"%d",score];
    [self.label setText:scoreStr];

}

@end

当我在 viewDidLoad 中检查标签并初始化 NewGame 方法控件时,我可以为标签设置新值。但是当我调用委托方法UpdateScore时,我的标签是空的,即使我在上面提到的调用方法之后执行这个方法。为什么会发生?如何解决这个问题?

4

1 回答 1

0
1> Either you can Do it using XIB or you make it Custom Label
 2> if you want to use XIB then , Drag and Drop UILabel in your XIB, and set its Delegate and LabelName to its property. Now you can set value of label any where in .m file.

3> if you make it CUSTOM , then you will have to write following code in your .m file:
  • (void)viewDidLoad

UILabel *UrLabel = [ [UILabel alloc] initWithFrame:CGRectMake((self.bounds.size.width / 2), 0.0, 150.0, 43.0) ];

[自我添加子视图:UrLabel];

UrLabel.text = [NSString stringWithFormat: @"%d", score];

于 2013-06-05T04:32:09.753 回答