2012-01-07 39 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

相关问题