First, use the For loop to create the view on the emulator
for (Nsinteger i = 0; i < 6; i++) {
for (Nsinteger j = 0; J <; J + +) {
// Create and light bulb view equal number of buttons to control the lamp switch
UIButton *lightbutton = [Uibuttonbuttonwithtype:uibuttontypesystem];
Lightbutton.frame = CGRectMake (i *, + (J * 64), 64,64);
//Add a picture to button
[Lightbutton setbackgroundimage:[uiimage imagenamed:@ "2"]forstate:uicontrolstatenormal];
//to associate an implementation method for button buttons
[Lightbutton addtarget:self Action: @selector (Pressbutton:) forcontrolevents:uicontroleventtouchupinside];
//set the Tag value for button buttons for easy search
Lightbutton.tag = + J + + * I;
//to add a button to the window view
[Self.window Addsubview:lightbutton];
}
}
if you want to implement a light bulb switch, you need a way to click on the button
-(void) Pressbutton: (UIButton *) button {
//randomly Select a button, then click on it can affect the button up to five, respectively, and up and down
Nsintegerarray[5] = {Button.tag, Button.tag + 1, Button.tag + 100,button.tag-1, button.tag-100};
if ([Button.currentbackgroundimage isequal:[uiimage imagenamed:@ "2"]) {
for (Nsinteger i = 0; i < 5; i++) {
UIButton *alightbutton = (UIButton *) [self.windowviewwithtag:array[i]];
if ([Alightbutton.currentbackgroundimage isequal:[uiimageimagenamed:@ "1"]) {
[Alightbutton setbackgroundimage:[uiimage imagenamed:@ "2"]forstate:uicontrolstatenormal];
} else {
[Alightbutton setbackgroundimage:[uiimage imagenamed:@ "1"]forstate:uicontrolstatenormal];
}
}
} else{
for (Nsinteger i = 0; i < 5; i++) {
UIButton *alightbutton = (UIButton *) [self.windowviewwithtag:array[i]];
if ([Alightbutton.currentbackgroundimage isequal:[uiimageimagenamed:@ "2"]) {
[Alightbutton setbackgroundimage:[uiimage imagenamed:@ "1"]forstate:uicontrolstatenormal];
} else {
[Alightbutton setbackgroundimage:[uiimage imagenamed:@ "2"]forstate:uicontrolstatenormal];
}
}
}
}
Lightsout--easy