Search code examples
xamarinxamarin.iosnavigationtableviewsegue

Selected row from TableView as Title on new TableView


I have data in my TableView. And I use swipe actions. When I swipe from left to right, I have the option to press "Bearbeiten". By clicking "Bearbeiten" a new TableView opens.

The title of the navigation controller of this new TableView, should have the same name as the swiped cell.

I tried it this way:

1.Create a list 2. Add data to this list 3. Prepare for Segue 4. Push the first entry of the list to the new ViewController 5. Add Navigation.title with data

But it doesn't work. What i am doing wrong?

MyCode:

First TableView:

....

List<string> wordnewvc = new List<string>();

....

    public UIContextualAction ContextualDefinitionAction(int row)
    {
        var action = UIContextualAction.FromContextualActionStyle(UIContextualActionStyle.Normal, "Bearbeiten", (ReadLaterAction, view, success) => {

                                                                wordnewvc.Add(words[row]);

                                                                secondViewController controller = this.Storyboard.InstantiateViewController("secondViewController") as secondViewController;
                                                                this.NavigationController.PushViewController(controller, true);

                                                            });
        action.BackgroundColor = UIColor.Orange;
        return action;
    }


    public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
    {
        base.PrepareForSegue(segue, sender);

        var secondViewController = segue.DestinationViewController as secondViewController;

        secondViewController.firma = wordsnewvc[0];
    }

Second ViewController:

        public string firma
    {
        get;
        set;
    }

    public secondViewController (IntPtr handle) : base (handle)
    {
    }


    public override void ViewDidLoad()
    {
        base.ViewDidLoad();


        NavigationItem.Title = firma;
    }

Solution

  • You mixd the Navigation Push and Segue

    Solution1:Navigation Push

    Method PrepareForSegue is no need.

    var action = UIContextualAction.FromContextualActionStyle(UIContextualActionStyle.Normal, "Bearbeiten", (ReadLaterAction, view, success) => {
        secondViewController controller = this.Storyboard.InstantiateViewController("secondViewController") as secondViewController;
        controller.firma = wordsnewvc[0];
        this.NavigationController.PushViewController(controller, true);
    });
    

    Solution2:Segue

    var action = UIContextualAction.FromContextualActionStyle(UIContextualActionStyle.Normal, "Bearbeiten", (ReadLaterAction, view, success) => {
        this.PerformSegue(identifierInStoryboard, null);
    });
    
    public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
    {
         if(segue.Identifier == identifierInStoryboard)
         {
             var secondViewController = segue.DestinationViewController as secondViewController;
             secondViewController.firma = wordsnewvc[0];
         }  
    }