MapMapM 2013-06-19 03:12 采纳率: 0%
浏览 3051
已采纳

修改UITextview内容遇到问题

从API中获取数据,然后将数据写入UITextview。但是系统报出错误。

下面是读取写入数据的代码:

NSOperationQueue *apiCallsQueue = [[NSOperationQueue alloc] init];
NSString *urlstring=@"x.com/api.php?id=";
urlstring =[urlstring stringByAppendingString:self.newsId];
NSURL *newsdetail = [NSURL URLWithString:urlstring];
newsdetailrequest = [NSURLRequest requestWithURL:newsdetail];

@try
{
    [NSURLConnection sendAsynchronousRequest:newsdetailrequest queue:apiCallsQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        NSDictionary *dictionary =  [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
        [textview setText:[dictionary objectForKey:@"title"]];
    }];
}
@catch (NSException *exception)
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Error"
                                                       delegate:nil
                                              cancelButtonTitle:@"Tamam"
                                              otherButtonTitles:nil];
    [errorView show];
}

错误输出:

2013-06-18 16:37:16.214 xpro[6816:5103] bool _WebTryThreadLock(bool), 0x7142ee0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1   0x3571fe9 WebThreadLock
2   0x15846e -[UITextView setText:]
3   0x5f00 __39-[NewsDetailViewController viewDidLoad]_block_invoke
4   0xbdbcf8 __block_global_0
5   0xb4375a -[NSBlockOperation main]
6   0xb11453 -[__NSOperationInternal start]
7   0xb11164 -[NSOperation start]
8   0xb9da31 __block_global_6
9   0x49ff53f _dispatch_call_block_and_release
10  0x4a11014 _dispatch_client_callout
11  0x4a022e8 _dispatch_root_queue_drain
12  0x4a02450 _dispatch_worker_thread2
13  0x96c53e72 _pthread_wqthread
14  0x96c3bd2a start_wqthread
(lldb) 

controller.h文件:

@interface NewsDetailViewController : UIViewController
{
NSURLRequest *newsdetailrequest;
NSMutableData *newsdetailData;
}
@property (weak, nonatomic) IBOutlet UINavigationBar *newsdetailTitle;
@property (weak, nonatomic) IBOutlet NSString *navBarTitle;
@property (weak, nonatomic) IBOutlet NSString *newsId;
@property (weak, nonatomic) IBOutlet UITextView *htmlDetail;
  • 写回答

1条回答 默认 最新

  • Chengzi_963 2013-06-19 07:41
    关注

    在主线程中:

    NSOperationQueue *apiCallsQueue = [[NSOperationQueue alloc] init];
    
    NSString *urlstring=@"x.com/api.php?id=";
    urlstring =[urlstring stringByAppendingString:self.newsId];
    
    NSURL *newsdetail = [NSURL URLWithString:urlstring];
    newsdetailrequest = [NSURLRequest requestWithURL:newsdetail];
    
    @try
    {
        [NSURLConnection sendAsynchronousRequest:newsdetailrequest queue:apiCallsQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
        {
            NSDictionary *dictionary =  [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
    
            dispatch_async(dispatch_get_main_queue(),
            ^{
                [textview setText:[dictionary objectForKey:@"title"]];
            });
        }];
    }
    @catch (NSException *exception)
    {
        UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"Error"
                                                           delegate:nil
                                                  cancelButtonTitle:@"Tamam"
                                                  otherButtonTitles:nil];
        [errorView show];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗