现在的位置: 首页 > 综合 > 正文

C# iphone MontTouch UISegmentedControl 的使用

2014年09月05日 ⁄ 综合 ⁄ 共 2215字 ⁄ 字号 评论关闭

想学用C#开发iphone 的就加入 QQ群:178290571 ,让我们共同进步吧!

C# MonoTouch for iphone 开发blog  http://blog.csdn.net/ssihc0/

MonoDevelop 版本:2.8.0

MonoTouch 版本:4.2.2

UISegmentedControl是一个按钮分组控件,每个分段都作为独立的按钮起作用。每个分段可以显示文本或图像。但不能同时显示两者。如果没有指定分段的宽度,那么基于分段总数量进行平分宽度。

下面是UISegmentedControl常用的属性:

 ControlStyle 使用UISegmentedControl枚举选择 Plain,Bordered,Bar,Bezeled 其中的一种样式。

NumberOfSegments 只读属性。表示多少个分段数量。

下面是方法和属性:

新建一个工程名为SegmentedControl 打开SegmentedControlViewController

添加下面代码:

public partial class SegmentedControlViewController : UIViewController
	{
		private UISegmentedControl segmentedControl;
		public SegmentedControlViewController (string nibName, NSBundle bundle) : base (nibName, bundle)
		{
		}
		
		public override void DidReceiveMemoryWarning ()
		{
			// Releases the view if it doesn't have a superview.
			base.DidReceiveMemoryWarning ();
			
			// Release any cached data, images, etc that aren't in use.
		}
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			segmentedControl= new UISegmentedControl(new System.Drawing.RectangleF(30f,35f,260f,45f));
			segmentedControl.InsertSegment("First",1,false);
			segmentedControl.InsertSegment("Second",2,false);
			segmentedControl.InsertSegment("Third",3,false);
			segmentedControl.SelectedSegment=0;
			segmentedControl.ControlStyle=UISegmentedControlStyle.Plain;
			segmentedControl.ValueChanged+= segmentedControl_ValueChanged;
			var label = new UILabel(new System.Drawing.RectangleF(30f,75f,260f,45f));
			label.BackgroundColor=UIColor.Clear;
			label.Tag=10;
			this.View.AddSubview(segmentedControl);
			this.View.AddSubview(label);
			
			//any additional setup after loading the view, typically from a nib.
		}
		private void segmentedControl_ValueChanged(object sender ,EventArgs e)
		{
			((UILabel)this.View.ViewWithTag(10)).Text=segmentedControl.SelectedSegment.ToString ();
		}
		
		public override void ViewDidUnload ()
		{
			base.ViewDidUnload ();
			
			// Release any retained subviews of the main view.
			// e.g. myOutlet = null;
		}
		
		public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
		{
			// Return true for supported orientations
			return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
		}
	}

代码分析:

  segmentedControl.InsertSegment("First",1,false); 添加 一个text 为First 默认不选中。

  label.Tag=10;  设置控制的id。这里的10一般要大于10 。

((UILabel)this.View.ViewWithTag(10)).Text=segmentedControl.SelectedSegment.ToString ();

通过用view的ViewWithTag 方法,根据id的值来找到控件。

运行结果:

源代码:

下载

下载后把gif 改成zip

抱歉!评论已关闭.