Multithreading 更新UI控件';从后台线程获取运行时中的s值。
我知道这听起来是一个非常简单的问题。我还是iPad应用程序开发的新手。请告诉我如何在运行时更新内容。下面的代码可能有助于详细了解我的问题 在下面的代码中,我尝试在TexViewcontrol中显示文本内容。当我将for循环从10更改为100000时,我需要等待所有内容都填满。我需要在循环运行时在屏幕上显示内容Multithreading 更新UI控件';从后台线程获取运行时中的s值。,multithreading,uitextview,main,Multithreading,Uitextview,Main,我知道这听起来是一个非常简单的问题。我还是iPad应用程序开发的新手。请告诉我如何在运行时更新内容。下面的代码可能有助于详细了解我的问题 在下面的代码中,我尝试在TexViewcontrol中显示文本内容。当我将for循环从10更改为100000时,我需要等待所有内容都填满。我需要在循环运行时在屏幕上显示内容 - (IBAction)RunHelloWorld:(id)sender { tctView.text = @"tt"; for(int i=0;i<10;i++)
- (IBAction)RunHelloWorld:(id)sender
{
tctView.text = @"tt";
for(int i=0;i<10;i++)
{
NSString *result1 = @"Testing";
tctView.text = [tctView.text stringByAppendingString:result1];
tctView.scrollEnabled = YES;
}
}
-(iAction)RunHelloWorld:(id)发送方
{
tctView.text=@“tt”;
对于(inti=0;i,我最终通过以下URL的帮助找到了解决方案。
非常感谢
这是我的密码
PCViewController.h文件代码
#import <UIKit/UIKit.h>
@interface PCViewController : UIViewController
{
IBOutlet UITextView *txtView;
}
-(void)ReadFile;
- (void) mainThreadSetText:(NSString*)text;
@end
#import "PCViewController.h"
@interface PCViewController ()
@end
@implementation PCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[NSThread detachNewThreadSelector:@selector(ReadFile) toTarget:self withObject:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)ReadFile
{
// Check whether current thread is externally terminated or not.
char line[1024];
FILE *fp = fopen("/Users/Teva/Desktop/File1.txt","r");
while([[NSThread currentThread] isCancelled] == NO)
{
if( fp != NULL )
{
while( fgets(line,1024,fp) )
{
printf("%s\n",line);
NSString *result1 = [NSString stringWithCString:line encoding:NSASCIIStringEncoding];
[self performSelectorOnMainThread:@selector(mainThreadSetText:) withObject:result1 waitUntilDone:YES];
[NSThread sleepForTimeInterval:1.0];
}
}
}
}
// Screen processing should be done outside the Thread.
- (void) mainThreadSetText:(NSString*)text
{
txtView.text = [txtView.text stringByAppendingString:text];
txtView.scrollEnabled = YES;
}
- (void)dealloc
{
[txtView release];
[super dealloc];
}
@end