2012-03-29 17 views
0

Cocoa TouchでUIButtonを動的に作成するにはどうすればよいですか?Cocoa TouchでUIButtonを動的に作成するにはどうしたらいいですか?

+0

'のために(int i = 0; i <10; i ++){UIButton * but = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:but]; } '、これはボタンを作成しますが、これは少しの研究で見つけられるはずです。どのような問題がありますか? – iNoob

+0

ちょっと@Vicky、最初に検索して質問を投稿してください:) – Krunal

答えて

1
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the size of the button 
    [myButton setTitle:@"Click Me!" forState:UIControlStateNormal]; 
    // add targets and actions 
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    // add to a view 
    [superView addSubview:myButton]; 

は、これはコーディング

ハッピーあなたの問題を解決し

1

それは非常に簡単です

UIImage * buttonImage = [UIImage imageNamed:@ "タブバー - refresh.png"];

refresh = [UIButton buttonWithType:UIButtonTypeCustom]; 
refresh.frame = CGRectMake(frame values); 
[refresh setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
[refresh addTarget:self action:@selector(refreshPressed:) forControlEvents:UIControlEventTouchUpInside];  
[self.view addSubview:refresh]; 
0

ここにコードがある...

UIButton *btnDone; 
btnDone = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btnDone setFrame:CGRectMake(0, 0, 28, 29)]; 

// For setting an image to button.... 
[btnDone setImage:[UIImage imageNamed:@"done.png"] forState:UIControlStateNormal]; 
[btnDone setImage:[UIImage imageNamed:@"done_hover.png"] forState:UIControlStateHighlighted]; 

// Add target to button... 
[btnDone addTarget:self action:@selector(btnDoneAction:) forControlEvents:UIControlEventTouchUpInside]; 

//Method implementation.. 
-(void)btnDoneAction:(id)sender 
{ 
    //Your stuff.. 
} 

希望、これはあなたが....役立ちます:)

1

あなたがそのような何かを行うことができます。

int yOfs = 0; 
for (int index = 0; index<10; index++) { 
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [aButton setBackgroundImage:[UIImage imageNamed:@"buttonImage"] forState:UIControlStateNormal]; 
    [aButton setTitle:[NSString stringWithFormat:@"Button %d",index] forState:UIControlStateNormal]; 
    [aButton setFrame:CGRectMake(20, yOfs, 100, 50)]; 
    [aButton addTarget:self action:@selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [aView addSubview:aButton]; 

    yOfs += 50; 
} 


- (IBAction)aButtonClicked:(id)sender 
{ 
    NSLog(@"Clicked on button with title %@",[sender titleLabel].text); 
} 
+0

あなたのボタンを配列の中にあなたの例のために保存する方が良いです – Gargo

関連する問題