在尝试更改UITextView.text时,应用程序崩溃了

时间:2012-03-24 05:09:41

标签: objective-c ios xcode uitextview

当我尝试更改UITextView文字时,我的应用程序崩溃了。

以下是代码:(头文件):

//
#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController {

    IBOutlet UIImageView *appImage;
    IBOutlet UILabel *appName;
    IBOutlet UILabel *appDeveloper;
    IBOutlet UILabel *appVersion;
    UIActivityIndicatorView *loading;
    IBOutlet UITextView *appAbout;
    NSString *selectedApp;

}

@property (nonatomic, retain) NSString *selectedApp;
@property (nonatomic, retain) IBOutlet UIImageView *appImage;
@property (nonatomic, retain) IBOutlet UILabel *appName;
@property (nonatomic, retain) IBOutlet UILabel *appDeveloper;
@property (nonatomic, retain) IBOutlet UILabel *appVersion;
@property (nonatomic, retain) UIActivityIndicatorView *loading;
@property (nonatomic, retain) UITextView *appAbout;

@end

实施档案:

- (void)loadData {

    NSString *trimmedString = [selectedApp stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    self.navigationItem.title = @"Info";
    NSLog(@"%@", trimmedString);
    NSString *website = [NSString stringWithFormat:@"http://shaymargolisapps.x10.mx/send.php?mission=GetNameOfApps&aname=%@", trimmedString]; 
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:website]];
    NSString *strResult = [[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]autorelease];
    appName.text = strResult;
    NSString *website1 = [NSString stringWithFormat:@"http://shaymargolisapps.x10.mx/send.php?mission=GetDeveloperOfApp&aname=%@", trimmedString]; 
    NSData *dataURL1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:website1]];
    NSString *strResult1 = [[[NSString alloc] initWithData:dataURL1 encoding:NSUTF8StringEncoding]autorelease];
    appDeveloper.text = strResult1;
    NSString *website2 = [NSString stringWithFormat:@"http://shaymargolisapps.x10.mx/send.php?mission=GetVersionOfApp&aname=%@", trimmedString]; 
    NSData *dataURL2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:website2]];
    NSString *strResult2 = [[[NSString alloc] initWithData:dataURL2 encoding:NSUTF8StringEncoding]autorelease];
    appVersion.text = strResult2;
    NSString *website3 = [NSString stringWithFormat:@"http://shaymargolisapps.x10.mx/send.php?mission=GetInfoOfApp&aname=%@", trimmedString];
    NSData *dataURL3 = [NSData dataWithContentsOfURL:[NSURL URLWithString:website3]];
    NSString *strResult3 = [[[NSString alloc] initWithData:dataURL3 encoding:NSUTF8StringEncoding]autorelease];
    NSLog(@"%@", strResult3);
    **appAbout.text = strResult3;**
    NSString *website4 = [NSString stringWithFormat:@"http://shaymargolisapps.x10.mx/send.php?mission=GetImageOfApp&aname=%@", trimmedString];
    NSData *dataURL4 = [NSData dataWithContentsOfURL:[NSURL URLWithString:website4]];
    NSString *strResult4 = [[[NSString alloc] initWithData:dataURL4 encoding:NSUTF8StringEncoding]autorelease];
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strResult4]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];
    [appImage setImage:image];
    [image release];
    [imageData release];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [loading stopAnimating];
    [loading setHidden:YES];
    [appName setHidden:NO];
    [appDeveloper setHidden:NO];
    [appVersion setHidden:NO];
    [appImage setHidden:NO];
    [appAbout setHidden:NO];

}

我的应用程序在appAbout.text = strResult3;行崩溃了。我做错了什么?

使用[self performSelectorInBackground:@selector(loadData) withObject:nil];

中的viewDidLoad调用loadData

(Xcode 4.4 DP2,iPhone模拟器5.1,基本SDK 4.3)

1 个答案:

答案 0 :(得分:4)

您不得从后台线程更改UI *对象。这必须从主线程完成。

您可以使用gcd在主线程上执行更改。像这样:

/* ... */
NSString *strResult4 = [[[NSString alloc] initWithData:dataURL4 encoding:NSUTF8StringEncoding]autorelease];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strResult4]];
UIImage* image = [[UIImage alloc] initWithData:imageData];

dispatch_async(dispatch_get_main_queue(), ^{
    self.navigationItem.title = @"Info";
    appName.text = strResult;
    appDeveloper.text = strResult1;
    appVersion.text = strResult2;
    appAbout.text = strResult3;
    [appImage setImage:image];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [loading stopAnimating];
    [loading setHidden:YES];
    [appName setHidden:NO];
    [appDeveloper setHidden:NO];
    [appVersion setHidden:NO];
    [appImage setHidden:NO];
    [appAbout setHidden:NO];
});

[image release];
[imageData release];