UIButton を押したら AlertViewが出てくるようにイベントを設定してみようと思います。
(iOS 5 で試してます。)
Button にイベントを設定するには、このメソッドを使います。
addTarget:action:forControlEvents:
サンプルはこんな感じになります。
(ViewController.mに実装してます。)
//ボタンを押したときに呼び出すメソッド
– (void)pushMyButton
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”ボタンのアラート“ message:@”ボタンをPushしました。“ delegate:nil cancelButtonTitle:@”OK” otherButtonTitles:nil];
[alert show];
}
– (void)viewDidLoad
{
[superviewDidLoad];
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 200, 50);
btn.center = CGPointMake(self.view.center.x, 100);
[btn setTitle:@”Push!”forState:UIControlStateNormal];
// ボタンのアクションを設定する
[btn addTarget:selfaction:@selector(pushMyButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}