我有一个UIWebView
页面,其中一个UIScrollView
位于我的UIScrollView
内UIImage
和按钮
我想从我的xml文件中为每个按钮设置特定的标题,请你给我一些提示来实现它吗?
右边我可以从xml获得第一个名字但是它已经为所有
设置了我的带有按钮和图像的scrollView:
const NSUInteger kNumImages = 5;
scrollView1.pagingEnabled = YES;
// load all the images
NSUInteger i;
for (i = 1; i<= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = YES;
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
[scrollView1 addSubview:imageView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
Presentation1Name *aSlide = [appDelegate.books objectAtIndex:0];
NSLog(@" %@", aSlide.slideLabel );
[btn setTitle:[NSString stringWithString:aSlide.slideLabel]
forState:UIControlStateNormal];
btn.frame = rect;
[btn setTag:i];
[btn addTarget:self action:@selector(buttonClick:)
forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:btn];
}
[self layoutScrollImages];
}
为所有按钮添加标题我正在使用代码
[btn setTitle:[NSString stringWithString:aSlide.slideLabel]
forState:UIControlStateNormal];
但是对于每个按钮,我想要从xml文件中获得特定标题
提前致谢!
这是我的xml文件:
<Presentation label="presentation1">
<slides>
<slide index="0" label="slide1" identifier="Slide1Name" />
<slide index="1" label="something" identifier="Slide2Name" />
</slides>
</Presentation>
修改:
当我添加Presentation1Name * aSlide = [appDelegate.books objectAtIndex:0]; objectAtIndex to i我将收到此错误
当我添加objectAtIndex时:i 进程终止 *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [NSPlaceholderString initWithString:]:nil参数' * *第一次抛出调用堆栈: (0x1c95012 0x10d2e7e 0x1c94deb 0xae97f5 0xae9774 0x3d64 0x4723 0x10e6705 0x1d920 0x1d8b8 0xde671 0xdebcf 0xddd38 0x4d33f 0x4d552 0x2b3aa 0x1ccf8 0x1bf0df9 0x1bf0ad0 0x1c0abf5 0x1c0a962 0x1c3bbb6 0x1c3af44 0x1c3ae1b 0x1bef7e3 0x1bef668 0x1a65c 0x295d 0x2885为0x1) libc ++ abi.dylib:terminate调用抛出异常 (LLDB)
这是因为
- [NSPlaceholderString initWithString:]:nil参数'
之后我改变了5 t0 2,因为我的xml文件中只有2个标签
const NSUInteger kNumImages= 2;
并且还将(i = 1; i&lt; = kNumImages; i ++)更改为
for (i = 0; i> kNumImages; i++)
但仍“无法正常工作
我的问题是我该怎么做
appDelegate.books count should be equal to kNumImages
答案 0 :(得分:0)
[btn setTitle:[NSString stringWithFormat:@"MyButton %d", i] forState:UIControlStateNormal];
答案 1 :(得分:0)
将所有按钮名称存储在 NSMutableArray 中,然后再使用
例如:
NSMutableArray *arrNames = [NSMutableArray alloc]initWithObjects:@"Dog",@"Cat",......,nil]; //fill your array
[btn setTitle:[arrNames objectAtIndex:i] forState:UIControlStateNormal];
第二种选择是在答案中使用@Andrey Chernukha