[英]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.