2012-01-07 36 views
0

我想在我的MonoTouch C#應用程序中創建一個屏幕,讓用戶搜索餐館。用戶可以搜索附近,任何地點或最近訪問過的餐館。這三個選項是作爲分段控制呈現的。當用戶單擊分段控件中的項目時,我切換可見視圖。換句話說,每個選項代表自己的觀點。MonoTouch使用視圖和控制器

我希望每個視圖都有自己的表格視圖控制器,這樣當用戶點擊餐廳時,會向用戶提供更多細節。從我所知道的,我需要實現這裏顯示的方法:http://www.alexyork.net/blog/post/UINavigationController-with-MonoTouch-Building-a-simple-RSS-reader-Part-1.aspx

我的問題是,我似乎無法找出一種方法來添加到視圖的控制器。在我的場景中,我相信我需要將UITableViewController添加到每個UIView(每個分段控件項目都有一個)。這可能嗎?如果是這樣,怎麼樣?如果不可能,我該如何去完成我的目標?這看起來很簡單,但我似乎有點失落,誤解了一些東西。這裏是我打電話ViewDidLoad事件時的方法:

private void LoadViews() 
{ 
    int subViewHeight = 320; 

    #region Nearby View 

    RectangleF nearbyRectangle = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, subViewHeight); 
    this.nearbyView = new UIView(nearbyRectangle); 
    this.nearbyView.BackgroundColor = UIColor.Blue; 

    this.nearbyTableViewController = new NearbyTableViewController(IntPtr.Zero); 
    this.NavigationController.PushViewController(nearbyTableViewController, false); 
    this.View.Add(nearbyView); 

    #endregion Nearby View 

    #region Elsewhere View 

    RectangleF elsewhereRectangle = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, subViewHeight); 
    this.elsewhereView = new UIView(elsewhereRectangle); 
    this.elsewhereView.Hidden = true; 
    this.elsewhereView.BackgroundColor = UIColor.Green; 

    // Add the search text field 
    UITextField searchElsewhereTextField = new UITextField(); 
    searchElsewhereTextField.BorderStyle = UITextBorderStyle.RoundedRect; 
    searchElsewhereTextField.Frame = new RectangleF(20, 13, 200, 31); 
    searchElsewhereTextField.Placeholder = "query"; 
    this.elsewhereView.AddSubview(searchElsewhereTextField); 

    // Add the search button 
    UIButton searchButton = UIButton.FromType(UIButtonType.RoundedRect); 
    searchButton.Frame = new RectangleF((UIScreen.MainScreen.Bounds.Width - 90), 13, 70, 31); 
    searchButton.SetTitle("Search", UIControlState.Normal); 
    this.elsewhereView.AddSubview(searchButton); 

    // Add the results table 
    this.elsewhereTableView = new UITableView(new RectangleF(0, 52, 
UIScreen.MainScreen.Bounds.Width, subViewHeight-70), UITableViewStyle.Plain); 
    this.elsewhereTableView.Source = new NearbyListDataSource(this);   
    this.elsewhereView.AddSubview(elsewhereTableView); 

    this.View.Add(elsewhereView); 

    #endregion Elsewhere View 

    #region Recent View 

    RectangleF recentRectangle = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, subViewHeight); 
    this.recentView = new UIView(recentRectangle); 
    this.recentView.Hidden = true; 

    this.recentTableView = new UITableView(new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, subViewHeight), UITableViewStyle.Plain); 
    this.recentTableView.Source = new NearbyListDataSource(this);   
    this.recentView.AddSubview(recentTableView); 

    this.View.Add(recentView); 

    #endregion Recent View 
} 

謝謝你的幫助!

回答

1

通常如果你想在UIViewControllers之間轉換,你可以使用UINavigationController並將它們「推」到屏幕上。在這種情況下,您需要UINavigationController的UINavigationBar中的UISegmentedControl。

this.PushViewController(myRecentRestaurantsViewController, true); 

當在分段控件中單擊關聯的項目時,您調用上述方法將適當的控制器推入視圖。

基本上UIViewControllers是視圖的「容器」。如果你想要一個視圖控制器的視圖添加到另一個視圖控制器的觀點,請看看我的博客文章對自定義容器:

http://blog.devnos.com/wont-somebody-please-think-of-the-children

+0

您好,我只是說我的代碼的問題。感謝您的答覆。我仍然有問題。你能否考慮檢查我的代碼並告訴我我做錯了什麼?我完全被這個感到沮喪。謝謝! – 2012-01-08 16:10:24

+0

在更復雜的情況下,您無法將所需行爲減少爲幾行代碼,請創建一個小型自包含示例應用程序以供審閱。 – Anuj 2012-01-08 20:06:13

相關問題