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
添加下面代码
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
补充:移动开发 , IOS ,