iOS.Lover
iOS.Lover

Reputation: 6051

Set UILabel's text from custom method

I created a method which gets data from a server, everything works fine except when I try to set string to for example UILabel or UITextView, nothing shows and changed ! here is my code :

    - (void)viewDidLoad
{
    [super viewDidLoad];


    [self getDataFromURL:@"http://somesites.net/panel/services?action=events&num=1"
                setTitle:_eTitle1.text image:_eImage1 description:_eNews1.text];

}

Getting Data :

 -(void)getDataFromURL:(NSString*)url setTitle:(NSString*)eTitle
                    image:(UIImageView*)eImages description:(NSString*)eDescriptions {


        NSURL *URL = [NSURL URLWithString:url];
        NSError *error1;
        NSString *strPageContent = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error1];
        strPageContent = [strPageContent gtm_stringByUnescapingFromHTML];
        if ([strPageContent rangeOfString:@"<plist version=\"1.0\">"].location != NSNotFound) {
            NSRange range = [strPageContent rangeOfString:@"<plist version=\"1.0\">"];
            strPageContent = [strPageContent substringWithRange:NSMakeRange(range.location+range.length, strPageContent.length-(range.location+range.length))];
            strPageContent = [strPageContent stringByReplacingOccurrencesOfString:@"</plist>" withString:@""];
        }


        NSError *error = nil;
        NSDictionary *dict = [XMLReader dictionaryForXMLString:strPageContent options:XMLReaderOptionsProcessNamespaces
                                                         error:&error];

        if ([dict count]>0) {

            NSDictionary *dictInner = [dict objectForKey:@"dict"];
            NSArray *arrValues = [dictInner objectForKey:@"string"];



           NSString *  strTitle = [[arrValues objectAtIndex:0] objectForKey:@"text"];
           NSString *strImage = [[arrValues objectAtIndex:1] objectForKey:@"text"];
           NSString *  strDescription = [[arrValues objectAtIndex:2] objectForKey:@"text"];

            eTitle = strTitle;
            eDescriptions = strDescription;

    //        [eImages setImageWithURL:[NSURL URLWithString:strImage]
    //                placeholderImage:[UIImage imageNamed:@"loadingPad.jpg"]];

            NSLog(@"Title: %@ | Image: %@ | Desc: %@",eTitle,strImage,eDescriptions);

        }


    }

compiler gives me the right information ! but these string could not set to my lable , IF I put my lable's string into the method it works !!! :

_eTitle1.text = strTitle ;

Upvotes: 0

Views: 47

Answers (1)

Nicola Miotto
Nicola Miotto

Reputation: 3696

It's completely normal: when you pass the "text" object to the method, you are passing the pointer to it. Assigning to it directly another NSString object will just assign a new pointer. In order to have side effect on a string you gotta use NSMutableString, but the UILabel has just an immutable NSString for the text attribute. So the only solution is to pass the UILabel or pass an initialized empty mutable string inside the method, change the content via [eTitleText setString:strTitle] and then, outside the method, assign it to the UILabel text attribute.

So, either you change the method like this (as you already did):

-(void)getDataFromURL:(NSString*)url setTitle:(UILabel*)eTitle
                    image:(UIImageView*)eImages description:(NSString*)eDescriptions {
...
eTitle.text = strTitle;
...

and using it like this:

- (void)viewDidLoad
{
    [super viewDidLoad];


    [self getDataFromURL:@"http://somesites.net/panel/services?action=events&num=1"
                setTitle:_eTitle1 image:_eImage1 description:_eNews1.text];

}

Or you can go this other way:

-(void)getDataFromURL:(NSString*)url setTitle:(NSMutableString*)eTitle
                        image:(UIImageView*)eImages description:(NSString*)eDescriptions
...
[eTitle setString:strTitle];
...

and using it like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableString *titleText = [NSMutableString new];
    [self getDataFromURL:@"http://somesites.net/panel/services?action=events&num=1"
                setTitle:titleText image:_eImage1 description:_eNews1.text];
    eTitle1.text = titleText;
}

Upvotes: 1

Related Questions