按鈕是用戶交互的最基礎控件。即便是在iPhone或者iPad中,用戶使用最多操做也是經過觸摸實現點擊。而點擊操做最多的控件每每是按鈕控件。通常使用UIButton類來實現按鈕。本節將主要講解按鈕相關的內容。ide
因爲按鈕拖放的方式比較簡單,因此再也不介紹。這裏直接講解代碼中如何添加按鈕。使用代碼爲主視圖添加一個按鈕的方式和在2.2.2節中講解的步驟是同樣的。首先須要使用UIButton類實例化一個按鈕對象,而後是設置位置和大小,最後是使用AddSubview()方法將按鈕對象添加到主視圖中。(因爲視圖的添加方式都同樣,後面將省略使用代碼添加視圖這塊內容。)。ui
【示例2-5】如下將使用代碼爲主視圖添加一個青色的按鈕。代碼以下:this
using System;spa
using System.Drawing;orm
using MonoTouch.Foundation;對象
using MonoTouch.UIKit;繼承
namespace Application教程
{事件
public partial class __16ViewController : UIViewController開發
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIButton button = new UIButton (); //實例化按鈕對象
button.Frame = new RectangleF (120, 261, 80, 30); //設置按鈕對象的位置和大小
button.BackgroundColor = UIColor.Cyan; //設置按鈕對象的背景顏色
this.View.AddSubview (button); //將按鈕對象添加到主視圖中
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.13所示。
圖2.13 運行效果
注意:因爲按鈕視圖繼承了UIView類,因此它繼承了UIView類中的屬性和方法。
在圖2.13中能夠看到,明明是添加了一個按鈕,可是就和添加了一個空白視圖同樣,爲了讓按鈕和空白視圖區別開,須要對按鈕進行一些設置。
1.設置按鈕的外觀
外觀是直接區別按鈕和其餘視圖的手段。若是是使用Interface Builder添加的按鈕,它的外觀設置方式有兩種,一種是直接打開屬性界面,對按鈕外觀的進行設置,如圖2.14所示。
圖2.14 按鈕的設置
另外一種就是使用代碼對按鈕的外觀進行設置。這一種方式適用於使用代碼添加的按鈕中。表2-2列出了經常使用的一些外觀設置屬性。
表2-2 經常使用屬性
【示例2-6】下面將在主視圖中添加一個按鈕。此按鈕的標題爲I am button,標題的顏色爲黑色。代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __18ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIButton button = new UIButton ();
button.Frame = new RectangleF (107, 269, 120, 30);
button.SetTitle ("I am button", UIControlState.Normal); //設置按鈕的標題
button.SetTitleColor (UIColor.Black, UIControlState.Normal); //設置按鈕的標題顏色
this.View.AddSubview (button);
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.15所示。
圖2.15 運行效果
2.設置按鈕的狀態
在示例2-6中,設置按鈕的標題和顏色時,須要對按鈕的狀態進行設置,表示按鈕在某一狀態下的標題和標題顏色是什麼樣子。例如,UIControlState.Normal就表示按鈕的一種狀態。對於像按鈕的這類視圖,便可以接受用戶輸入的視圖也被稱爲控件。這些控件都有本身的狀態。表2-3就爲開發者詳細介紹了控件的狀態。
表2-3 控件的狀態
3.設置按鈕的類型
按鈕的形式是多種多樣的。例如,在通信錄中,添加新聯繫人的按鈕是一個加號;查看來電的詳細信息時是一個感嘆號等。這些按鈕的實現,能夠在實例化按鈕對象時使用UIButtonType來實現。UIButtonType中的內容如表2-4所示。
表2-4 UIButtonType的內容
【示例2-7】如下代碼將設置兩個不一樣風格的按鈕。代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __19ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
//實例化按鈕對象並設置按鈕的類型
UIButton button1 = new UIButton (UIButtonType.DetailDisclosure);
button1.Center = new PointF (160, 150); //設置按鈕的中心位置
this.View.AddSubview (button1);
//實例化按鈕對象並設置按鈕的類型
UIButton button2 = new UIButton (UIButtonType.ContactAdd);
button2.Center = new PointF (160, 350); //設置按鈕的中心位置
this.View.AddSubview (button2);
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.16所示。
圖2.16 運行效果
4.設置按鈕的發光效果
發光的按鈕開發者可能在不少的地方遇到過,它的實現其實很簡單,就是使用了ShowsTouchWhenHighlighted屬性來實現的。
【示例2-8】如下代碼將實現一個發光的按鈕。代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __17ViewController : UIViewController
{
…… //這裏省略了視圖加載和卸載先後的一些方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIButton button = new UIButton ();
button.Frame = new RectangleF (137, 269, 46, 30);
button.SetTitle ("Hello", UIControlState.Normal);
this.View.AddSubview (button);
button.ShowsTouchWhenHighlighted = true; //按鈕發光的設置
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.17所示。
圖2.17 運行效果
以上講解了按鈕的格式化設置。做爲按鈕,最重要的功能就是實現和用戶的響應工做。它的實現事件是TouchUpInside。其語法形式以下:
按鈕對象.TouchUpInside +=觸摸按鈕後的方法;
或者是:
按鈕對象名.TouchUpInside +=(sender,e)=>{
……
};
其中,sender表示事件監視的對象,e就是事件所須要的數據。
【示例2-9】如下就爲開發者實現了按鈕的響應。當用戶觸摸按鈕後,主視圖就會變色。代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __3ViewController : UIViewController
{
UIButton buttonChangeColor;
bool isYellow;
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
//建立按鈕buttonChangeColor
private void CreateButton ()
{
RectangleF viewFrame = this.View.Frame;
RectangleF buttonFrame = new RectangleF (10f, viewFrame.Bottom - 200f,
viewFrame.Width - 20f, 50f);
this.buttonChangeColor = UIButton.FromType (UIButtonType.System); //實例化對象
//對按鈕的格式化設置
this.buttonChangeColor.Frame = buttonFrame;
this.buttonChangeColor.SetTitle ("Tap to change view color", UIControlState.Normal);
this.buttonChangeColor.SetTitle ("Changing color...", UIControlState.Highlighted);
//實現響應
this.buttonChangeColor.TouchUpInside += this.ButtonChangeColor_TouchUpInside;
this.View.AddSubview (this.buttonChangeColor);
}
//實現觸摸按鈕後改變主視圖的背景顏色
private void ButtonChangeColor_TouchUpInside (object sender, EventArgs e)
{
if (isYellow) {
this.View.BackgroundColor = UIColor.LightGray;
isYellow = false;
} else {
this.View.BackgroundColor = UIColor.Yellow;
isYellow = true;
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
this.CreateButton (); //調用CreateButton()方法
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
在此程序中,有一個isYellow,它是一個布爾類型的變量,當此變量爲true時,將主視圖的背景改成淺灰色。當此變量爲false時,將主視圖的背景改成黃色。運行效果如圖2.18所示。
圖2.18 運行效果
本文選自:Xamarin iOS開發實戰大學霸內部資料,轉載請註明出處,尊重技術尊重IT人!