【问题标题】:add label to UIButtons in iCarousel在 iCarousel 中为 UIButtons 添加标签
【发布时间】:2023-03-30 20:34:01
【问题描述】:

我在 ViewController 中实现了 iCarousel 方法,一切正常,但有一件事,我想在轮播视图标签下方添加,对于每个图像,我想更改此标签区域中的文本。这是我用于将每个图像创建为按钮的代码。

提前致谢。

UIImage *buttonImage =[NSArray arrayWithObjects:[UIImage imageNamed:@"Hotels.png"],
                       [UIImage imageNamed:@"image2.png"],
                       [UIImage imageNamed:@"image3.png"],
                       [UIImage imageNamed:@"image4.png"],
                       [UIImage imageNamed:@"image1.png"],
                       [UIImage imageNamed:@"FastFood.png"],
                       [UIImage imageNamed:@"Taxi.png"],nil];

UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);

[button setImage:[buttonImage objectAtIndex:index]forState:UIControlStateNormal]; [按钮 addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 返回按钮;

【问题讨论】:

  • 请正确格式化代码。 UIImage 对象用 NSArray 初始化。
  • 抱歉格式不正确,这是我在这里的第一篇文章。取而代之的是 NSArray,我还能用什么来在 iCarousel 中创建带有图像的按钮?

标签: uibutton uilabel icarousel


【解决方案1】:

iCarousel 有一个委托方法:

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel;

使用该方法更新您的标签,因为每次轮播中居中的项目更新时都会触发它。

要实际确定要使用的标签文本,您可能需要创建第二个字符串数组来匹配您的图像数组。这样您就可以使用 carousel.currentItemIndex 属性作为字符串数组的索引来选择正确的标签。

【讨论】:

  • 是的,我想添加第二个与图像同时变化的数组。我怎样才能在我的课堂上实现这个方法?
  • 将其作为属性存储在您的视图控制器中。 iCarousel 中包含的基本示例应用程序展示了如何执行此操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多