/** Here we use button to do because many times need to text also need to display the picture, so button more appropriate */-(void) creatorbtn{//Number of columns Nsinteger column = 4; Number of buttons Nsinteger buttoncount = 18; The width and height of the button cgfloat buttonw = Self.view.frame.size.width/column; CGFloat buttonh = buttonw; for (int i = 0; i < Buttoncount; i++) {UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom]; Btn.frame = CGRectMake ((((i) column) * buttonw), ((i/column) * buttonh), buttonW-10, buttonH-10); NSLog (@ "%@", Nsstringfromcgrect (Btn.frame)); [Btn Setbackgroundcolor:[uicolor Redcolor]; Bind tag, listen behind Click Btn.tag = i; [Self.view ADDSUBVIEW:BTN]; Listen button click [btn addtarget:self Action: @selector (btnaction:) forcontrolevents:uicontroleventtouchupinside]; }}-(void) Btnaction: (UIButton *) btn{//According to the button's tag to listen for click NSLog (@ "Clicked the%ld button", (long) btn.tag); }
IOS Custom Nine Gongge