1. 程式人生 > >monodelvelop中執行後彈出指定xib窗體及顯示分組資料

monodelvelop中執行後彈出指定xib窗體及顯示分組資料

環境:蘋果系統 

開發工具:monodevelop xcode

開發語言:c#

疑問:如何在新建的monotouch空專案中 執行後彈出指定的xib窗體

解答:

      1、首先新增新檔案,選擇ipad/iphone view controller,命名為mainwindows

      2、在mainwindows中寫入如下程式碼(使用UITableView顯示分組資料,格式化程式碼快捷鍵Ctrl+i):

View Code
  1 using System.Linq;
  2 using System;
  3 using System.Drawing;
  4
using System.Globalization; 5 using System.Collections.Generic; 6 using MonoTouch.Foundation; 7 using MonoTouch.UIKit; 8 9 namespace tables03s { 10 11 struct Element 12 { 13 public string Number{get;set;} 14 public string Name{get;set;} 15 public
string Symbol{get;set;} 16 public string Family{get;set;} 17 public string StartsWith{get{return Name.Substring(0,1);}} 18 } 19 public partial class mainWindows : UIViewController { 20 private List<Element> elements; 21 public mainWindows() : base
("mainWindows", null) { 22 23 elements = new List<Element> 24 { 25 new Element{Name="hydrogen",Number="1",Symbol="H",Family="Hydrogen"}, 26 new Element{Name="helium",Number="2",Symbol="He",Family="Noble gas"}, 27 new Element{Name="lithium",Number="3",Symbol="Li",Family="Alkali Metal"}, 28 new Element{Name="beryllium",Number="4",Symbol="Be",Family="Alkaline Earth"}, 29 new Element{Name="boron",Number="5",Symbol="B",Family="Boron"}, 30 new Element{Name="carbon",Number="6",Symbol="C",Family="Carbon"}, 31 new Element{Name="nitrogen",Number="7",Symbol="N",Family="Pnictide"}, 32 new Element{Name="oxygen",Number="8",Symbol="O",Family="Chalcogen"}, 33 new Element{Name="fluorine",Number="9",Symbol="F",Family="Halogen"}, 34 new Element{Name="neon",Number="10",Symbol="Ne",Family="Noble gas"}, 35 new Element{Name="sodium",Number="11",Symbol="Na",Family="Alkali Metal"}, 36 new Element{Name="magnesium",Number="12",Symbol="Mg",Family="Alkaline Earth"}, 37 new Element{Name="aluminum",Number="13",Symbol="Al",Family="Boron"}, 38 new Element{Name="silicon",Number="14",Symbol="Si",Family="Boron"} 39 40 }; 41 42 43 } 44 45 46 public override void DidReceiveMemoryWarning() { 47 // Releases the view if it doesn't have a superview. 48 base.DidReceiveMemoryWarning(); 49 50 // Release any cached data, images, etc that aren't in use. 51 } 52 53 public override void ViewDidLoad() { 54 base.ViewDidLoad(); 55 tableViews.Source=new MyTableViewSource(elements); 56 // tableViews.Source=new MyTableViewSource(elements); 57 // Perform any additional setup after loading the view, typically from a nib. 58 } 59 60 public override void ViewDidUnload() { 61 base.ViewDidUnload(); 62 63 // Clear any references to subviews of the main view in order to 64 // allow the Garbage Collector to collect them sooner. 65 // 66 // e.g. myOutlet.Dispose (); myOutlet = null; 67 68 ReleaseDesignerOutlets(); 69 } 70 71 public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation) { 72 // Return true for supported orientations 73 return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown); 74 } 75 76 private class MyTableViewSource:UITableViewSource 77 { 78 private List<string> sectionTitles; 79 private SortedDictionary<int,List<Element>> sectionElements 80 =new SortedDictionary<int, List<Element>>(); 81 public MyTableViewSource(List<Element> list) 82 { 83 sectionTitles=(from c in list select c.StartsWith).Distinct().ToList(); 84 sectionTitles.Sort(); 85 foreach(var element in list) 86 { 87 int sectionNum=sectionTitles.IndexOf(element.StartsWith); 88 if(sectionElements.ContainsKey(sectionNum)) 89 { 90 sectionElements[sectionNum].Add(element); 91 } 92 else 93 { 94 sectionElements.Add(sectionNum,new List<Element>{element}); 95 } 96 97 } 98 99 } 100 public override int NumberOfSections(UITableView tableView) { 101 return sectionTitles.Count; 102 } 103 public override string TitleForHeader(UITableView tableView, int section) { 104 return sectionTitles[section]; 105 } 106 public override string[] SectionIndexTitles(UITableView tableView) { 107 return sectionTitles.ToArray(); 108 } 109 public override int RowsInSection(UITableView tableview, int section) { 110 return sectionElements[section].Count; 111 } 112 public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) { 113 114 string kCellIdetifier="mycell"; 115 UITableViewCell cell=tableView.DequeueReusableCell(kCellIdetifier); 116 if(cell==null) 117 { 118 cell=new UITableViewCell(UITableViewCellStyle.Value2,kCellIdetifier); 119 } 120 Element display=sectionElements[indexPath.Section][indexPath.Row]; 121 cell.TextLabel.Text=display.Symbol; 122 cell.DetailTextLabel.Text=display.Name; 123 cell.DetailTextLabel.Font=UIFont.SystemFontOfSize(20); 124 return cell; 125 } 126 public override void RowSelected(UITableView tableView, NSIndexPath indexPath) { 127 Element display=sectionElements[indexPath.Section][indexPath.Row]; 128 ShowAlert("RowSelected","You selected "+display.Name); 129 tableView.DeselectRow(indexPath,true); 130 } 131 public void ShowAlert(string title,string message) 132 { 133 using (var alert=new UIAlertView(title,message,null,"OK",null)) 134 { 135 alert.Show(); 136 } 137 } 138 } 139 } 140 141 }

      3、在AppDelegate中寫入如下程式碼,主要呼叫了window的RootViewController屬性:

View Code
 1 public partial class AppDelegate : UIApplicationDelegate {
 2     // class-level declarations
 3     UIWindow window;
 4     mainWindows mainUiwindows;
 5     //
 6     // This method is invoked when the application has loaded and is ready to run. In this 
 7     // method you should instantiate the window, load the UI into it and then make the window
 8     // visible.
 9     //
10     // You have 17 seconds to return from this method, or iOS will terminate your application.
11     //
12     public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
13         window = new UIWindow(UIScreen.MainScreen.Bounds);
14         mainUiwindows=new mainWindows();
15         window.RootViewController=mainUiwindows; //important
16         window.MakeKeyAndVisible();
17         return true;
18     }
19 }

      4、效果圖如下: