[ios]应用程序崩溃时试图改变 UITextView.text

发布时间: 2015/4/8 16:50:51
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我的应用程序崩溃时我试图改变 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; 线。在做什么错了?

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

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

解决方法 1:

您不必须从后台线程更改 UI * 对象。有主线程的工作要做。

您可以使用最小公倍数在主线程上执行所做的更改。这样的事情:

/* ... */
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];
官方微信
官方QQ群
31647020