若是須要向用戶顯示一條很是重要的消息時,警告視圖(UIAlertView類)就能夠派上用場了。它的功能是把須要注意的信息顯示給用戶。通常顯示一條信息,或者是顯示一條信息和幾個按鈕。本節將主要講解如何爲主視圖添加警告視圖,如何將警告視圖進行顯示、如何以不一樣的形式顯示警告視圖以及響應警告視圖。ide
在工具欄中是沒有警告視圖的,開發者必須使用代碼的形式在主視圖中進行添加。它的添加和其餘視圖的添加是有區別的。具體步驟以下:工具
1.實例化對象this
UIAlertView類提供的警告視圖,因此在使用前,必需要進行實例化。其語法形式以下:spa
UIAlertView 對象名=new UIAlertView();orm
2.顯示對象
在實例化對象之後,就是此警告視圖對象進行顯示。此時不須要使用AddSubview()方法,而須要使用Show()方法,其語法形式以下:教程
對象名.Show();事件
【示例2-27】如下將實如今主視圖中顯示一個警告視圖的功能。代碼以下:內存
using System;開發
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __25ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIAlertView alertview = new UIAlertView (); //實例化警告視圖對象
alertview.Show (); //顯示
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.46所示。
圖2.46 運行效果
在一個應用程序中,警告視圖的樣式是多種多樣的。如下將講解用戶在應用程序中常見到的四種警告視圖樣式。
1.無按鈕的警告視圖
在有的警告視圖中是不須要要有按鈕的,固然,它不須要用戶在此視圖上作任何的操做,這就是無按鈕的警告視圖。
【示例2-28】如下就是一個簡單的無按鈕的警告視圖的實現。代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __26ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIAlertView alertView = new UIAlertView ();
alertView.Title = "提示"; //設置警告視圖的標題
alertView.Message = "電量不足"; //設置警告視圖向用戶顯示的信息
alertView.Show ();
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.47所示
圖2.47 運行效果
2.具備一個按鈕的警告視圖
具備一個按鈕的警告視圖,通常是想要強制性的引發用戶的注意。若是用戶沒有注意到此視圖,那麼它會一直存在,直到用戶注意此視圖,並使用此視圖中提示的按鈕去關閉,警告視圖纔會消失。如何爲警告視圖添加按鈕呢,須要使用AddButton()方法實現,其語法形式以下:
警告視圖對象名.AddButton(「按鈕標題」);
【示例2-29】如下就是一個具備一個按鈕的警告視圖的實現。代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __27ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIAlertView alertView = new UIAlertView ();
alertView.Title="提示";
alertView.Message="內存空間不足";
alertView.AddButton ("前去清理"); //爲警告視圖添加按鈕
alertView.Show ();
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.48所示。
圖2.48 運行效果
3.具備多個按鈕的警告視圖
有時,在警告視圖中有一個按鈕是沒法知足須要的,須要在此視圖中添加多個按鈕,從而讓用戶實現多方面的選擇。此時仍是須要使用AddButton()方法。
【示例2-30】如下就是具備多個按鈕的警告視圖的效果,代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __28ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIAlertView alertView = new UIAlertView ();
alertView.Title="謝謝";
alertView.Message = "親若是你對咱們的商品滿意,請點亮五顆星";
//添加多個按鈕
alertView.AddButton ("前往評論");
alertView.AddButton ("暫不評論");
alertView.AddButton ("殘忍拒絕");
alertView.Show ();
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.49所示。
圖2.49 運行效果
4.具體文本輸入框的警告視圖
在警告視圖中也能夠添加文本框視圖,此時須要使用AlertViewStyle屬性。
【示例2-31】如下的代碼就是爲文本框添加了兩個警告視圖,代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __29ViewController : UIViewController
{
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIAlertView alertView = new UIAlertView ();
alertView.Title="登陸";
alertView.AlertViewStyle = UIAlertViewStyle.LoginAndPasswordInput;//設置警告視圖的風格
alertView.Show ();
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
運行效果如圖2.50所示。
圖2.50 運行效果
注意:AlertViewStyle屬性一共有四種風格,這四種風格的效果如圖2.51所示。
圖2.51 警告視圖風格效果
在以上的警告視圖示例中,爲警告視圖添加了按鈕,它們的功能都是同樣的,那就是實現警告視圖的關閉。此時,多按鈕的警告視圖就顯得畫蛇添足了,那麼如何讓警告視圖中的每個按鈕都實現它本身的功能呢?這就須要實現警告視圖的響應,此時須要使用到Dismissed事件,其語法形式若是:
警告視圖對象.Dismissed +=觸摸按鈕後的方法;
或者是:
警告視圖對象.Dismissed += (sender, e) => {
……
};
其中,sender表示事件監視的對象,e就是事件所須要的數據。
【示例2-32】如下的代碼將在主視圖中顯示一個標題爲btnShowAlert的按鈕。觸摸此按鈕後,將顯示一個具備兩個按鈕的警告視圖,其標題分別爲OK和Cancel。當用戶觸摸警告視圖中的任意一個按鈕,都會改變爲btnShowAlert按鈕的標題,代碼以下:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace Application
{
public partial class __12ViewController : UIViewController
{
UIButton btnShowAler;
…… //這裏省略了視圖控制器的構造方法和析構方法
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
btnShowAler = new UIButton ();
btnShowAler.Frame = new RectangleF (106, 269, 108, 30);
btnShowAler.SetTitle ("Show Alert", UIControlState.Normal);
this.View.AddSubview (btnShowAler);
btnShowAler.TouchUpInside+=(sender, e) =>this.ShowAlert("Alert Message", "Tap OK or Cancel") ;
}
//觸摸btnShowAler按鈕後實現的功能
private void ShowAlert(string title, string message)
{
// Create the alert
UIAlertView alertView = new UIAlertView(); //實例化警告視圖對象
alertView.Title = title; //設置標題
alertView.Message = message; //設置信息
//添加按鈕
alertView.AddButton("OK");
alertView.AddButton("Cancel");
//響應警告視圖
alertView.Dismissed += (sender, e) => {
if (e.ButtonIndex == 0)
{
btnShowAler.SetTitle("OK!",UIControlState.Normal);
} else
{
btnShowAler.SetTitle("Cancelled!",UIControlState.Normal);
}
};
alertView.Show();
}
…… //這裏省略了視圖加載和卸載先後的一些方法
#endregion
}
}
在此程序中sender表示的就是警告視圖中的按鈕,e表示點擊按鈕的一些參數如ButtonIndex。運行效果如圖2.52所示。
圖2.52 運行效果
本文選自:Xamarin iOS開發實戰大學霸內部資料,轉載請註明出處,尊重技術尊重IT人!