首次单击后停止创建标签

时间:2013-10-09 10:14:11

标签: ios objective-c uilabel

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

- (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个案例(未在代码片段中全部显示),每当我按下随机按钮时,代码将再次执行,创建一个新的选框标签(不是很好)并更改新标签在这种情况下,文本和视频对应于所选择的新案例(这是正常工作)。我的问题是如何防止再次创建标签?如果我在IBAction的-(void)viewDidLoad中设置标签,则标签未定义,因此我在提到watchTitle的任何地方都收到错误。我不知道如何处理,任何可以让我走上正确轨道的反馈和解释都受到欢迎。

2 个答案:

答案 0 :(得分: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;
        ........

答案 1 :(得分: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;
            ........