c# iphone 开发 MonoDevelop viewController的使用

打开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 

添加下面代码

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











                
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值