2012-03-23 83 views
0

我有任务来开发应用程序与词计数和显示在运行时生成uiscrollview与uilabel作为子视图。如何从uiscroll视图运行时获取当前uilabel文本

这个过程就好像当用户在当时加载页面的时候1000个单词将填充rutime生成的uilabel和scrollview一样,并且所有的东西都是从运行时设置的。但它的应用程序,我们有一个按钮来添加按钮点击当前uilabel文本。

和单词随机来自数据库。当我点击按钮时,它给了我不同的单词。从滚动视图显示的单词。

以下是我的代码以滚动视图填写的UILabel数据:

wordName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 2)]; 
       [wordNameArray addObject:wordName]; 
int i = 0; 
    for (i = 1; i <= kNumImages; i++) 
    { 
     randomN = [wordNameArray objectAtIndex:arc4random() % [wordNameArray count]]; 

     UIImage *image = [UIImage imageNamed:@"word_bg.png"]; 
     word = [[UILabel alloc]initWithFrame:CGRectMake(15,12,250,120)]; 
     [word setFont:[UIFont fontWithName:@"Helvetica-Bold" size:36.0f]]; 
     word.textColor = [UIColor whiteColor]; 
     word.lineBreakMode = UILineBreakModeWordWrap; 
     word.numberOfLines = 2; 
     word.textAlignment = UITextAlignmentCenter; 
     word.backgroundColor = [UIColor clearColor]; 
     [word setText:randomN]; 
     lblWord.text = word.text; 

     word.tag = i; 
     NSLog(@"tag no:%d",word.tag); 

     imageView = [[UIImageView alloc] initWithImage:image]; 
     [imageView addSubview:word]; 

     CGRect rect = imageView.frame; 
     rect.size.height = kScrollObjHeight; 
     rect.size.width = kScrollObjWidth; 
     imageView.frame = rect; 
     imageView.tag = i; 

     //NSLog(@"%@",word.text); 
     //NSLog(@"%@",lblWord.text); 

     // [scrollView1 addSubview:l]; 
     [scrollView1 addSubview:imageView]; 

     touchToSee.hidden = TRUE; 
     [imageView release]; 
    } 

    [self layoutScrollImages]; 

以下是我的滚动事件得到来自标签的字,但它给我从数组最后一个字:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 
    touchToSee.hidden = TRUE; 
    NSLog(@"word-%@",word.text); 

} 

而其他当我上按钮我没有得到完美的作品点击所以这下面是我的代码:

NSString *getword = [NSString stringWithFormat:@"%@",word.text]; 
    NSLog(@"lblCountdownBackward:%@",word.text); 

所以请帮助我,并提供一些示例代码,如果可能的话。

在此先感谢。

回答

0

你似乎是创建大量单词的对象,并将它们添加到屏幕上。

在你的循环,你高达kNumImages执行代码。并且在每次运行中,word对象将被替换为新的对象。

因此,当你以后的话,这将是最后word对象创建(因此硬道理)。

为了避免这种情况,您可以使用NSMutableArray并不断添加新的对象UILabel(而不是创建word对象)。再后来就做类似

[array objectAtIndex:0].text; 
相关问题