Search code examples
ipadxcode4xamarin.iosmaster-detail

Multiple tabs in rootview


I am very new to XCode and Monotouch development. I am trying to add multiple tabs in root view (Master-Detail application). I am using Mono for development and xCode 4 for UI design. For example, one tab shows list of employees and another tab shows list of departments.

I removed the default table view in the rootviewController.xib file and replaced with tabbedview but I cant get new table view for each tab working.

I would appreciate if anyone can show me the correct way of doing this.

UPDATED: I use the code below to add 2 tabs and 2 table view. One is to hold "Securities" and the other one to hold the "Indicators"

using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Collections.Generic;

namespace ChartQ
{
public partial class RootViewController : UITabBarController 
{
    public DetailViewController DetailVC { get; set; }

    private List<SecurityInfo> listSecInfo =  new List<SecurityInfo>();
    private List<Indicator> listIndicator =  new List<Indicator>();
    public RootViewController () : base ("RootViewController", null)
    {
        this.Title = NSBundle.MainBundle.LocalizedString ("Securities", "Securities");
        //this.ClearsSelectionOnViewWillAppear = false;
        this.ContentSizeForViewInPopover = new SizeF (100f, 200f);
        //this.ContentSizeForViewInPopover = new SizeF (320f, 600f);

        // Custom initialization
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        //Read data from Database..
        UITableViewController secController =  new UITableViewController();
        UITableViewController IndiController =  new UITableViewController();

        this.AddChildViewController(secController);
        this.AddChildViewController(IndiController);
        SQLLiteDatabase db =  new SQLLiteDatabase();
        listSecInfo =  db.ReadSecurities();
        listIndicator  = db.ReadIndicator();
        //TableView.Source = new SecuritiesDataSource (this);
        secController.TableView.Source = new SecuritiesDataSource (this);
        secController.TableView.SelectRow (NSIndexPath.FromRowSection (0, 0), false, UITableViewScrollPosition.Middle);

        IndiController.TableView.Source = new IndicatorDataSource (this);
        IndiController.TableView.SelectRow (NSIndexPath.FromRowSection (0, 0), false, UITableViewScrollPosition.Middle);


    }

    public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
    {
        // Return true for supported orientations
        return true;
    }

    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 ViewDidUnload ()
    {
        base.ViewDidUnload ();


        // Clear any references to subviews of the main view in order to
        // allow the Garbage Collector to collect them sooner.
        //
        // e.g. myOutlet.Dispose (); myOutlet = null;

        ReleaseDesignerOutlets ();
    }
    class SecuritiesDataSource : UITableViewSource
    {
        static NSString cellIdentifier = new NSString ("CellId");
        RootViewController controller;

        public SecuritiesDataSource (RootViewController controller)
        {
            this.controller = controller;
        }

        // Customize the number of sections in the table view.
        public override int NumberOfSections (UITableView tableView)
        {
            return 1;
        }

        public override int RowsInSection (UITableView tableview, int section)
        {
            return controller.listSecInfo.Count;
        }

        public override string TitleForHeader (UITableView tableView, int section)
        {
            return "Securities";
        }

        // Customize the appearance of table view cells.
        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            string cellIdentifier = "Cell";
            var cell = tableView.DequeueReusableCell (cellIdentifier);
            if (cell == null) {
                cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);
                //Add in a detail disclosure icon to each cell
                cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
            }

            // Configure the cell.
            var sInfo = controller.listSecInfo [indexPath.Row];
            cell.TextLabel.Text = String.Format ("{0}",sInfo.SecCode);
            cell.DetailTextLabel.Text = sInfo.SecName;

            return cell;
        }


        private void InfoAlert (string msg)
        {
            using (UIAlertView av = new UIAlertView("Info", msg, null, "OK", null)) {
                av.Show ();
            }
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            var sInfo = controller.listSecInfo [indexPath.Row];

            controller.DetailVC.DrawChart(sInfo.SecID, sInfo.SecCode);
        }



    }
    class IndicatorDataSource : UITableViewSource
    {
        static NSString cellIdentifier = new NSString ("CellId");
        RootViewController controller;

        public IndicatorDataSource (RootViewController controller)
        {
            this.controller = controller;
        }

        // Customize the number of sections in the table view.
        public override int NumberOfSections (UITableView tableView)
        {
            return 1;
        }

        public override int RowsInSection (UITableView tableview, int section)
        {
            return controller.listIndicator.Count;
        }

        public override string TitleForHeader (UITableView tableView, int section)
        {
            return "Indicators";
        }

        // Customize the appearance of table view cells.
        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            string cellIdentifier = "Cell";
            var cell = tableView.DequeueReusableCell (cellIdentifier);
            if (cell == null) {
                cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);
                //Add in a detail disclosure icon to each cell
                cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
            }

            // Configure the cell.
            var sInfo = controller.listIndicator [indexPath.Row];
            cell.TextLabel.Text = String.Format ("{0}",sInfo.DescriptiveName);
            cell.DetailTextLabel.Text = sInfo.ShortName;

            return cell;
        }



        private void InfoAlert (string msg)
        {
            using (UIAlertView av = new UIAlertView("Info", msg, null, "OK", null)) {
                av.Show ();
            }
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            var sInfo = controller.listIndicator [indexPath.Row];

            //controller.DetailVC.DrawChart(sInfo.SecID, sInfo.SecCode);
        }


    }

}

However, it crashes when I click the second tab. I am getting the error as below

"Got a SIGSEGV while executing native code"

The first list populated ok.


Solution

  • I think the UITableViewController objects are getting collected and causing your abort. Make secController and secController member variables.

    public partial class RootViewController : UITabBarController
    {
        UITableViewController secController;
        UITableViewController IndiController;
    
        public DetailViewController DetailVC { get; set; }
    
    ...
    
    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        //Read data from Database..
        secController = new UITableViewController ();
        IndiController = new UITableViewController ();