how can I access UIComponent of the base class from RowSelected function of the TableView ?

I am trying to print the row selected from the TableView in TextView, but I am unable to access any UIObject of the Base Class. Any workaround for this problem will be appreciated.

            namespace TeraMessage
            {
                [Register ("ConversationViewController")]
                partial class ConversationViewController
                {
                    [Outlet]
                    MonoTouch.UIKit.UITextView textMessage { get; set; }
                    }
            }


            public partial class ConversationViewController : UIViewController
            {
                    private UITableView cannedMessageTableView;
                    cannedMessageTableView = new UITableView(View.Bounds); // defaults to Plain style
                                    string[] tableItems = new string[]{ 
                                            "string 1",
                                            "string 2",
                                            "string 3",
                                            "string 4",
                                            "string 5"              
                                    };

                                    cannedMessageTableView.Source = new CMTableSource(tableItems);

            UIButton cannedMessageBtn = new UIButton (UIButtonType.Custom);
            cannedMessageBtn.TouchUpInside += (sender, e) => {
                            System.Diagnostics.Debug.WriteLine ("button tapped");
                            Add (cannedMessageTableView);
                            }
            }

            public class CMTableSource : UITableViewSource {

                            string[] tableItems;

                            string cellIdentifier = "TableCell";

                            public CMTableSource (string[] items)
                            {
                                    tableItems = items;
                            }
                            public override int RowsInSection (UITableView tableview, int section)
                            {
                                    return tableItems.Length;
                            }
                            public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
                            {
                                    UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
                                    // if there are no cells to reuse, create a new one
                                    if (cell == null)
                                    {
                                            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
                                            cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
                                            cell.TextLabel.Lines = 0;
                                    }

                                    cell.TextLabel.Text = tableItems[indexPath.Row];
                                    return cell;
                            }

                            public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
                            {
                                    Console.WriteLine("Row Selected " + tableItems[indexPath.Row]);

                                    tableView.DeselectRow (indexPath, true); // iOS convention is to remove the highlight

                                    tableView.RemoveFromSuperview ();

                            }       
                    }

Thanks in advance.

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    I don't understand what you are trying to do.

  • SatishBirajdarSatishBirajdar USMember ✭✭
    1. ConversationViewController includes Textmessage (uitextview) and CannedMessageButton (UIButton)
    2. whenever I tap on CannedMessageButton, cannedMessageTableView is added.

    what I want is,
    3. whenever I select any row of the cannedMessageTableView, the data should be added in the Textmessage of ConversationViewController.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    You want to use a navigation controller. Start here: http://developer.xamarin.com/guides/ios/getting_started/hello,_iOS_multiscreen/

  • SatishBirajdarSatishBirajdar USMember ✭✭

    I tried few different ways one of them as below:

     public partial class ConversationViewController : UIViewController
    {
                UIButton cannedMessageBtn = new UIButton (UIButtonType.Custom);
                cannedMessageBtn.TouchUpInside += (sender, e) => {
                                NavigationController.PushViewController (new MyTableViewController(),true); 
                }
    }
    public class MyTableViewController : UITableViewController
        {
                        public CanMessageController () : base (UITableViewStyle.Grouped)
                        {
                        }
                                    public override void ViewDidLoad ()
                        {
                                base.ViewDidLoad ();
                                                    TableView.Source = new MyTableViewSource (this.ParentViewController);
                        }
           }
    
    
        class MyTableViewSource : UITableViewSource
        {
            UIViewController parentController;
            public MyTableViewSource (UIViewController parentController) 
            {
                this.parentController = parentController;
            }
    
            public override int RowsInSection (UITableView tableview, int section)
            {
                //...
            }
    
            public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
            {
                //...
            }
    
            public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
            {
                var index = indexPath.Row;
                parentController.NavigationController.PushViewController (new ConversationViewController(index));
            }
        }
    

    This is now giving an error in RowSelected Function as parentController.NavigationController is Null.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    You have to create a navigation controller. It doesn't come for free. The NavigationController property is set by the navigation controller when a view controller is pushed into it. Somehow you have to create one to start with, though, and your initial view controller (MyTableViewController) needs to be the initial view controller of the navigation controller.

    I think you should go back and read that tutorial I linked to more closely.

  • SatishBirajdarSatishBirajdar USMember ✭✭

    public class CanMessageController : UITableViewController
    {
    public override void ViewDidLoad ()
    {
    base.ViewDidLoad ();
    //NavigationController.NavigationBar.Translucent = false;

                        Persistence pers = Persistence.GetInstance();
                        string[] tableItems = new string[]{ 
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_0, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_1, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_2, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_3, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_4, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_5, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_6, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_7, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_8, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_9, string.Empty)
    
                        };
    
                        // Register the TableView's data source
                        TableView.Source = new CanMessageSource (tableItems,this.NavigationController);
                }
    

    }

    public partial class ConversationViewController : UIViewController
    {
    UIButton cannedMessageBtn = new UIButton (UIButtonType.Custom);
    cannedMessageBtn.TouchUpInside += (sender, e) => {
    NavigationController.PushViewController (new MyTableViewController(),true);
    }
    }

    public class CanMessageSource : UITableViewSource
    {
    UINavigationController parentController;
    string[] tableItems;
    string cellIdentifier = "TableCell";

                public CanMessageSource (string[] items,UINavigationController parentController)
                {
                        //this.parentController = parentController;
                        tableItems = items;
                }
    
                public override int NumberOfSections (UITableView tableView)
                {
                        // TODO: return the actual number of sections
                        return 1;
                }
    
                public override int RowsInSection (UITableView tableview, int section)
                {
                        // TODO: return the actual number of items in the section
                        //return 1;
                        return tableItems.Length;
                }
    
                public override string TitleForHeader (UITableView tableView, int section)
                {
                        return "Canned Message";
                }
    
                public override string TitleForFooter (UITableView tableView, int section)
                {
                        return "Footer";
                }
    
                public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
                {
                        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
                        if (cell == null) {
                                cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
                                cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
                                cell.TextLabel.Lines = 0;
                        }
    
                        // TODO: populate the cell with the appropriate data based on the indexPath
                        cell.TextLabel.Text = tableItems[indexPath.Row];
    
                        return cell;
                }
    
                public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
                {
    
    
    
                        Console.WriteLine("Row Selected " + tableItems[indexPath.Row]);
    

    // ConversationViewController CV = new ConversationViewController ();
    // CV.cannedText = tableItems [indexPath.Row]
    // CV.updateTextMessage (tableItems [indexPath.Row]);
    //CV.textMessage.Text += tableItems [indexPath.Row];

                        //                              tableView.Superview
                        //                              for (UITextView *textmessage in tableView
    
                        //cannedMessageAdded = true;
    
                        //updateTextMessage(tableItems[indexPath.Row]);
    
                        tableView.DeselectRow (indexPath, true); // iOS convention is to remove the highlight
    
                        //this.parentController.PopToViewController(new ConversationViewController (),true);
    
    
                        //this.parentController.NavigationController.PopToViewController (new ConversationViewController (),true);
    

    // this.parentController.NavigationController.PushViewController (new ConversationViewController(),true);

    // this.parentController.NavigationController.PopViewControllerAnimated (true);

                        CanMessageController cm = new CanMessageController ();
                        cm.selectedRow (tableItems[indexPath.Row]);
    
                }
    
                public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
                {
                        // NOTE: Don't call the base implementation on a Model class
                        // see http://docs.xamarin.com/guides/ios/application_fundamentals/delegates,_protocols,_and_events
                        //              throw new NotImplementedException ();
                        string item = tableItems[indexPath.Row];
                        UIFont someFont = UIFont.SystemFontOfSize(13f);
                        SizeF size = new SizeF (tableView.Bounds.Width - 40, float.MaxValue);
                        float height = tableView.StringSize(item, someFont, size, UILineBreakMode.WordWrap).Height;
    
    
                        return (height + 30);
                }               
    

    }

    public override void ViewDidLoad ()
    {
    base.ViewDidLoad ();
    //NavigationController.NavigationBar.Translucent = false;

                        Persistence pers = Persistence.GetInstance();
                        string[] tableItems = new string[]{ 
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_0, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_1, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_2, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_3, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_4, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_5, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_6, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_7, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_8, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_9, string.Empty)
    
                        };
    
                        // Register the TableView's data source
                        TableView.Source = new CanMessageSource (tableItems,this.NavigationController);
                }
    
                public void selectedRow(string str)
                {
    
    
                        Console.WriteLine ("Satish Row Selected " + str);
                        NavigationController.PopViewControllerAnimated (true);
    
                        //ParentViewController = this;
                        //ConversationViewController cvm = new ConversationViewController ();
                        //NavigationController.DismissViewController (true, null);
                        //NavigationController.PushViewController (new ConversationViewController(), true);
    
    
                        //PresentViewController (new ConversationViewController (), true,()=>{});
    
                        //NavigationController.PopToRootViewController(true);
                }
    
                public override void ViewWillAppear(bool animated)
                {
                        base.ViewWillAppear(animated);
                        //NavigationController.PopViewControllerAnimated (true);
    
                }
    
  • SatishBirajdarSatishBirajdar USMember ✭✭
    public class CanMessageController : UITableViewController { public override void ViewDidLoad () { base.ViewDidLoad (); //NavigationController.NavigationBar.Translucent = false;
    
                        Persistence pers = Persistence.GetInstance();
                        string[] tableItems = new string[]{ 
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_0, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_1, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_2, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_3, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_4, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_5, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_6, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_7, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_8, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_9, string.Empty)
    
                        };
    
                        // Register the TableView's data source
                        TableView.Source = new CanMessageSource (tableItems,this.NavigationController);
                }
    }
    
    public partial class ConversationViewController : UIViewController { UIButton cannedMessageBtn = new UIButton (UIButtonType.Custom); cannedMessageBtn.TouchUpInside += (sender, e) => { NavigationController.PushViewController (new MyTableViewController(),true); } }
    
    public class CanMessageSource : UITableViewSource { UINavigationController parentController; string[] tableItems; string cellIdentifier = "TableCell";
    
                public CanMessageSource (string[] items,UINavigationController parentController)
                {
                        //this.parentController = parentController;
                        tableItems = items;
                }
    
                public override int NumberOfSections (UITableView tableView)
                {
                        // TODO: return the actual number of sections
                        return 1;
                }
    
                public override int RowsInSection (UITableView tableview, int section)
                {
                        // TODO: return the actual number of items in the section
                        //return 1;
                        return tableItems.Length;
                }
    
                public override string TitleForHeader (UITableView tableView, int section)
                {
                        return "Canned Message";
                }
    
                public override string TitleForFooter (UITableView tableView, int section)
                {
                        return "Footer";
                }
    
                public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
                {
                        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
                        if (cell == null) {
                                cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
                                cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
                                cell.TextLabel.Lines = 0;
                        }
    
                        // TODO: populate the cell with the appropriate data based on the indexPath
                        cell.TextLabel.Text = tableItems[indexPath.Row];
    
                        return cell;
                }
    
                public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
                {
    
    
    
                        Console.WriteLine("Row Selected " + tableItems[indexPath.Row]);
    // ConversationViewController CV = new ConversationViewController (); // CV.cannedText = tableItems [indexPath.Row] // CV.updateTextMessage (tableItems [indexPath.Row]); //CV.textMessage.Text += tableItems [indexPath.Row];
    
                        //                              tableView.Superview
                        //                              for (UITextView *textmessage in tableView
    
                        //cannedMessageAdded = true;
    
                        //updateTextMessage(tableItems[indexPath.Row]);
    
                        tableView.DeselectRow (indexPath, true); // iOS convention is to remove the highlight
    
                        //this.parentController.PopToViewController(new ConversationViewController (),true);
    
    
                        //this.parentController.NavigationController.PopToViewController (new ConversationViewController (),true);
    // this.parentController.NavigationController.PushViewController (new ConversationViewController(),true);
    
    // this.parentController.NavigationController.PopViewControllerAnimated (true);
    
                        CanMessageController cm = new CanMessageController ();
                        cm.selectedRow (tableItems[indexPath.Row]);
    
                }
    
                public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
                {
                        // NOTE: Don't call the base implementation on a Model class
                        // see http://docs.xamarin.com/guides/ios/application_fundamentals/delegates,_protocols,_and_events
                        //              throw new NotImplementedException ();
                        string item = tableItems[indexPath.Row];
                        UIFont someFont = UIFont.SystemFontOfSize(13f);
                        SizeF size = new SizeF (tableView.Bounds.Width - 40, float.MaxValue);
                        float height = tableView.StringSize(item, someFont, size, UILineBreakMode.WordWrap).Height;
    
    
                        return (height + 30);
                }               
    }
    
    public override void ViewDidLoad () { base.ViewDidLoad (); //NavigationController.NavigationBar.Translucent = false;
    
                        Persistence pers = Persistence.GetInstance();
                        string[] tableItems = new string[]{ 
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_0, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_1, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_2, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_3, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_4, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_5, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_6, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_7, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_8, string.Empty),
                                pers.ReadEntry(Persistence.KEY_CANNEDMESSAGE_9, string.Empty)
    
                        };
    
                        // Register the TableView's data source
                        TableView.Source = new CanMessageSource (tableItems,this.NavigationController);
                }
    
                public void selectedRow(string str)
                {
    
    
                        Console.WriteLine ("Satish Row Selected " + str);
                        NavigationController.PopViewControllerAnimated (true);
    
                        //ParentViewController = this;
                        //ConversationViewController cvm = new ConversationViewController ();
                        //NavigationController.DismissViewController (true, null);
                        //NavigationController.PushViewController (new ConversationViewController(), true);
    
    
                        //PresentViewController (new ConversationViewController (), true,()=>{});
    
                        //NavigationController.PopToRootViewController(true);
                }
    
                public override void ViewWillAppear(bool animated)
                {
                        base.ViewWillAppear(animated);
                        //NavigationController.PopViewControllerAnimated (true);
    
                }
    
  • adamkempadamkemp USInsider, Developer Group Leader mod

    I searched for "new UINavigationController" and found zero instances. You have to create a UINavigationController in order to use the NavigationController property! For example, it might look something like this:

    public override void FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        Window = new UIWindow(UIScreen.MainScreen.Bounds);
        var firstViewController = new CanMessageController();
        var navigationController = new UINavigationController(firstViewController);
        Window.RootViewController = navigationController;
        Window.MakeKeyAndVisible();
    }
    

    You could also create one in your main storyboard and make that (the navigation controller) your initial view controller. Either way you are responsible for creating that navigation controller somehow. It does not come for free.

Sign In or Register to comment.