Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2011, 04:11 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 36
evosonic is on a distinguished road
Default 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:

Code:
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSString *strFirst = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
    
    Test_Image_LoadViewController *main = [[Test_Image_LoadViewController alloc] init];
    
    [main setText:strFirst];
}
In Test_Image_LoadViewController I have the setText method setup:

Header:
Code:
-(void)setText:(NSString *)path;
Implementation File:
Code:
-(void)setText:(NSString *)path
{
       
    NSLog(@"Text: %@", path);
    tLab.text = path;
}
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?
evosonic is offline   Reply With Quote
Old 09-07-2011, 05:09 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by evosonic View Post
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:

Code:
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSString *strFirst = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
    
    Test_Image_LoadViewController *main = [[Test_Image_LoadViewController alloc] init];
    
    [main setText:strFirst];
}
In Test_Image_LoadViewController I have the setText method setup:

Header:
Code:
-(void)setText:(NSString *)path;
Implementation File:
Code:
-(void)setText:(NSString *)path
{
       
    NSLog(@"Text: %@", path);
    tLab.text = path;
}
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-08-2011, 07:57 AM   #3 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 36
evosonic is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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.
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!
evosonic is offline   Reply With Quote
Old 09-08-2011, 08:38 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by evosonic View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 373
8 members and 365 guests
apatsufas, JackReidy, jeroenkeij, Sami Gh, tim0504, UMAD, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:54 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0