[英]How to handle a click event for Button that placed inside a Control Object?
[英]Handle button click event of button list inside UIScrollView Monotouch
我的 iPhone 應用程序的 View 上有一個 UIScrollView 和一個 UIPageControl。 我用 10 個按鈕的列表填充此 Scrollview。 這些按鈕都正確填充,我可以完美地滾動它們。 我的問題是:如何將點擊事件連接到每個按鈕? 每個按鈕將執行基本相同的任務(播放聲音),但是每個按鈕的聲音會有所不同。 這些按鈕是通過以下方法以編程方式創建的:
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() 方法中調用。
如何將點擊事件鏈接到這些按鈕中的每一個? 我在互聯網上搜索了很多,但無濟於事。
將匿名方法連接到 for 循環中的每個點擊事件怎么樣?
button.TouchUpInside += (s, e) => { //play i.mp3
};
我不確定如何實際播放聲音文件,但您可以將聲音文件命名為 i.*,因為這就是您的按鈕的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.