Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Initiating TextView UI Update from another View Controller [Objective C]

MatthewMMatthewM West London, UKPosts: 41New Users @
Hi everyone,

I have an issue that I am stumped on and really could use some help please.

I will try and explain this as simply as I can.

I have two view controllers, VC A and VC B.

VC A is running code that is receiving data from a tcp stream. Upon detecting a certain trigger VC A calls a method on VC B.

The method on VB B is to read contents from a specific table in a SQLITE db and then use some of that data to populate a Text View on VC B.

Now, the issue is that if the method on VC B is initiated by VC A, the TEXT VIEW does NOT get updated. The method is definitely getting called because I can NSLOG the output from the method, it just doesnt update in the TEXT VIEW.

Now, I also have a button on VC B, as a test! When I press this button on VC B the method is called, reads the data from the db and then populates the TEXT VIEW. The button is calling the exact same method as VC A is calling.

Everything is working, when the method is called from VC A or VC B, apart from the TEXT VIEW being updated when calling from VC A.
[self.messageTXT performSelectorOnMainThread: @selector(setText:)
                                            withObject:[NSString stringWithFormat:@%@%@",  self.messageTXT.text, msg]
                                         waitUntilDone: YES];

I have tried forcing the TEXT VIEW update to use the main thread and still no luck when calling from VC A. Only if the button is pressed on VC B does it then update.

Has anyone any experience on calling methods on another VC and updating the UI using data from that particular VC and NOT the calling VC?

If you need any other code or have any questions please let me know.

Thanks in advance!!!

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,677Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    How have you established the connection between VC A and B?
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • MatthewMMatthewM West London, UKPosts: 41New Users @
    BrianSlick wrote: »
    How have you established the connection between VC A and B?

    Yep, as below:

    #import "msgView.h"
    @property (nonatomic,strong) msgView *msgViewV;
    
               msgView *vcb;
                vcb = [[msgView alloc] init];
                [vcb ListMessages];
    
    

    As mentioned before, VC A calls the method on VC B with no issue. The method extracts data from a database, but then fails to show that data in text view. This issue only occures when calling from VC A. If called from a button on VC B, then the text view populates.

    I just don't see how some code can run and some just gets ignored even though I force the text view update to run on the main thread. #weird

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,677Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Is this the only place that msgView is created? Is there a Storyboard here? How does msgView get put onto the screen?

    Class names should begin with capital letters, btw.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • MatthewMMatthewM West London, UKPosts: 41New Users @
    BrianSlick wrote: »
    Is this the only place that msgView is created? Is there a Storyboard here? How does msgView get put onto the screen?

    Class names should begin with capital letters, btw.

    msgView was created using Storyboard and the usual performSegueWithIdentifier: is how msgView "put onto the screen".

    I must also mention that the method on VC B is called by VC A while VC B has focus and is in view.

    Thanks Brian, appreciated. :-)

    Matt




  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,677Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    You have 2 distinct instances of VC B. One is on screen. This is the one that responds to your button press.

    The other is not on screen. Any time you alloc/init, you are making a new object. This is the one that VC A is talking to. That's why you don't see anything happen on screen; this instance isn't on screen.

    So, nothing to do with threading at all. You will need to establish a direct link between the instances created in your Storyboard.

    Logs such as these will help to reveal these issues:
    NSLog(@>>> Entering <%p> %s <<<, self, __PRETTY_FUNCTION__);
    NSLog(@<<< Leaving  <%p> %s >>>, self, __PRETTY_FUNCTION__);
    

    %p is the object address. Place these in your method in VC B, and you'll see that you do indeed have 2 different addresses depending on who calls.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • MatthewMMatthewM West London, UKPosts: 41New Users @

    This is from when VC A calls the method:

    >>> Entering <0x7feafcc27100> -[msgView ListMessages:] <<<
    <<< Leaving <0x7feafcc27100> -[msgView ListMessages:] >>>

    This is from when I press the button on msgView that calls the same method:

    >>> Entering <0x7feafcc20fb0> -[msgView ListMessages:] <<<
    <<< Leaving <0x7feafcc20fb0> -[msgView ListMessages:] >>>

    Think this proves your point then.

    So it looks like the connection I have between VC A and VC B is not correctly set?
  • MatthewMMatthewM West London, UKPosts: 41New Users @
    BTW, Thank you so much for pointing me in the right direction!

    I will now endeavour to learn how to do this properly.

    Thanks again Brian! Your comments were a great help towards me understanding the problem.

    Matt
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,677Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    You don't have a link between the items in your Storyboard. In VC A, you are creating a 2nd VC B, and then sending your updates to that one.

    So you either need to put your 2nd one on the screen, or you need to establish a connection to the 1st one (Storyboard). If it can be done directly in the Storyboard, I do not know how.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • MatthewMMatthewM West London, UKPosts: 41New Users @
    Brian,

    Thanks again for your help.

    Now I understand why it was happening I have now used a different approach to trigger the method in VC B.

    Quite simply I am now using NSNotificationCenter to tell VC B when to fire the required method!

    And, it's working perfectly!

    Don't know why I tried the route I did, but anyhow, it's all a learning curve even after 6 years developing in Obj C..!!

    Thanks again and have a great day!

    Matt
Sign In or Register to comment.