1

每次单击随机按钮时,我都会创建一个选取框标签,并根据随机选择的情况设置其文本。这是我的代码示例:

- (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提到的任何地方我都会收到错误。我不知道如何处理,任何可以让我走上正轨的反馈和解释都非常受欢迎。

4

2 回答 2

1

尝试将创建代码移动到 viewDidLoad 中(仅将选取框放在属性中并隐藏它)。然后按下按钮,您只需显示它。

//Define the property somewhere in your interface
@property (nonatomic, strong) MarqueeLabel* watchTitle;
//...

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.watchTitle = [[MarqueeLabel alloc] initWithFrame:CGRectMake(0, 77, self.view.frame.size.width-20, 44) rate:50.0f andFadeLength:10.0f];
    self.watchTitle.numberOfLines = 1;
    self.watchTitle.shadowOffset = CGSizeMake(0.0, -1.0);
    self.watchTitle.textAlignment = NSTextAlignmentRight;
    self.watchTitle.textColor = [UIColor colorWithRed:0.234 green:0.234 blue:0.234 alpha:1.000];
    self.watchTitle.backgroundColor = [UIColor clearColor];
    self.watchTitle.font = [UIFont fontWithName:@"Helvetica-Neue-Light" size:25.000];
    self.watchTitle.marqueeType = MLRightLeft;
    self.watchTitle.hidden = YES;
    [self.view addSubview:self.watchTitle];
}

- (IBAction)randomWatch:(id)sender {
    self.watchTitle.hidden = NO;

    watchTut1.hidden = YES;
    watchTut2.hidden = YES;
    watchTut3.hidden = YES;
    watchTut4.hidden = YES;
    int i = rand() % 25;
    switch (i) {
        case 0:
            self.watchTitle.text = @"";
            self.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:
            self.watchTitle.text = @"";
            self.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;
        ........
于 2013-10-09T10:32:44.387 回答
1

watchTitle范围更改为类。这样做。

    - (IBAction)randomWatch:(id)sender 
    {
    if(!watchTitle)
    {
       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;
            ........
于 2013-10-09T10:33:53.703 回答