1

你好。我正在开发一个应用程序,我想在应用程序本身中设置背景。现在我使用 for 循环和设置标签创建了一组按钮,但由于某种原因,当我单击按钮时没有任何反应。

这是创建按钮的地方:

-(void)showSettings {
  [[objc_getClass("DreamBoard") sharedInstance] hideAllExcept:mainView];
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:.5];

  if(toggled){
    photos = [[NSArray arrayWithObjects:   
                [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper1.png"],
                [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper2.png"],
                [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper3.png"],
                [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper4.png"],
                 nil] retain];

    w=0;
    for ( UIImage *image in photos )
    {                          
        for (l = 0; l<(int)photos.count; l++) 
        {
            wallpaperButton = [[UIButton alloc] initWithFrame:CGRectMake(5,130+150*w,150,150)];

            wallpaperButton.tag = l;

            [wallpaperButton setImage:image forState:UIControlStateNormal];

            [wallpaperButton addTarget:self action:@selector(setWallpaper) forControlEvents:UIControlEventTouchDown];

            [settingsMenu addSubview: wallpaperButton];
            [wallpaperButton release];

        }

        w++;
    }

这是设置壁纸的功能:

-(void)setWallpaper {

UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];

if ([wallpaperButton tag] == 1) {
  bgView.image = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper1.png"];
}
else if ([wallpaperButton tag] == 0) {
  bgView.image = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Wallpapers/Wallpaper2.png"];   
}
else if ([wallpaperButton tag] == 2) {
  bgView.image = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/    Wallpapers/Wallpaper3.png"];  
}
else if ([wallpaperButton tag] == 3) {
  bgView.image = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Wallpapers/Wallpaper4.png"]; 
}

[mainView insertSubview:bgView atIndex:0];
[bgView release];  

}

现在按钮显示正确,但它们没有做任何事情。任何帮助表示赞赏。谢谢。

4

1 回答 1

2

添加 NSLog 语句以查看是否正在调用 setWallpaper 方法。如果是,那么我认为这可能是一个修复:

如果您只是将图像添加到您的 XCode 项目中,您可以通过它们的名称来获取它们。将图像拖到项目中时,请确保选中“如果需要,将文件复制到项目文件夹”框。然后要获取图像,您可以这样做:

if ([wallpaperButton tag] == 1) {
    bgView.image = [UIImage imageNamed:@"Wallpaper1.png"];
}
.
.
.
[self.view addSubview:bgView];

另外,您应该知道,除非您的应用程序隐藏状态栏(带有电池和时钟的状态栏),否则您的图像实际上应该有 460 的高度,因为 20px 被用完。

于 2012-07-11T17:47:42.410 回答