2012-05-06 12 views
0

私のiPhoneアプリケーションで私のビューにUIScrollViewとUIPageControlがあります。私はこのScrollviewに10個のボタンのリストを入力します。ボタンはすべて正しく挿入され、完全にスクロールできます。私の質問は、それぞれのボタンにクリックイベントを接続するにはどうすればいいですか?各ボタンは本質的に同じタスク(音を鳴らす)を実行しますが、サウンドはこれらのボタンごとに異なります。ボタンは、以下の方法でプログラム的に作成される:UIScrollView Monotouch内のボタンリストのハンドルボタンクリックイベント

private void CreatePanels() 
    { 
     scrollView.Scrolled += ScrollViewScrolled; 

     int count = 10; 
     RectangleF scrollFrame = scrollView.Frame; 
     scrollFrame.Width = scrollFrame.Width * count; 
     scrollView.ContentSize = scrollFrame.Size; 

     for (int i=0; i<count; i++) 
     { 

      float h = 150.0f; 
      //float w = 50.0f; 
      float padding = 10.0f; 
      int n = 25; 

      var button = UIButton.FromType (UIButtonType.RoundedRect); 
      button.SetTitle (i.ToString(), UIControlState.Normal); 
      UIImage img = new UIImage("Images/btntest.png"); 
      button.SetImage(img, UIControlState.Normal); 
      button.Frame = new RectangleF (
         (padding + 40) * (i + 1) + (i * (View.Frame.Width - 100)) , 
         padding,       
         View.Frame.Width - 100,   
         h); 

      RectangleF frame = scrollView.Frame; 
      PointF location = new PointF(); 
      location.X = frame.Width * i; 

      frame.Location = location; 
      button.Frame = frame; 

      scrollView.AddSubview(button); 
     } 

     pageControl.Pages = count; 
    } 

    private void ScrollViewScrolled (object sender, EventArgs e) 
    { 
     double page = Math.Floor(((scrollView.ContentOffset.X - scrollView.Frame.Width)/2)/scrollView.Frame.Width) + 1; 

     pageControl.CurrentPage = (int)page; 
    } 

CreatePanels()メソッドは、UIScrollViewのを移入のviewDidLoad()メソッド内で呼び出されます。

これらのボタンのそれぞれにクリックイベントをリンクさせてください。私はインターネットをたくさん検索しましたが、無駄です。どんな援助も評価されるだろう。

ありがとうございます。 J

答えて

2

匿名メソッドをforループの各クリックイベントに配線するのはどうですか?

button.TouchUpInside += (s, e) => { //play i.mp3
};

私は実際にサウンドファイルを再生するかどうかはわかりませんが、あなたは私のように名前のサウンドファイルを持つことができます。*それはあなたのボタンが命名されているものであるから。

+0

ありがとうございます。それは私の問題を解決しました。ボタンをクリックしたかどうかを知るために、buttonタグのプロパティをiに設定する必要があります。ご協力いただきありがとうございます。いいぞ! –

関連する問題