每次单击随机按钮时,我都会创建一个选取框标签,并根据随机选择的情况设置其文本。这是我的代码示例:
- (IBAction)randomWatch:(id)sender {
MarqueeLabel *watchTitle = [[MarqueeLabel alloc] initWithFrame:CGRectMake(0, 77, self.view.frame.size.width-20, 44) rate:50.0f andFadeLength:10.0f];
watchTitle.numberOfLines = 1;
watchTitle.shadowOffset = CGSizeMake(0.0, -1.0);
watchTitle.textAlignment = NSTextAlignmentRight;
watchTitle.textColor = [UIColor colorWithRed:0.234 green:0.234 blue:0.234 alpha:1.000];
watchTitle.backgroundColor = [UIColor clearColor];
watchTitle.font = [UIFont fontWithName:@"Helvetica-Neue-Light" size:25.000];
watchTitle.marqueeType = MLRightLeft;
[self.view addSubview:watchTitle];
watchTut1.hidden = YES;
watchTut2.hidden = YES;
watchTut3.hidden = YES;
watchTut4.hidden = YES;
int i = rand() % 25;
switch (i) {
case 0:
watchTitle.text = @"";
watchTitle.text = @" How To Catch a Pokemon";
[[self watchPlaceholder]loadHTMLString:@"<iframe width=\"300\" height=\"225\" src=\"http://www.youtube.com/embed/NyGv8XtKJc4\" frameborder=\"0\" allowfullscreen></iframe>" baseURL:nil];
break;
case 1:
watchTitle.text = @"";
watchTitle.text = @" Full Turn ECAL/Benjamin Muzzin";
[[self watchPlaceholder]loadHTMLString:@"<iframe width=\"300\" height=\"225\" src=\"http://player.vimeo.com/video/74735651\" frameborder=\"0\" allowfullscreen></iframe>" baseURL:nil];
break;
........
我有 25 个案例(代码片段中未全部显示),每次我按下随机按钮时,当然代码将再次执行创建一个新的 Marquee 标签(这不好)并更改新标签文本和视频在这种情况下,对应于选择的新案例(这工作正常)。我的问题是如何防止再次创建标签?如果我-(void)viewDidLoad
在 IBAction 中设置标签,则标签未定义,因此在watchTitle
提到的任何地方我都会收到错误。我不知道如何处理,任何可以让我走上正轨的反馈和解释都非常受欢迎。