Change properties of object from external delegate
I have an NSURLConnection that I have set the delegate to point to an external object. I have my delegate methods in that file and they fire properly. However, on the connectionDidFinishLoading method I want to change the text of a label on my main view - but it's not working.
Here is connectionDidFinishLoading from the external delegate:
I get the correct text in the log, but the label will not change. If I move (just for testing purposes) the delegate methods inside the main view implementation file, it works. I have multiple NSURLConnection's going so that is why I am separating them out.
I have an NSURLConnection that I have set the delegate to point to an external object. I have my delegate methods in that file and they fire properly. However, on the connectionDidFinishLoading method I want to change the text of a label on my main view - but it's not working.
Here is connectionDidFinishLoading from the external delegate:
I get the correct text in the log, but the label will not change. If I move (just for testing purposes) the delegate methods inside the main view implementation file, it works. I have multiple NSURLConnection's going so that is why I am separating them out.
Any ideas?
A copule of things:
Your connectionDidFinishLoading method is creating a brand-new Test_Image_LoadViewController object. If you already had one of those view controllers, you've just created a second one, that won't have anything in common with the first one. Did you mean to create a new one at this point?
Second: View controllers don't load their views right away, and can have them unloaded when they are not frontmost. You don't want to try to set your label directly like that.
Instead, create a retained NSString property called something like labelText. Just have the connectionDidFinishLoading method set the property. Then, in your Test_Image_LoadViewController's viewWillAppear method, set the label.text value using the string.
That way, the view controller remembers what it is supposed to display in that label, and displays it at the appropriate time.
If you later push another view controller on top of that view controller, let the user do memory-intensive stuff, and then come back to the Test_Image_LoadViewController, the Test_Image_LoadViewController's entire view hierarchy, including it's label field, may have been released while the other view controller was in use. In that case, the view controller will load it's views again, and when viewWillAppear is called, it will set up the label text correctly again.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Your connectionDidFinishLoading method is creating a brand-new Test_Image_LoadViewController object. If you already had one of those view controllers, you've just created a second one, that won't have anything in common with the first one. Did you mean to create a new one at this point?
Second: View controllers don't load their views right away, and can have them unloaded when they are not frontmost. You don't want to try to set your label directly like that.
Instead, create a retained NSString property called something like labelText. Just have the connectionDidFinishLoading method set the property. Then, in your Test_Image_LoadViewController's viewWillAppear method, set the label.text value using the string.
That way, the view controller remembers what it is supposed to display in that label, and displays it at the appropriate time.
If you later push another view controller on top of that view controller, let the user do memory-intensive stuff, and then come back to the Test_Image_LoadViewController, the Test_Image_LoadViewController's entire view hierarchy, including it's label field, may have been released while the other view controller was in use. In that case, the view controller will load it's views again, and when viewWillAppear is called, it will set up the label text correctly again.
Hey Duncan, I did not mean to create a completely new instance of Test_Image_LoadViewController - how can I reference the view controller being shown on the screen? I think that's the problem, because anything line of code that deals with changing a property of any item on the view is completely ignored. The NSLog always fires, but the text just will not change.
Hey Duncan, I did not mean to create a completely new instance of Test_Image_LoadViewController - how can I reference the view controller being shown on the screen? I think that's the problem, because anything line of code that deals with changing a property of any item on the view is completely ignored. The NSLog always fires, but the text just will not change.
Thanks for your help!
You asked:
Quote:
how can I reference the view controller being shown on the screen?
That depends on how your code is structured. How do you create the Test_Image_LoadViewController that is already on-screen? Do you create it in code somewhere by doing call that contains "[[Test_Image_LoadViewController alloc] init]"
Another way to create your view controllers is to add an instance of your view controller to your mainWindow nibfile. That's what I usually do.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.