PrepareForSegue is not called

trying to get a tableviewcontroller working in Visual Studio 2017 but the PrepareForSegue is never called. my tableviewcontroller represents a list of machines and want to seque to machine detail. my

I have tried creating the seque between the tableviewcontroller and the detailview and the cell in the tableviewcontroller and detailview but neither works.
my tableviewcell has a basic style {tried custom and no luck either)

key part in my tableviewcontroller looks like

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

        List<Machine> listallMachines = service.GetAllMachines();
        TableView.Source = new machinesDataSource(listallMachines, this);
        this.NavigationItem.Title = "Machines";
    }

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

        if (segue.Identifier == "MachineDetailSeque")
        {
            var machineController = segue.DestinationViewController as MachineDetailController;
            if (machineController != null)
            {
                var source = TableView.Source as machinesDataSource;
                var rowPath = TableView.IndexPathForSelectedRow;
                var item = source.GetItem(rowPath.Row);
                machineController.SelectedMachine = item;
            }
        }
    }

and my data source:
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(cellId) as UITableViewCell;
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, cellId);
}
Machine machine = machines[indexPath.Row];
cell.TextLabel.Text = machine.Name;
cell.ImageView.Image = UIImage.FromFile("Images/" + machine.Name + ".jpg");
return cell;
}
my list appears but when I click on a row my breakpoint on PrepareForSegue is never called.
it is amazing to me that this process is tied to design tools as I long time .Net dev this seems weak. There must be a clean programatic way to do this.
I can get the seque to occur by overriding the RowSelected in my datasource and manually doing the seque. This works as i navigate to the detailview but
the PrepareForSegue is not called. I prefer this method as do not like relying on design tools but need a clean way to pass the data. of course I can hack a solution but do not want to

Best Answer

Answers

  • tuyenvtuyenv VNUniversity ✭✭✭

    How do you do manually the segue?

  • johnmcfetridge.4012johnmcfetridge.4012 USMember ✭✭

    I captured the caller's controller in my datasource constructor:

    public machinesDataSource(List machines, UITableViewController caller)
    {
    this._controller = caller as machineController;

            this.machines = machines;
        }
    

    then overrode

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {

            _controller.PerformSegue("mySeque", _controller);
        }
    

    as i said the navigation occurred but the onPrepareSegue was not called (breakpoint there). sure I can hack it but not my goal

  • johnmcfetridge.4012johnmcfetridge.4012 USMember ✭✭

    thanks Minafawzy but no that is the id that I gave the Seque in the designer. Anyways using RowSelected override in the DataSource was an attempt to get by the real problem that I described in the top part. I should not need the RowSelected and PerformSeque . Now it is good to know this method should work but I found the REAL ISSUE.

    my tableview cell has a custom type and the identifier did not match the id I assigned in the datasource

    NSString cellId = new NSString("machine");
    
        public machinesDataSource(List<Machine> machines, UITableViewController caller)
        { 
    
            this.machines = machines;
        }
    
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell(cellId) as UITableViewCell;
            if (cell == null)
            {
                cell = new UITableViewCell(UITableViewCellStyle.Default, cellId);
            }
    

    I had noticed this earlier and must have accidentally removed the identifier. Not thrilled by whole IOS approach that is so tied to design tools. missing my XAML or AXML

  • johnmcfetridge.4012johnmcfetridge.4012 USMember ✭✭
    Accepted Answer

    I just posted a series of 3 videos on Youtube on TableViews and Navigation. The source is also on Github. Pretty rough as mostly for my use to refer to later as I find this a great way to refresh my memory in not just code but sports.

    getting started using a Tableview object
    https://www.youtube.com/edit?o=U&video_id=mybBEd20DaY

    using Tableview with shared PCL
    https://www.youtube.com/edit?o=U&video_id=UOmGLRQtlTk

    add navigation and detail view
    https://www.youtube.com/edit?o=U&video_id=i-wRDpEgNVk

    Hope these help someone

Sign In or Register to comment.