1

我有一个UIWebView页面UIScrollView,在我的内部UIScrollView我有UIImage和按钮我想为我的 xml 文件中的每个按钮设置特定的标题,你能给我一些实现这个的提示吗?

现在我可以从 xml 中获取名字,但它是为所有人设置的

我的带有按钮和图像的滚动视图:

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:我进程终止* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSPlaceholderString initWithString:]: nil argument' * * First throw call stack: (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:终止调用抛出异常(lldb)

这是因为

-[NSPlaceholderString initWithString:]: 无参数'

之后我更改 5 t0 2 因为我的 xml 文件中只有 2 个标签

const NSUInteger kNumImages= 2;

并将 (i = 1; i<= kNumImages; i++) 更改为

for (i = 0; i> kNumImages; i++)

但它仍然"无法正常工作

我的问题是我该怎么做

appDelegate.books count should be equal to kNumImages
4

2 回答 2

0

将所有按钮名称存储在NSMutableArray中,然后再使用它

例如像这样:

 NSMutableArray *arrNames = [NSMutableArray alloc]initWithObjects:@"Dog",@"Cat",......,nil]; //fill your array

 [btn setTitle:[arrNames objectAtIndex:i] forState:UIControlStateNormal];

第二种选择是在他的回答中使用@Andrey Chernukha justed

于 2012-10-11T10:19:59.913 回答
0
[btn setTitle:[NSString stringWithFormat:@"MyButton %d", i] forState:UIControlStateNormal];
于 2012-10-11T10:13:08.777 回答