// Layout the lamp image in the view. Five rows and five columns for (INT I = 0; I <5; I ++) {for (Int J = 0; j <5; j ++) {uibutton * lightbutton = [uibutton buttonwithtype: uibuttontypesystem]; lightbutton. frame = cgrectmake (I * 64,120 + (J * 64), 64, 64); [lightbutton setbackgroundimage: [uiimage imagenamed: @ "2.png"] forstate: uicontrolstatenormal]; [lightbutton addtarget: self action: @ selector (Click :) forcontrolevents: uicontroleventtouchupinside]; [self. window addsubview: lightbutton]; lightbutton. tag = 100 + J + 10 * I; // The tag difference between the left and right buttons is 1, and the tag difference between the upper and lower buttons is 10 }}
// Button click event-(void) Click :( uibutton *) button {// defines the button clicked by the array storage and its upper and lower right button int array [5] = {button. tag, button. tag + 1, button. tag + 10, button. tag-1, button. tag-10}; // when the front butonbackground image is not a bright light (2.png is a bright image name) if ([button. currentbackgroundimage isequal: [uiimage imagenamed: @ "2.png"]) {// traverse the array for (INT I = 0; I <5; I ++) {uibutton * abutton = (uibutton *) [self. window viewwithtag: array [I]; // specifies the status of the top left and right buttoners that are clicked on. If it is an out-of-the-box lamp, change the background image to a bright lamp (1.png is an out-of-Box Lamp image) If. currentbackgroundimage isequal: [uiimage imagenamed: @ "1.png"]) {[abutton failed: [uiimage imagenamed: @" 2.png"] forstate: uicontrolstatenormal];} else {[abutton failed: [uiimage imagenamed: @ "1.png"] forstate: uicontrolstatenormal] ;}// else checks whether the background image of the current buton is out of light.} else {for (INT I = 0; I <5; I ++) {uibutton * abutton = (uibutton *) [self. window viewwithtag: array [I]; If ([abutton. currentbackgroundimage isequal: [uiimage imagenamed: @ "2.png"]) {[abutton success: [uiimage imagenamed: @" 1.png"] forstate: uicontrolstatenormal];} else [abutton failure: [uiimage imagenamed: @ "2.png"] forstate: uicontrolstatenormal] ;}}
OC enables simple light-off games