開啟monoDevelop,選擇Start a New Solution
選擇iphone Window-based Project 項目名為firstViewController 確定
再按確定,系統自動建下面檔案,
main.cs 內容
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace FirstViewController
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}
// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
window.MakeKeyAndVisible ();
return true;
}
// This method is required in iPhoneOS 3.0
public override void OnActivated (UIApplication application)
{
}
}
}
這裡重寫的FinishedLaunching 方法,如果FinishedLaunching 方法不在10S內返回,那麼iso就會關閉該應用程式!在這裡不給添加太多的代碼!
添加iPhone View with Controller ,在file->new->file 選擇iPhone and iPad ->iPhone View with Controller
輸入first,,按確定
展開Solution 裡的first.xib
雙擊first.xib.cs
添加下面代碼
view plain
private void butonClickFunction(object sender,EventArgs e )
{
((UIButton) sender).SetTitle("clicked!",UIControlState.Normal);
}
public override void ViewDidLoad()
{
button2=UIButton.FromType(UIButtonType.RoundedRect);
button2.Frame=new System.Drawing.RectangleF(100f,20f,60f,40f);
button2.SetTitle("click",UIControlState.Normal);
button2.TouchUpInside+=new System.EventHandler(butonClickFunction);
this.View.AddSubview(button2);
base.ViewDidLoad();
}
butonClickFunction 方法是button 點擊操作時調用的方法
ViewDidLoad() 方法是重寫ViewDidLoad 方法,這個方法是view 載入的時候觸發的,在這個方法裡可以載入一些控制項和初始化操作,在這裡我們添加一個UIbuttion ,這裡的button2,在類開始添加申明,UIButtion button2 ,
UIButton.FromType(UIButtonType.RoundedRect);
這裡的UIButtonType.RoundedRect 近回的是一個圓邊的button ,也就是標準iso button
button.Frame=new System.Drawing.RectangleF(左上方的X,左上方的Y,寬度,高度)
button2.SetTitle("click",UIControlState.Normal); 設定標題,
button2.TouchUpInside+=new System.EventHandler(butonClickFunction);設定事件
this.View.AddSubview(button2); 把button2 添加到當前view 裡,做為子view
base.ViewDidLoad(); 調用父類的viewDidLoad 在xcode 裡,在子類裡調用父類方法,經常出現在,實列化類時候都是調用base.init 他們都 是NSObject 的子類
返回到main.cs
在FinishedLaunching 添加代碼
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
first f=new first ();
window.AddSubview(f.View);
window.MakeKeyAndVisible ();
return true;
}
Build and run
摘自 ssihc0的專欄