我正在尝试在我的MonoTouch C#应用中创建一个屏幕,让用户可以搜索餐馆。用户可以搜索附近,任何地方或最近访问过的餐馆。这三个选项显示为分段控件。当用户单击分段控件中的项目时,我切换可见视图。换句话说,每个选项代表自己的视图。
我希望每个视图都有自己的表视图控制器,这样当用户点击餐馆时,会向用户提供更多详细信息。据我所知,我需要实现此处显示的方法:http://www.alexyork.net/blog/post/UINavigationController-with-MonoTouch-Building-a-simple-RSS-reader-Part-1.aspx
我的问题是,我似乎无法找到将控制器添加到视图的方法。在我的场景中,我相信我需要为每个UIView添加一个UITableViewController(每个分段控件项一个)。这可能吗?如果是这样,怎么样?如果不可能,我该如何实现目标?这看起来很简单,但我似乎离开了,并且误解了一些东西。这是我在查看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
}
感谢您的帮助!
答案 0 :(得分:1)
通常,如果你想在UIViewControllers之间转换,你可以使用UINavigationController并将它们“推”到屏幕上。在这种情况下,您需要在UINavigationController的UINavigationBar中使用UISegmentedControl。
this.PushViewController(myRecentRestaurantsViewController, true);
在分段控件中单击关联项时,可以调用上面的方法将相应的控制器推入视图。
基本上UIViewControllers是视图的“容器”。如果您想将ViewController的View添加到另一个ViewController的视图,请查看我关于自定义容器的博客文章:
http://blog.devnos.com/wont-somebody-please-think-of-the-children