我正在尝试创建两个按钮,其标签最初设置为默认值。这两个按钮由popOverViewController管理,其中viewDidLoad方法初始化所有控件。我还有另一个名为addButtons的方法,它在收到Apple的服务器包含产品描述和价格的有效响应后,由AppDelegate执行。
我的问题是我无法使用该方法更新标签。
popOverViewController中的viewDidLoad方法是:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Add a label to the popover's view controller.
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(button1Week:)
forControlEvents:UIControlEventTouchDown];
NSString *priceWeekly = [NSString stringWithFormat:@"weekly subscription"];
[button setTitle:priceWeekly forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 200.0, 50.0);
[button2 addTarget:self
action:@selector(button1Month:)
forControlEvents:UIControlEventTouchDown];
[button2 setTitle:@"monthly subscription" forState:UIControlStateNormal];
button2.frame = CGRectMake(0.0, 55.0, 200.0, 50.0);
[self.view addSubview:button];
[self.view addSubview:button2];
}
和我的更新标签方法,我确信它会在正确的时间被调用,是:
-(void)addButtons {
[self.button setTitle:@"updated label" forState:UIControlStateNormal];
[self.button2 setTitle:@"updated label 2" forState:UIControlStateNormal];
}
您可以查看我的代码并建议更新标签的正确方法吗?基本上我想用默认标签初始化控件,因为控件创建和服务器响应之间的等待时间。
更新:
我已按如下方式更新了代码,但仍然执行了addButtons方法中的所有内容,因为我要检查NSLog,但按钮标题仍然保留为viewdidload中创建的
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Add a label to the popover's view controller.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ilManifestoAppDelegate *delegate = (ilManifestoAppDelegate*)[[UIApplication sharedApplication] delegate];
[button addTarget:self
action:@selector(button1Week:)
forControlEvents:UIControlEventTouchDown];
// [button setTitle:priceWeekly forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 200.0, 50.0);
[button2 addTarget:self
action:@selector(button1Month:)
forControlEvents:UIControlEventTouchDown];
// [button2 setTitle:@"monthly subscription" forState:UIControlStateNormal];
button2.frame = CGRectMake(0.0, 55.0, 200.0, 50.0);
// [self addButtons];
NSString *weekPrice = [NSString stringWithFormat:@"settimanale a %@",delegate.priceWeek];
[button setTitle:weekPrice forState:UIControlStateNormal];
[button2 setTitle:@"updated label 2" forState:UIControlStateNormal];
NSLog(@"week price %@", weekPrice);
[self.view addSubview:button];
[self.view addSubview:button2];
}
-(void)addButtons {
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setTitle:@"modificato" forState:UIControlStateNormal];
[self.view addSubview:button];
NSLog(@"addButtons executed");
}
答案 0 :(得分:0)
在viewDidLoad
button
您正在使用button2
和addButtons
。在self.button
中,您使用的是self.button2
和addButtons
。你确定这些是相同的按钮吗?
还在{{1}}中放置一个断点,看它是否真的被调用,如果你还没有这样做的话。
您是否还为xib中的按钮创建了插座,如果您还没有将它们放在代码中。