Xamarin.ios 在ParentViewController.View.AddSubview之后无法访问视图
我正在使用Monotouch开发iPad应用程序 以下是我的设想: 我创建了选项卡式基础应用程序。 e、 家庭、管理、报告等 主选项卡是UIViewController 我希望在“主页”选项卡中包含三个部分: e、 g.类别(带有导航控件的表视图(之所以使用导航,是因为我们在类别中有子类别),在类别表旁边,是所选类别的项目(其他表视图),右侧是所选项目的详细视图 这就是我所做的 动态创建两个tableview控制器并添加到主视图控制器 HomeViewController.cs:Xamarin.ios 在ParentViewController.View.AddSubview之后无法访问视图,xamarin.ios,Xamarin.ios,我正在使用Monotouch开发iPad应用程序 以下是我的设想: 我创建了选项卡式基础应用程序。 e、 家庭、管理、报告等 主选项卡是UIViewController 我希望在“主页”选项卡中包含三个部分: e、 g.类别(带有导航控件的表视图(之所以使用导航,是因为我们在类别中有子类别),在类别表旁边,是所选类别的项目(其他表视图),右侧是所选项目的详细视图 这就是我所做的 动态创建两个tableview控制器并添加到主视图控制器 HomeViewController.cs: pub
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
RootViewController rvc = new RootViewController("",UITableViewStyle.Grouped);
// navigation controller will manage the views displayed and provide navigation buttons
navigationController = new UINavigationController();
navigationController.PushViewController(rvc, false);
navigationController.TopViewController.Title ="Category";
navigationController.View.Frame = new RectangleF (0, 50, (50), (600));
// Main window to which we add the navigation controller to
this.View.AddSubview(navigationController.View);
itemtable.Delegate = new TableViewDelegate (list);
itemtable.DataSource = new TableViewDataSource (list);
// Perform any additional setup after loading the view, typically from a nib.
}
=====================================================
RootViewController:
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
namespace DVPNTN_MobileApp
{
[MonoTouch.Foundation.Register("RootViewController")]
public partial class RootViewController : UITableViewController
{
public List<string> RootData = new List<string> { "Group1", "Group2" };
MonoTouch.UIKit.UINavigationController navigationControllerItem;
string SelectedGroup;
// Allow us to set the style of the TableView
public RootViewController(string selectedGroup, UITableViewStyle style) : base(style)
{
this.SelectedGroup = selectedGroup;
}
class DataSource : UITableViewDataSource
{
static NSString kCellIdentifier = new NSString ("MyIdentifier");
RootViewController tvc;
public DataSource (RootViewController tvc)
{
this.tvc = tvc;
}
public override int RowsInSection (UITableView tableView, int section)
{
return tvc.RootData.Count;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (kCellIdentifier);
if (cell == null)
{
cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
}
cell.TextLabel.Text = tvc.RootData.ElementAt(indexPath.Row);
cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
return cell;
}
}
class TableDelegate : UITableViewDelegate
{
RootViewController tvc;
SubGroupViewController sgvc;
public TableDelegate (RootViewController tvc)
{
this.tvc = tvc;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
string selectedGroup = tvc.RootData.ElementAt(indexPath.Row);
sgvc = new SubGroupViewController(selectedGroup, UITableViewStyle.Grouped);
tvc.NavigationController.PushViewController(sgvc,true);
//tvc.View.RemoveFromSuperview();
//tvc.DidReceiveMemoryWarning();
GC.Collect();
}
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
TableView.Delegate = new TableDelegate (this);
TableView.DataSource = new DataSource (this);
RootVIewItemController rvc1 = new RootVIewItemController(SelectedGroup,UITableViewStyle.Grouped);
// navigation controller will manage the views displayed and provide navigation buttons
navigationControllerItem = new UINavigationController();
navigationControllerItem.PushViewController(rvc1, false);
navigationControllerItem.TopViewController.Title = SelectedGroup + " " + "Item List";
navigationControllerItem.View.Frame = new RectangleF (0, 300, (50),(700));
//this.View.AddSubview(navigationControllerItem.View);
//rvc1.View.EnableInputClicksWhenVisible = true;
//this.ParentViewController.AddChildViewController(navigationControllerItem);
**> Problem is here --- subview is successfully added to parent view but it's not accessible, mean items are there but we can't touch cell or row???????**
ParentViewController.View.AddSubview(navigationControllerItem.View);
GC.Collect();
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
// Return true for supported orientations
return true;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用单调的基础;
使用MonoTouch.UIKit;
使用系统图;
命名空间DVPNTN_MobileApp
{
[基础]登记(“RootViewController”)
公共部分类RootViewController:UITableViewController
{
public List RootData=新列表{“Group1”、“Group2”};
MonoTouch.UIKit.UINavigationController导航控制器项;
字符串SelectedGroup;
//允许我们设置TableView的样式
公共根视图控制器(字符串selectedGroup,UITableViewStyle样式):基本(样式)
{
this.SelectedGroup=SelectedGroup;
}
类数据源:UITableViewDataSource
{
静态NSString kCelliIdentifier=新NSString(“MyIdentifier”);
rootview控制器tvc;
公共数据源(RootViewController tvc)
{
this.tvc=tvc;
}
公共覆盖int RowsInSection(UITableView表格视图,int section)
{
返回tvc.RootData.Count;
}
公共覆盖UITableViewCell GetCell(UITableView tableView,NSIndexPath indexPath)
{
var cell=tableView.DequeueReusableCell(kCellIdentifier);
if(单元格==null)
{
单元格=新的UITableViewCell(UITableViewCellStyle.Default,kCellIdentifier);
}
cell.textlab.Text=tvc.RootData.ElementAt(indexPath.Row);
cell.accessority=UITableViewCellAccessority.DetailDisclosure按钮;
返回单元;
}
}
类TableDelegate:UITableViewDelegate
{
rootview控制器tvc;
子控制器sgvc;
公共TableDelegate(RootViewController tvc)
{
this.tvc=tvc;
}
public override void RowSelected(UITableView tableView,NSIndexPath indexPath)
{
字符串selectedGroup=tvc.RootData.ElementAt(indexPath.Row);
sgvc=新的子视图控制器(selectedGroup,UITableViewStyle.Grouped);
tvc.NavigationController.PushViewController(sgvc,true);
//tvc.View.RemoveFromSuperview();
//tvc.DidReceiveMemoryWarning();
GC.Collect();
}
}
public override void direceivememorywarning()
{
//如果视图没有superview,则释放该视图。
base.DidReceiveMemoryWarning();
//释放所有未使用的缓存数据、图像等。
}
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
TableView.Delegate=新的TableDelegate(此);
TableView.DataSource=新数据源(此);
RootVIewItemController rvc1=新的RootVIewItemController(SelectedGroup,UITableViewStyle.Grouped);
//导航控制器将管理显示的视图并提供导航按钮
navigationControllerItem=新的UINavigationController();
navigationControllerItem.PushViewController(rvc1,错误);
navigationControllerItem.TopViewController.Title=SelectedGroup+“”+“项目列表”;
navigationControllerItem.View.Frame=新矩形F(0,300,(50),(700));
//this.View.AddSubview(navigationControllerItem.View);
//rvc1.View.EnableInputClicksWhenVisible=true;
//this.ParentViewController.AddChildViewController(navigationControllerItem);
**>问题就在这里---子视图已成功添加到父视图,但不可访问,这意味着项目在那里,但我们无法触摸单元格或行**
ParentViewController.View.AddSubview(navigationControllerItem.View);
GC.Collect();
}
公共覆盖布尔值应自动旋转指针面方向(UIInterfaceOrientation到InterfaceOrientation)
{
//对于支持的方向返回true
返回true;
}
}
}
*********ItemViewController***
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace DVPNTN_MobileApp
{
[MonoTouch.Foundation.Register("RootVIewItemController")]
public partial class RootVIewItemController : UITableViewController
{
public List<string> RootData = new List<string> { "Item 1", "Item 2", "Item 3", "Item 4" };
string SelectedGroup;
public RootVIewItemController (string selectedGroup, UITableViewStyle style) : base (style)
{
this.SelectedGroup = selectedGroup;
}
class DataSource : UITableViewDataSource
{
static NSString kCellIdentifier = new NSString ("MyIdentifier");
RootVIewItemController tvc;
public DataSource (RootVIewItemController tvc)
{
this.tvc = tvc;
}
public override int RowsInSection (UITableView tableView, int section)
{
return tvc.RootData.Count;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (kCellIdentifier);
if (cell == null)
{
cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
}
cell.TextLabel.Text = tvc.RootData.ElementAt(indexPath.Row);
//cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
return cell;
}
}
class TableDelegate : UITableViewDelegate
{
RootVIewItemController tvc;
SubGroupViewController sgvc;
public TableDelegate (RootVIewItemController tvc)
{
this.tvc = tvc;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
string selectedGroup = tvc.RootData.ElementAt(indexPath.Row);
Console.WriteLine(
"TableViewDelegate.RowSelected: Label={0}",selectedGroup);
/*
if(sgvc == null)
sgvc = new SubGroupViewController(selectedGroup, UITableViewStyle.Grouped);
tvc.NavigationController.PushViewController(sgvc,true);*/
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
TableView.Delegate = new TableDelegate (this);
TableView.DataSource = new DataSource (this);
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用单调的基础;
使用MonoTouch.UIKit;
命名空间DVPNTN_MobileApp
{
[基础]登记(“RootVIewItemController”)
公共部分类RootVIewItemController:UITableViewController
{
公共列表根数据=新列表{“项目1”、“项目2”、“项目3”、“项目4”};
字符串SelectedGroup;
public RootVIewItemController(字符串selectedGroup,UITableViewStyle样式):基本(样式)
{
this.SelectedGroup=SelectedGroup;
}
类数据源:UITableViewDataSource
{
静态NSString kCelliIdentifier=新NSString(“MyIdentifier”);
RootVIewItemController tvc;
公共数据源(RootVIewItemController tvc)
{
this.tvc=tvc;
}
公共覆盖int RowsInSection(UITableView表格视图,int section)
{
返回tvc.RootData.Count;
}
公共覆盖UITableViewCell GetCell(UITableView tableView,NSIndexPath indexPath)
{
var cell=tableView.DequeueReusableCell(kCellIdentifier);
if(单元格==null)
{
单元格=新的UITableViewCell(UITableViewCellStyle.Default,kCellIdentifier);
}
cell.textlab.Tex