Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 循环从viewDidLoad上的文档目录设置UIImages_Ios_Iphone_Objective C_Uibutton_Uiimage - Fatal编程技术网

Ios 循环从viewDidLoad上的文档目录设置UIImages

Ios 循环从viewDidLoad上的文档目录设置UIImages,ios,iphone,objective-c,uibutton,uiimage,Ios,Iphone,Objective C,Uibutton,Uiimage,我有9个按钮,其图像由用户动态设置。每个按钮的当前图像都保存在用户文档文件夹中。在viewDidLoadid中,我想将每个图像重新设置为它的UIButton 我可以很容易地做到这一点: NSString *filePath = [self documentsPathForFileName: @"boss1.png"]; NSData *pngData = [NSData dataWithContentsOfFile:filePath]; UIImage *savedBoss = [UIImage

我有9个按钮,其图像由用户动态设置。每个按钮的当前图像都保存在用户文档文件夹中。在
viewDidLoad
id中,我想将每个图像重新设置为它的UIButton

我可以很容易地做到这一点:

NSString *filePath = [self documentsPathForFileName: @"boss1.png"];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *savedBoss = [UIImage imageWithData:pngData];
[boss1Button setImage:savedBoss forState:UIControlStateNormal];


...... 8 more times....
当然,我更喜欢使用循环。只是,我不确定在objective C中会是什么样子。在jQuery中,我可以这样做:

$('.bosses').each(function( index ) {
    var imageUrl='../images/boss'+(index+1)
    $('#'+this.id).css('background-image', 'url(' + imageUrl + ')')
});
如何创建一个类似的
objective-C
循环,使boss图像名称和按钮名称同样递增

此外,有没有更好的方法完全做到这一点


我觉得拥有一个图像URL的
NSArray
和一个
UIButton
名称的NSArray,并使用循环将它们配对可能会更好。。。。但是我也不确定这里的语法是什么样子的。

试试这个,按钮标签是1到8

for (id subview in self.view.subviews) {
  if([subview isKindOfClass:[UIButton class]]) {
    UIButton* button = (UIButton*)subview;
    NSString *filePath = [self documentsPathForFileName:[NSString stringWithFromate@"boss%d.png", button.tag]];
    NSData *pngData = [NSData dataWithContentsOfFile:filePath];
    UIImage *savedBoss = [UIImage imageWithData:pngData];
    [button setImage:savedBoss forState:UIControlStateNormal];
  }
}

这样就可以了。

像这样试试,按钮标签是1到8

for (id subview in self.view.subviews) {
  if([subview isKindOfClass:[UIButton class]]) {
    UIButton* button = (UIButton*)subview;
    NSString *filePath = [self documentsPathForFileName:[NSString stringWithFromate@"boss%d.png", button.tag]];
    NSData *pngData = [NSData dataWithContentsOfFile:filePath];
    UIImage *savedBoss = [UIImage imageWithData:pngData];
    [button setImage:savedBoss forState:UIControlStateNormal];
  }
}

这样就可以了。

像这样试试,按钮标签是1到8

for (id subview in self.view.subviews) {
  if([subview isKindOfClass:[UIButton class]]) {
    UIButton* button = (UIButton*)subview;
    NSString *filePath = [self documentsPathForFileName:[NSString stringWithFromate@"boss%d.png", button.tag]];
    NSData *pngData = [NSData dataWithContentsOfFile:filePath];
    UIImage *savedBoss = [UIImage imageWithData:pngData];
    [button setImage:savedBoss forState:UIControlStateNormal];
  }
}

这样就可以了。

像这样试试,按钮标签是1到8

for (id subview in self.view.subviews) {
  if([subview isKindOfClass:[UIButton class]]) {
    UIButton* button = (UIButton*)subview;
    NSString *filePath = [self documentsPathForFileName:[NSString stringWithFromate@"boss%d.png", button.tag]];
    NSData *pngData = [NSData dataWithContentsOfFile:filePath];
    UIImage *savedBoss = [UIImage imageWithData:pngData];
    [button setImage:savedBoss forState:UIControlStateNormal];
  }
}
这样就可以了。

就像:

    //Add all the buttons in an array for easy loop
    NSArray *array = @[btn1,btn2,btn3,btn4... etc];
   NSString *name = @"BOSS";
   int x = 1;

    for(UIButton *btn in array){ // loop through all the buttons

        NSString *filePath = [self documentsPathForFileName:[NSString stringWithFormat:@"%@%d.png",name,x]];
        NSData *pngData = [NSData dataWithContentsOfFile:filePath];
        UIImage *savedBoss = [UIImage imageWithData:pngData];
        [btn setImage:savedBoss forState:UIControlStateNormal];
        x++;

    }
它将像:

    //Add all the buttons in an array for easy loop
    NSArray *array = @[btn1,btn2,btn3,btn4... etc];
   NSString *name = @"BOSS";
   int x = 1;

    for(UIButton *btn in array){ // loop through all the buttons

        NSString *filePath = [self documentsPathForFileName:[NSString stringWithFormat:@"%@%d.png",name,x]];
        NSData *pngData = [NSData dataWithContentsOfFile:filePath];
        UIImage *savedBoss = [UIImage imageWithData:pngData];
        [btn setImage:savedBoss forState:UIControlStateNormal];
        x++;

    }
它将像:

    //Add all the buttons in an array for easy loop
    NSArray *array = @[btn1,btn2,btn3,btn4... etc];
   NSString *name = @"BOSS";
   int x = 1;

    for(UIButton *btn in array){ // loop through all the buttons

        NSString *filePath = [self documentsPathForFileName:[NSString stringWithFormat:@"%@%d.png",name,x]];
        NSData *pngData = [NSData dataWithContentsOfFile:filePath];
        UIImage *savedBoss = [UIImage imageWithData:pngData];
        [btn setImage:savedBoss forState:UIControlStateNormal];
        x++;

    }
它将像:

    //Add all the buttons in an array for easy loop
    NSArray *array = @[btn1,btn2,btn3,btn4... etc];
   NSString *name = @"BOSS";
   int x = 1;

    for(UIButton *btn in array){ // loop through all the buttons

        NSString *filePath = [self documentsPathForFileName:[NSString stringWithFormat:@"%@%d.png",name,x]];
        NSData *pngData = [NSData dataWithContentsOfFile:filePath];
        UIImage *savedBoss = [UIImage imageWithData:pngData];
        [btn setImage:savedBoss forState:UIControlStateNormal];
        x++;

    }


现在正在处理它,
isKindaOfClass
应该是
iskindofcclass
我认为,对吗?如果您已经在视图中添加了按钮,这将起作用。我很惊讶您在没有自动完成的情况下关闭了它!看起来
stringWithFormat
有点不对劲,我想已经修复了,但仍然抛出了“预期的”]`NSString*filePath=[self-documentsPathForFileName:[NSString stringWithFormat@“boss%d.png”,button.tag]]“是的@Joshua,如果他需要添加按钮,你已经向他展示了一种方法:)@Joshua是的,他们已经在故事板上设置好了,现在正在制作,
isKindaOfClass
应该是
isKindOfClass
我想,对吧?如果你已经在视图中添加了按钮,这将起作用。我印象深刻的是,你在没有自动完成的情况下完成了它!看起来
stringWithFormat
有点不对劲,我想已经修复了,但仍然抛出了“预期的”]`NSString*filePath=[self-documentsPathForFileName:[NSString-stringWithFormat@“boss%d.png”,button.tag]]`是的@Joshua,如果他需要添加按钮,你已经向他展示了一种方法:)@Joshua是的,他们已经在故事板上设置好了,现在正在制作,
isKindaOfClass
应该是
isKindOfClass
我想,对吧?如果你已经在视图中添加了按钮,这将起作用。我印象深刻的是,你在没有自动完成的情况下完成了它!看起来
stringWithFormat
有点不对劲,我想已经修复了,但仍然抛出了“预期的”]`NSString*filePath=[self-documentsPathForFileName:[NSString stringWithFormat@“boss%d.png”,button.tag]]“是的@Joshua,如果他需要添加按钮,你已经向他展示了一种方法:)@Joshua是的,他们已经在故事板上设置好了,现在正在制作,
isKindaOfClass
应该是
isKindOfClass
我想,对吧?如果你已经在视图中添加了按钮,这将起作用。我印象深刻的是,你在没有自动完成的情况下完成了它!看起来
stringWithFormat
有点不对劲,我想已经修复了,但仍然抛出了“预期的”]`NSString*filePath=[self-documentsPathForFileName:[NSString-stringWithFormat@“boss%d.png”,button.tag]]`是的@Joshua,如果他需要添加按钮,你已经向他展示了一种方法:)@Joshua是的,他们在故事板中设置完美,必须更改“boss”的情况,但这正是我需要的,非常可读,谢谢!很好,我不得不改变“老板”的情况,但这正是我需要的,而且非常可读,谢谢!很好,我不得不改变“老板”的情况,但这正是我需要的,而且非常可读,谢谢!很好,我不得不改变“老板”的情况,但这正是我需要的,而且非常可读,谢谢!