c# iphone 開發 MonoDevelop viewController的使用

來源:互聯網
上載者:User

 

 

開啟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的專欄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.