开发者

how to change an UIButon image on a casted controller

I am trying to set the fields and button image on a casted controller. I can successfully change title fields and details text fields but the button does not change at all.

- (void)prepareController:(MLSViewController *)controller forRow:(NSUInteger)row {
    AdvancedTableItem *item = [self.tableItems objectAtIndex:row];
    EditLocationViewController *castedController = (EditLocationViewController *)controller;
    castedController.titleText = item.text;
    castedController.detailsText = item.details;
    castedController.isDefaultLocation = (defaultItem == item);
    [castedController.iconButton setImage:[UIImage imageNamed:item.icon] for开发者_如何转开发State:UIControlStateNormal ];
}

I did test to see if item.icon was correctly populated before calling setImage on the casted controller's button and it was correct.

In short this code works fine for the titleText and detailsText changes but not for the button image.

Any clue?


There are several things that might be wrong. I would do the following:

  1. Create the Image beforehand and make sure it is loaded
  2. Make sure that the iconButton (which I assume is an IBOutlet) is linked
  3. Make sure that this method is invoked after the View is displayed otherwise the IBOutlets are not set but I assume that is the case otherwise the title would not appear either).

This is what I would do in your method:

- (void)prepareController:(MLSViewController *)controller forRow:(NSUInteger)row {
    AdvancedTableItem *item = [self.tableItems objectAtIndex:row];
    NSLog( @"Item from table: %@", item );
    EditLocationViewController *castedController = (EditLocationViewController *)controller;
    NSLog( @"Controller: %@", castedController );
    castedController.titleText = item.text;
    castedController.detailsText = item.details;
    castedController.isDefaultLocation = (defaultItem == item);
    NSLog( @"Icon Name: %@", item.icon );
    UIImage *icon = [UIImage imageNamed:item.icon];
    NSLog( @"Icon Image: %@, icon button on controller: %@", castedController.iconButton );
    [castedController.iconButton setImage:icon forState:UIControlStateNormal ];
}

Make sure that all values are not NIL (especially the icon and the iconButton).

When dealing with problems in iPhone / iOS NSLog is your friend and nested invocations are your enemy. Just because a variable is NIL doesn't mean that an App will complain about it. It might just ignore it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜