0

我正在尝试创建两个按钮,其标签最初设置为默认值。这两个按钮由 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");
}
4

1 回答 1

0

viewDidLoad你使用buttonand button2。在addButtons你使用self.buttonand self.button2。您是否确保这些按钮是相同的?

如果您还没有这样做,还可以放置一个断点addButtons以查看它是否真的被调用。

如果您没有仅使用代码放置它们,您是否还创建了 xib 中按钮的出口。

于 2013-02-13T08:38:57.547 回答