簡體   English   中英

通過自定義方法設置UILabel的文本

[英]Set UILabel's text from custom method

我創建了一個從服務器獲取數據的方法,除了我嘗試將字符串設置為例如UILabelUITextView之外,其他所有方法都工作正常,什么都沒有顯示和更改! 這是我的代碼:

    - (void)viewDidLoad
{
    [super viewDidLoad];


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

}

獲取數據:

 -(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);

        }


    }

編譯器給我正確的信息! 但是這些字符串不能設置為我的標簽,如果我將標簽的字符串放入可以工作的方法中!

_eTitle1.text = strTitle ;

這是完全正常的:將“文本”對象傳遞給方法時,就是將指針傳遞給該方法。 直接為其分配另一個NSString對象將只分配一個新的指針。 為了對字符串產生副作用,您必須使用NSMutableString ,但UILabel的text屬性只有一個不變的NSString。 因此,唯一的解決方案是在方法內部傳遞UILabel或傳遞初始化的空可變字符串,通過[eTitleText setString:strTitle]更改內容,然后在方法外部將其分配給UILabel text屬性。

因此,您可以像這樣更改方法(就像您已經做的那樣):

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

並像這樣使用它:

- (void)viewDidLoad
{
    [super viewDidLoad];


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

}

或者,您可以采用其他方式:

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

並像這樣使用它:

- (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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM