1
\$\begingroup\$

I am starting to write code for a keyboard in iOS. It's pretty much hard-coded and I need suggestions on how I can make it more professional style and more efficient.

 //row 1
UIButton *row1Btn1=(UIButton *)[_lettersRow1View viewWithTag:11];
UIButton *row1Btn2=(UIButton *)[_lettersRow1View viewWithTag:12];
UIButton *row1Btn3=(UIButton *)[_lettersRow1View viewWithTag:13];
UIButton *row1Btn4=(UIButton *)[_lettersRow1View viewWithTag:14];
UIButton *row1Btn5=(UIButton *)[_lettersRow1View viewWithTag:15];
UIButton *row1Btn6=(UIButton *)[_lettersRow1View viewWithTag:16];
UIButton *row1Btn7=(UIButton *)[_lettersRow1View viewWithTag:17];
UIButton *row1Btn8=(UIButton *)[_lettersRow1View viewWithTag:18];
UIButton *row1Btn9=(UIButton *)[_lettersRow1View viewWithTag:19];
UIButton *row1Btn10=(UIButton *)[_lettersRow1View viewWithTag:20];
UIButton *row1Btn11=(UIButton *)[_lettersRow1View viewWithTag:21];
UIButton *row1Btn12=(UIButton *)[_lettersRow1View viewWithTag:22];



[row1Btn1 setTitle:items[0] forState:UIControlStateNormal];
[row1Btn2 setTitle:items[1] forState:UIControlStateNormal];
[row1Btn3 setTitle:items[2] forState:UIControlStateNormal];
[row1Btn4 setTitle:items[3] forState:UIControlStateNormal];
[row1Btn5 setTitle:items[4] forState:UIControlStateNormal];
[row1Btn6 setTitle:items[5] forState:UIControlStateNormal];
[row1Btn7 setTitle:items[6] forState:UIControlStateNormal];
[row1Btn8 setTitle:items[7] forState:UIControlStateNormal];
[row1Btn9 setTitle:items[8] forState:UIControlStateNormal];
[row1Btn10 setTitle:items[9] forState:UIControlStateNormal];
[row1Btn11 setTitle:items[10] forState:UIControlStateNormal];
[row1Btn12 setTitle:items[11] forState:UIControlStateNormal];


//row 2

UIButton *row2Btn1=(UIButton *)[_lettersRow2View viewWithTag:23];
UIButton *row2Btn2=(UIButton *)[_lettersRow2View viewWithTag:24];
UIButton *row2Btn3=(UIButton *)[_lettersRow2View viewWithTag:25];
UIButton *row2Btn4=(UIButton *)[_lettersRow2View viewWithTag:26];
UIButton *row2Btn5=(UIButton *)[_lettersRow2View viewWithTag:27];
UIButton *row2Btn6=(UIButton *)[_lettersRow2View viewWithTag:28];
UIButton *row2Btn7=(UIButton *)[_lettersRow2View viewWithTag:29];
UIButton *row2Btn8=(UIButton *)[_lettersRow2View viewWithTag:30];
UIButton *row2Btn9=(UIButton *)[_lettersRow2View viewWithTag:31];
UIButton *row2Btn10=(UIButton *)[_lettersRow2View viewWithTag:32];
UIButton *row2Btn11=(UIButton *)[_lettersRow2View viewWithTag:33];
UIButton *row2Btn12=(UIButton *)[_lettersRow2View viewWithTag:34];

[row2Btn1 setTitle:items[12] forState:UIControlStateNormal];
[row2Btn2 setTitle:items[13] forState:UIControlStateNormal];
[row2Btn3 setTitle:items[14] forState:UIControlStateNormal];
[row2Btn4 setTitle:items[15] forState:UIControlStateNormal];
[row2Btn5 setTitle:items[16] forState:UIControlStateNormal];
[row2Btn6 setTitle:items[17] forState:UIControlStateNormal];
[row2Btn7 setTitle:items[18] forState:UIControlStateNormal];
[row2Btn8 setTitle:items[19] forState:UIControlStateNormal];
[row2Btn9 setTitle:items[20] forState:UIControlStateNormal];
[row2Btn10 setTitle:items[21] forState:UIControlStateNormal];
[row2Btn11 setTitle:items[22] forState:UIControlStateNormal];
[row2Btn12 setTitle:items[23] forState:UIControlStateNormal];


//row 3

UIButton *row3Btn1=(UIButton *)[_lettersRow3View viewWithTag:35];
UIButton *row3Btn2=(UIButton *)[_lettersRow3View viewWithTag:36];
UIButton *row3Btn3=(UIButton *)[_lettersRow3View viewWithTag:37];
UIButton *row3Btn4=(UIButton *)[_lettersRow3View viewWithTag:38];
UIButton *row3Btn5=(UIButton *)[_lettersRow3View viewWithTag:39];
UIButton *row3Btn6=(UIButton *)[_lettersRow3View viewWithTag:40];
UIButton *row3Btn7=(UIButton *)[_lettersRow3View viewWithTag:41];
UIButton *row3Btn8=(UIButton *)[_lettersRow3View viewWithTag:42];
UIButton *row3Btn9=(UIButton *)[_lettersRow3View viewWithTag:43];
UIButton *row3Btn10=(UIButton *)[_lettersRow3View viewWithTag:44];
UIButton *row3Btn11=(UIButton *)[_lettersRow3View viewWithTag:45];
UIButton *row3Btn12=(UIButton *)[_lettersRow3View viewWithTag:46];

[row3Btn1 setTitle:items[24] forState:UIControlStateNormal];
[row3Btn2 setTitle:items[25] forState:UIControlStateNormal];
[row3Btn3 setTitle:items[26] forState:UIControlStateNormal];
[row3Btn4 setTitle:items[27] forState:UIControlStateNormal];
[row3Btn5 setTitle:items[28] forState:UIControlStateNormal];
[row3Btn6 setTitle:items[29] forState:UIControlStateNormal];
[row3Btn7 setTitle:items[30] forState:UIControlStateNormal];
[row3Btn8 setTitle:items[31] forState:UIControlStateNormal];
[row3Btn9 setTitle:items[32] forState:UIControlStateNormal];
[row3Btn10 setTitle:items[33] forState:UIControlStateNormal];
[row3Btn11 setTitle:items[34] forState:UIControlStateNormal];
[row3Btn12 setTitle:items[35] forState:UIControlStateNormal];

//row 4

UIButton *row4Btn1=(UIButton *)[_numbersRow1View viewWithTag:47];
UIButton *row4Btn2=(UIButton *)[_numbersRow1View viewWithTag:48];
UIButton *row4Btn3=(UIButton *)[_numbersRow1View viewWithTag:49];
UIButton *row4Btn4=(UIButton *)[_numbersRow1View viewWithTag:50];
UIButton *row4Btn5=(UIButton *)[_numbersRow1View viewWithTag:51];
UIButton *row4Btn6=(UIButton *)[_numbersRow1View viewWithTag:52];
UIButton *row4Btn7=(UIButton *)[_numbersRow1View viewWithTag:53];
UIButton *row4Btn8=(UIButton *)[_numbersRow1View viewWithTag:54];
UIButton *row4Btn9=(UIButton *)[_numbersRow1View viewWithTag:55];
UIButton *row4Btn10=(UIButton *)[_numbersRow1View viewWithTag:56];


[row4Btn1 setTitle:items[36] forState:UIControlStateNormal];
[row4Btn2 setTitle:items[37] forState:UIControlStateNormal];
[row4Btn3 setTitle:items[38] forState:UIControlStateNormal];
[row4Btn4 setTitle:items[39] forState:UIControlStateNormal];
[row4Btn5 setTitle:items[40] forState:UIControlStateNormal];
[row4Btn6 setTitle:items[41] forState:UIControlStateNormal];
[row4Btn7 setTitle:items[42] forState:UIControlStateNormal];
[row4Btn8 setTitle:items[43] forState:UIControlStateNormal];
[row4Btn9 setTitle:items[44] forState:UIControlStateNormal];
[row4Btn10 setTitle:items[45] forState:UIControlStateNormal];
\$\endgroup\$

closed as unclear what you're asking by forsvarir, vnp, Mast, t3chb0t, mdfst13 Oct 15 '16 at 14:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ What is items? Where is that defined? And is _numbersRow1View created in IB? (i.e, is that assigned through an IBOutlet?) \$\endgroup\$ – A O Oct 14 '16 at 18:00
1
\$\begingroup\$

Perhaps using the UICollectionView class will resolve your issue?

  1. Create UIViewController

  2. Add UICollectionView on storyboard

  3. Add @property (nonatomic, weak) @IBOutlet UICollectionView *collectionView

  4. Set delegate and datasource for UICollectionView:

    collectionView.delegate = self
    collectionView.dataSource = self
    

    Don't forget to add conformation of UICollectionViewDelegate/ UICollectionViewDataSource to controller.

  5. Add @property (nonatomic, assign) NSInteger buttonsCount;

  6. Implement dataSource methods:

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return buttonsCount;
    }
    
  7. Create a custom UICollectionViewCell and implement this method:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        ...
        cell.text = indexPath.row
        return cell
    }
    

For more detailed information, please read this link.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.