0

i have a uivewcontroller that has event handler on view loading .it contains code that firing in background and also the UI , so for the UI code im using InvokeOnMainThread . it works fine till i navigate to another controller and return back to it. when the event fires it doesn't execute the UI code . every time i push to this controller i'm creating a new instance of it . so i tried to make it only one instance of this controller and it works fine !!!! Can any one explain to me why this happening ??!!

        public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        if (hubConnection == null) {
            hubConnection = new HubConnection ("http://" + JsonRequest.IP + ":8070/", "userId=" + userId);
            hubProxy = hubConnection.CreateHubProxy ("myChatHub");
            hubConnection.EnsureReconnecting ();
            //}
            if (hubConnection.State == ConnectionState.Disconnected) {
                hubConnection.Start ();
            }
            hubConnection.Received += HandleReceived;

        }
    }

    void HandleReceived (string obj)
    {
        InvokeOnMainThread (delegate {
            discussion.Root [0].Add (new ChatBubble (true, text));

        });
    }
2
  • A look at your code would help, a lot.
    – James
    Oct 21, 2013 at 10:29
  • that's a simple code i just created.. you need to create instance of the controller and push and it will show that second time you push the controller when u click on the button it will fire but won't execute the handler Oct 21, 2013 at 10:56

2 Answers 2

2

First of all there's no need need for using InvokeOnMainThread here since TouchUpInside is guaranteed to fire on the main thread.

Second problem is that your sendButton field is static but your controller instance is not. And that 's why it is only ever added to the first instance of your controller. Remove the static keyword and it should work.

1
  • actually this is not my exact situation, i just tried to simulate the problem with a simpler sample , my exact situation is that im implementing signalr chat im my controller .. so im initializing my hub connection and attaching received event .. there's must be one object of the hub connection in the entire application so i tried to make it static just like the button sample .. but the received event doesn't execute the UI code .. so i tried to make it non static by sending it as a parameter but same issue !! Oct 21, 2013 at 12:17
1

You should, almost always, never use a static UI component, that's almost always going to cause problems. Any sort of UI building is generally done in the LoadView method and any sort of event wiring / view setup should be done in ViewDidLoad e.g.

public class TestController : UITableViewController
{
    private UIButton sendButton;
    ...
    public override void LoadView()
    {
        base.LoadView();
        if (sendButton == null)
        {
            sendButton = new UIButton (UIButtonType.RoundedRect)
            {
                 Frame = new RectangleF (100, 100, 80, 50),
                 BackgroundColor = UIColor.Blue
            };
            View.AddSubview(sendButton);
        }
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        sendButton.TouchUpInside += HandleTouchUpInside;
    }

    public override void ViewDidUnload()
    {
        if (sendButton != null)
        {
            sendButton.Dispose();
            sendButton = null;
        }
    }
}

Couple of notes:

  • ViewDidLoad/ViewDidUnload were deprecated in iOS 6 so you don't need to do this sort of stuff anymore, it's recommended you put cleanup code in the DidReceiveMemoryWarning method instead.
  • Your code is already running on in the main loop - InvokeOnMainThread is unnecessary.
3
  • actually this is not my exact situation, i just tried to simulate the problem with a simpler sample , my exact situation is that im implementing signalr chat im my controller .. so im initializing my hub connection and attaching received event .. there's must be one object of the hub connection in the entire application so i tried to make it static just like the button sample .. but the received event doesn't execute the UI code .. so i tried to make it non static by sending it as a parameter but same issue !! Oct 21, 2013 at 12:21
  • @AhmedMohamedAnas well that's quite an important detail which you should have probably documented in your original question... Regardless, you appear to be tying your hub notifications to a UI component which isn't a great idea. You should show some code demonstrating what you have at the minute (with regards to the hub connection etc.). At the moment, your issue is too blurry to grasp.
    – James
    Oct 21, 2013 at 12:26
  • I updated the code as you see on received im getting a string message and im adding it as a new element to the tableview ... Oct 21, 2013 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.