简体   繁体   English

从未调用重写的DialogViewController方法?

[英]Overridden DialogViewController methods are never called?

This is my first question, I apologize if it is not the best. 这是我的第一个问题,如果不是最好的,我道歉。

I am brand new to using Xamarin and iOS development. 我是使用Xamarin和iOS开发的新手。 I have a custom implementation of DialogViewController so that I can override certain methods on UITableViewController. 我有一个DialogViewController的自定义实现,以便我可以覆盖UITableViewController上的某些方法。 For the sake of the question, it is a very basic implementation. 为了这个问题,这是一个非常基本的实现。

public class CustomDialogViewController : DialogViewController
    {
        public CustomDialogViewController(RootElement root)
            : base (root)
        { }

        public override NSIndexPath WillSelectRow(UITableView tableView, NSIndexPath indexPath)
        {
            return base.WillSelectRow(tableView, indexPath);
        }

        public override NSIndexPath WillDeselectRow(UITableView tableView, NSIndexPath indexPath)
        {
            return base.WillDeselectRow(tableView, indexPath);
        }

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            base.RowSelected(tableView, indexPath);
        }
    }

I create my instance like this: 我像这样创建我的实例:

var rootElement = new RootElement("Main");
_mainSection = new Section();
rootElement.Add(_mainSection);
dvcProductGroupList = new CustomDialogViewController(rootElement);
dvcProductGroupList.Style = UITableViewStyle.Plain;
dvcProductGroupList.TableView.Frame = new RectangleF(...);
dvcProductGroupList.View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
dvcProductGroupList.OnSelection += dvcProductGroupList_OnSelection;

The DialogViewController is then added to the view. 然后将DialogViewController添加到视图中。 I put breakpoints at the start of each of those overrides but they are never hit. 我在每个覆盖的开头都设置了断点,但它们从未被击中。 At the point of creation there are no elements but when the view loads it populates the dialog view controller with some data, 4 string elements. 在创建时没有元素,但是当视图加载时,它会使用一些数据,4个字符串元素填充对话框视图控制器。 I would really appreciate any assistance in trying to get more control over row selection using MonoTouch Dialog. 我非常感谢您尝试使用MonoTouch Dialog更好地控制行选择。 I am trying to get the effect of canceling a row change so that it doesn't select another row when clicked and I think those are the correct methods, I just don't understand why they are never getting hit and I feel like it's a really simple problem. 我试图获得取消行更改的效果,以便在单击时不会选择另一行,我认为这些是正确的方法,我只是不明白为什么它们永远不会被击中而我觉得它是一个真的很简单。

Thanks. 谢谢。

Whether or not I am approaching this in the correct way I am not sure but I did get the answer from someone regarding the exact issue I was having with the overridden methods not getting called, so I will post their answer in case anyone has the same issue. 无论我是否以正确的方式接近这一点,我都不确定,但我确实从某人那里得到了关于我所遇到的被覆盖的方法未被调用的确切问题的答案,所以如果有人有相同的话,我会发布他们的答案问题。 Credit to JonathanBird from the Xamarin Forums. 感谢来自Xamarin论坛的JonathanBird。

You are attempting to override these methods in the wrong class. 您试图在错误的类中覆盖这些方法。 DialogViewController uses a UITableViewSource object to build out the UITableView. DialogViewController使用UITableViewSource对象构建UITableView。 My understanding is that when a UiTableViewSource is used to configure a UITableViewController then methods in the UITableViewController are not called. 我的理解是,当使用UiTableViewSource配置UITableViewController时,不会调用UITableViewController中的方法。 Since DialogViewController is a subclass of UITableViewController this applies to it as well. 由于DialogViewController是UITableViewController的子类,因此它也适用于它。

DialogViewController uses a UITableViewSource object of subtype DialogViewController.Source. DialogViewController使用子类型DialogViewController.Source的UITableViewSource对象。 If you want to override the methods in question then you need to subclass DialogViewController.Source and override the methods there. 如果要覆盖有问题的方法,则需要继承DialogViewController.Source并覆盖那里的方法。

Finally, you will need to substitute the the DialogViewController.Source that the DialogViewController creates with the one you have created. 最后,您需要将DialogViewController创建的DialogViewController.Source替换为您创建的那个。 You have an opportunity to do this by overriding CreateSizingSource () in your CustomDialogViewController. 您有机会通过覆盖CustomDialogViewController中的CreateSizingSource()来完成此操作。

DialogViewController uses the following implementation of CreateSizingSouce(). DialogViewController使用CreateSizingSouce()的以下实现。

public virtual DialogViewController.Source CreateSizingSource (bool unevenRows)
{
    return (!unevenRows) ? new DialogViewController.Source (this) : new DialogViewController.SizingSource (this);
}

If your implementation does not make use of unevenRows then you only need to worry about returning one type of Source. 如果您的实现没有使用unevenRows,那么您只需要担心返回一种类型的Source。

For example the override in your CustomDialogViewController might be: 例如,CustomDialogViewController中的覆盖可能是:

public override Source CreateSizingSource (bool unevenRows)
{
    return new CustomSource(this);
}

Where CustomSource is your subclass of DialogViewController.Source which overrides the methods you desire. 其中CustomSource是DialogViewController.Source的子类,它覆盖了您想要的方法。

Below is one of many possible forms your implementation might take which shows an override of the RowSelected method. 下面是您的实现可能采用的许多可能形式之一,它显示了RowSelected方法的重写。

public class CustomDialogViewController : DialogViewController
{
    public CustomDialogViewController (RootElement root) : base (root) {}

    public override Source CreateSizingSource (bool unevenRows)
    {
        return new CustomSource(this);
    }

    private class CustomSource : DialogViewController.Source
    {
        public CustomSource (CustomDialogViewController controller) : base (controller)
        {}

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            // You handle row selected here
            base.RowSelected (tableView, indexPath);
        }
    }
}

These methods are meant to make the managed UITableViewController conform to the ObjC UITableViewDataSource protocol. 这些方法旨在使托管的UITableViewController符合ObjC UITableViewDataSource协议。 You can then use: 然后你可以使用:

this.TableView.WeakDataSource = this;

in your controller. 在你的控制器中。

Using MT.Dialog you only deal with elements and only in rare cases with the underlying UITableView . 使用MT.Dialog只处理元素,并且仅在极少数情况下使用底层UITableView Usually your elements have tap/click handlers. 通常,您的元素具有点击/点击处理程序。 In those, perform your actions. 在那些,执行您的行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM