2012-03-24 96 views
3

當我嘗試更改UITextView文本時,我的應用程序崩潰。嘗試更改UITextView.text時應用程序崩潰

下面是代碼:(頭文件):

// 
#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從viewDidLoad

(Xcode的4.4 DP2,iPhone模擬器5.1 SDK基地4.3)

+0

什麼是從崩潰的錯誤信息?另外,輸出strResult3內容的NSLog語句是否能提供您期望的輸出結果? – jonkroll 2012-03-24 05:36:13

+0

是的。 NSLog給我期望的輸出。 CRASH錯誤按摩: – theShay 2012-03-24 05:42:08

+0

'布爾_WebTryThreadLock(布爾),0x6d9d7d0:試圖從主線程或Web線程以外的線程獲取Web鎖。這可能是從輔助線程調用UIKit的結果。現在崩潰... 1 WebThreadLock 2 - [UITextView中的setText:] 3 - [DetailViewController loadData] 4 - [NSThread主] 5 __NSThread__main__ 6 _pthread_start 7 thread_start' – theShay 2012-03-24 05:44:51

回答

4

調用[self performSelectorInBackground:@selector(loadData) withObject:nil];不得更改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]; 
相關問題