Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView not updating

I'm currently building a chess game for iPhone. The pieces on the board are represented by a series of UIImageViews that each represent on square of the board, and reference the correct image to display for that square. One a square has been chosen and the move has been made, the function -(void)updateBoard; is called to in the ViewController:

-(void)updateBoard{

NSLog(@"updating board");

[square1 setImage:[UIImage imageNamed:[game imageForSquare:1]]];
[square2 setImage:[UIImage imageNamed:[game imageForSquare:2]]];
[square3 setImage:[UIImage imageNamed:[game imageForSquare:3]]];
[square4 setImage:[UIImage imageNamed:[game imageForSquare:4]]];
[square5 setImage:[UIImage imageNamed:[game imageForSquare:5]]];
[square6 setImage:[UIImage imageNamed:[game imageForSquare:6]]];
[square7 setImage:[UIImage imageNamed:[game imageForSquare:7]]];
[square8 setImage:[UIImage imageNamed:[game imageForSquare:8]]];
[square9 setImage:[UIImage imageNamed:[game imageForSquare:9]]];
[square10 setImage:[UIImage imageNamed:[game imageForSquare:10]]];
[square11 setImage:[UIImage imageNamed:[game imageForSquare:11]]];
[square12 setImage:[UIImage imageNamed:[game imageForSquare:12]]];
[square13 setImage:[UIImage imageNamed:[game imageForSquare:13]]];
[square14 setImage:[UIImage imageNamed:[game imageForSquare:14]]];
[square15 setImage:[UIImage imageNamed:[game imageForSquare:15]]];
[square16 setImage:[UIImage imageNamed:[game imageForSquare:16]]];
[square17 setImage:[UIImage imageNamed:[game imageForSquare:17]]];
[square18 setImage:[UIImage imageNamed:[game imageForSquare:18]]];
[square19 setImage:[UIImage imageNamed:[game imageForSquare:19]]];
[square20 setImage:[UIImage imageNamed:[game imageForSquare:20]]];
[square21 setImage:[UIImage imageNamed:[game imageForSquare:21]]];
[square22 setImage:[UIImage imageNamed:[game imageForSquare:22]]];
[square23 setImage:[UIImage imageNamed:[game imageForSquare:23]]];
[square24 setImage:[UIImage imageNamed:[game imageForSquare:24]]];
[square25 setImage:[UIImage imageNamed:[game imageForSquare:25]]];
[square26 setImage:[UIImage imageNamed:[game imageForSquare:26]]];
[square27 setImage:[UIImage imageNamed:[game imageForSquare:27]]];
[square28 setImage:[UIImage imageNamed:[game imageForSquare:28]]];
[square29 setImage:[UIImage imageNamed:[game imageForSquare:29]]];
[square30 setImage:[UIImage imageNamed:[game imageForSquare:30]]];
[square31 setImage:[UIImage imageNamed:[game imageForSquare:31]]];
[square32 setImage:[UIImage imageNamed:[game imageForSquare:32]]];
[square33 setImage:[UIImage imageNamed:[game imageForSquare:33]]];
[square34 setImage:[UIImage imageNamed:[game imageForSquare:34]]];
[square35 setImage:[UIImage imageNamed:[game imageForSquare:35]]];
[square36 setImage:[UIImage imageNamed:[game imageForSquare:36]]];
[square37 setImage:[UIImage imageNamed:[game imageForSquare:37]]];
[square38 setImage:[UIImage imageNamed:[game imageForSquare:38]]];
[square39 setImage:[UIImage imageNamed:[game imageForSquare:39]]];
[square40 setImage:[UIImage imageNamed:[game imageForSquare:40]]];
[square41 setImage:[UIImage imageNamed:[game imageForSquare:41]]];
[square42 setImage:[UIImage imageNamed:[game imageForSquare:42]]];
[square43 setImage:[UIImage imageNamed:[game imageForSquare:43]]];
[square44 setImage:[UIImage imageNamed:[game imageForSquare:44]]];
[square45 setImage:[UIImage imageNamed:[game imageForSquare:45]]];
[square46 setImage:[UIImage imageNamed:[game imageForSquare:46]]];
[square47 setImage:[UIImage imageNamed:[game imageForSquare:47]]];
[square48 setImage:[UIImage imageNamed:[game imageForSquare:48]]];
[square49 setImage:[UIImage imageNamed:[game imageForSquare:49]]];
[square50 setImage:[UIImage imageNamed:[game imageForSquare:50]]];
[square51 setImage:[UIImage imageNamed:[game imageForSquare:51]]];
[square52 setImage:[UIImage imageNamed:[game imageForSquare:52]]];
[square53 setImage:[UIImage imageNamed:[game imageForSquare:53]]];
[square54 setImage:[UIImage imageNamed:[game imageForSquare:54]]];
[square55 setImage:[UIImage imageNamed:[game imageForSquare:55]]];
[square56 setImage:[UIImage imageNamed:[game imageForSquare:56]]];
[square57 setImage:[UIImage imageNamed:[game imageForSquare:57]]];
[square58 setImage:[UIImage imageNamed:[game imageForSquare:58]]];
[square59 setImage:[UIImage imageNamed:[game imageForSquare:59]]];
[square60 setImage:[UIImage imageNamed:[game imageForSquare:60]]];
[square61 setImage:[UIImage imageNamed:[game imageForSquare:61]]];
[square62 setImage:[UIImage imageNamed:[game imageForSquare:62]]];
[square63 setImage:[UIImage imageNamed:[game imageForSquare:63]]];
[square64 setImage:[UIImage imageNamed:[game imageForSquare:64]]];


NSLog(@"-------------------BOARD READOUT-----------------------:");

for (int i = 1; i <=64; i++) {
    NSLog(@"Square %i: %@", i, [game imageForSquare:i]);
}

}

'square1', 'square2' etc. are UIImageViews, and the [game imageForSquare:] method returns an NSString* of the appropriate image that needs to be displayed.

When playing with two human players the board updates perfectly, and after this the program waits for input from the next player, and I have have run many games without any problems.

I also have a computer player that selects its move using an algorithm, and I'm currently trying to set up a game with two computer players against each other. (I have not tried a human vs. computer game yet).

The problem is that when the updateBoard method is called none of the images are updated, although the NSLog readout at the bottom tells me that everything ran ok, and I can actually see the two players playing the game via the console.

I assume that this has something to do with the fact that immediately after calling updateBoard I'm passing control to the next computer player and somehow that is preventing the UIImageViews from updating correctly. I did a test to comment out the line of code where the next player is called (meaning that only one move is ever made and then execution stops) and the board did update correctly.

Sorry for the long question, thanks in advance for any suggestions!

like image 751
SteveB Avatar asked Dec 07 '22 12:12

SteveB


1 Answers

The user interface is only updated when the program control returns to the main runloop. You should do the computation in a background thread, and call

dispatch_async(dispatch_get_main_queue(), ^{
   [self updateBoard];
});

from the background thread when you want update the display. Then you will not block the UI while doing the computation.

like image 118
Martin R Avatar answered Jan 06 '23 18:01

Martin R