Kimson Magat
Kimson Magat

Reputation: 24

C#(Xamarin iOS) How to navigate from UITableView to other ViewController?

What line of code should I put inside of RowSelected?

I already used this but no luck at all :/ .

public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
    var window = UIApplication.SharedApplication.KeyWindow;
    var vc = window.RootViewController;
    while (vc.PresentedViewController != null)
    {
        vc = vc.PresentedViewController;
    }
    mainnearmisses myTarget = (UIViewController)vc.Storyboard.InstantiateViewController("mnearm") as mainnearmisses;
    myTarget.email = locations[indexPath.Row].shopname + "";
    vc.PresentViewController(myTarget, true, null);
}

Upvotes: 0

Views: 301

Answers (1)

ColeX
ColeX

Reputation: 14475

Some suggestions for you.

1. Set the ViewController itself as parameter in the initializing constructor of the dataSource.

in ViewController

this.TableView.Source = new TableSource(tableItems.ToArray(), this);

in dataSource

ViewController owner;
public TableSource (string[] items, ViewController owner)
{
    tableItems = items;
    this.owner = owner;
}

this can make your code easy to navigation to another VC. just use this instead of looking for current vc on Windows .

this.owner.PresentViewController(myTarget, true, null);

2. If your tableView fills with the screen , you can change your ViewController to UITableViewController.

since it inherits form IUITableViewDataSource , so it can handle the event(i.e. that method create the tableView) in its own class.

3.As I mentioned above ,check if that viewController exists

check the storyboard id and find the corresponding viewController in your project.

Upvotes: 1

Related Questions