有不明白的地方欢迎入群 347636249 探讨
using System;
//
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Drawing;
namespace GCForum
{
public class MyTestVC :UIViewController
{
public MyTestVC ()
{
}
private UISegmentedControl _segmentedControl;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.View.BackgroundColor = UIColor.White;
UILabel lbl = chongqing.ControlCenter.CreateLbl (new RectangleF (10, 100, 150, 20), "");
this.View.Add (lbl);
_segmentedControl = new UISegmentedControl (new object[] {
"one","two","three","four"
});
_segmentedControl.Frame = new RectangleF (10, 10, this.View.Frame.Width - 20, 50);
_segmentedControl.ControlStyle = UISegmentedControlStyle.Bar;
_segmentedControl.TintColor = UIColor.Blue;
_segmentedControl.SetImage (UIImage.FromFile ("theme/red/courwatch.png"), 0);
_segmentedControl.SelectedSegment = 1;//让第二项选中
//===============>two
//lbl.Text = _segmentedControl.TitleAt (_segmentedControl.SelectedSegment) ?? "title not set";
//注册VlueChange事件
_segmentedControl.ValueChanged += (s,e) => {
lbl.Text = _segmentedControl.TitleAt (_segmentedControl.SelectedSegment) ?? "title not set";
};
this.View.AddSubview (_segmentedControl); //注意是AddSubview方法,非Add
}
//...
}
}
基本上,UISegmentedControl就是视图标签控件。虽然也可用它创建分类菜单,但它用的最多的还是让用户在一组特定的子视图中互动,有效地对视图分组。
这个控件由多个按钮组成,分成不同的标签页。每个按钮都可以有自己的标题和图标,而控件本身可以采用不同的风格。
这里做一下说明,mono touch工程中有一个Resources文件夹,它是当前项目文件的资源文件夹,所有的资源文件可放入(只是一个默认的,不是说非要放进去,可自己定义目录文件夹都行),所以这里的UIImage.FromFile ("theme/red/courwatch.png")可以自动解析到该文件夹下。
当然了,这里主要的还是ValueChanged方法,从最近的几篇文章来看,有些控件都有这个ValueChanged的共有方法,可考虑写做一定的设计动态处理,当然这是后话了。