2017-03-31 80 views
-8

如何使用循環來創建一個10個UIButtons呈圓形與數字文本如何創建圓形10個UIButtons在for循環中

for(i=0;i<=10;i++) { 
    j=j+10; 

    UIButton *bt = [[UIButton alloc]initWithFrame:CGRectMake(10, j+10, 50, 50)]; 
    bt.backgroundColor = [UIColor redColor]; 

    bt.layer.cornerRadius = 0.5 * bt.frame.size.width; 
    [bt setTitle:@"click" forState:UIControlStateNormal]; 
    [bt addTarget:nil action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:bt]; 

} 
+1

什麼是你遇到的問題? – Carcigenicate

+0

你爲什麼要加10到j,2次? – Honey

+0

@親愛的,他需要指定j –

回答

4

你所看到的只是「一鍵通」被垂直拉長,這讓你認爲你只創建一個按鈕而不是11?

其原因是偏移您應用代碼中:

j = j + 10; 

利用上述,你看到11 UIButton小號彼此重疊,因此幻覺,有隻有一個按鈕被垂直拉長。

嘗試改變的偏移量:

j = j + 50; 

你會看到,那麼,11個UIButton S中的屏幕上垂直。

順便說一句,如果你的意思是隻創建10個按鈕,而不是11,狀態應改爲:

for(int i = 0; i < 10; i++) //instead of using <= 

,不要忘了初始化j;否則你什麼也看不到:

int j = 0; //do this before you enter the loop 

如果你沒有這樣做,你應該也收到警告。具有上述修改10個UIButton S的


結果:

enter image description here

+0

我想顯示圓形按鈕的十個圓形按鈕 –