Forum Xamarin.iOS

Xamarin.iOS and event handling

JohnNolanJohnNolan USMember

Hi all,

I have a more complex scenario than the one depicted in the documentation here in which I have the following UI hierarchy.

UIViewController  (needs to subscribe and respond to the event raised in the RowSelected)
|
----> UICollectionView
    |
    ----> UICollectionViewScoure
        |
        ----> UICollectionViewCell
            |
            ----> UITableView
                |
                ----> UITableViewSource    (raises an event in an overridden RowSelected)
                    |
                    ----> UITableViewCell     

The RowSelected method raises an event which I am hoping to provide a delegate for in the parent UIViewController. The UIViewController will then interrogate the object sent to it and display a new UIViewController modally with the information gleaned from the selected table view cell.

I really want to avoid passing a reference to the UIViewController around and try and keep it as decoupled as possible. However, is it event possible without using a UITableViewDelegate and doing it this way.

The problem I am having is that the event is raised fairly deep and out of context....

Any idea how best to achieve this in Xamarin?

Best Answer

Answers

  • JohnNolanJohnNolan USMember

    UPDATE:

    Have been reading up and found an interesting article on SO from DavidBish

    public class B
    {
        //A public event for listeners to subscribe to
        public event EventHandler SomethingHappened;
    
        private void Button_Click(object o, EventArgs s)
        {
            //Fire the event - notifying all subscribers
            if(SomethingHappened != null)
                SomethingHappened(this, null);
        }
    
    public class A
    {
        //Where B is used - subscribe to it's public event
        public A()
        {
            B objectToSubscribeTo = new B();
            objectToSubscribeTo.SomethingHappened += HandleSomethingHappening;
        }
    
        public void HandleSomethingHappening(object sender, EventArgs e)
        {
            //Do something here
        }
    }
    

    However, I cannot instantiate a UITableSource within my UIViewController in my scenario as it is actually created a few layers down.

  • JohnNolanJohnNolan USMember

    EventAggregator! thanks so much that is ideal.

Sign In or Register to comment.