iphone - 我怎样才能改变 UITextview 的内容

标签 iphone ios objective-c ipad

我正在从我的 api 获取数据并尝试将数据写入 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;

最佳答案

试试这个。

分派(dispatch)到主线程进行 UI 更新。

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

关于iphone - 我怎样才能改变 UITextview 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17170496/

相关文章:

ios - 呈现的 View Controller 在使用自定义 UIViewController 动画制作动画后消失

ios - 以编程方式控制 ScrollView — Objective-c

objective-c - 为什么我必须点击 UIDatePicker 两次才能设置时间?

ios - 如何在 Objective-C 中从 HTML 读取 JSON-LD 数据?

iphone - 为什么我的子类 UIView 在 UIScrollView 中没有接收到移动的触摸?

ios - 如何将 TabBar 项目引导到特定 View

iphone - 键盘隐藏时的iOS事件

ios - 音视频合并后的视频方向

iphone - 何时在 iPhone 中释放 NSString

通过 WiFi/BT 传输 iPhone RS232 数据