ios线程监听模式
来源:互联网 发布:黄庭坚书法知乎 编辑:程序博客网 时间:2024/05/22 13:03
#import "TutorialProjectViewController.h"
@implementation TutorialProjectViewController
@synthesize threadValueLabel, threadProgressView, testValueLabel, threadStartButton;
// ------ Tutorial code starts here ------
- (IBAction) startThreadButtonPressed:(UIButton *)sender {
threadStartButton.hidden = YES;
threadValueLabel.text = @"0";
threadProgressView.progress = 0.0;
[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];
}
- (void)startTheBackgroundJob {
//注册一个监听者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Update) name:@"UpdateTableView" object:nil];
NSLog(@"1111111111");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
[NSThread sleepForTimeInterval:3];
[self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void)makeMyProgressBarMoving {
xx++;
// NSLog(@"22222222 %i",xx);
float actual = [threadProgressView progress];
threadValueLabel.text = [NSString stringWithFormat:@"%.2f", actual];
if (actual < 1) {
threadProgressView.progress = actual + 0.01;
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
}
if (xx==5||xx==15) {
//发出一个消息。此前注册的监听者可以发现这个消息,并且出发相应的方法
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateTableView" object:nil];
NSLog(@"xxxxxxxxxxxxxx==3");
}
if (xx==10) {
//取消监听。
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UpdateTableView" object:nil];
}
else threadStartButton.hidden = NO;
}
//受到监听的消息时,调用这个函数
-(void)Update{
NSLog(@"----------sdafs");
}
- (IBAction) testValueSliderChanged:(UISlider *)sender {
testValueLabel.text = [NSString stringWithFormat:@"%.2f", sender.value];
}
// ------ Tutorial code ends here ------
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// This function is for button which takes you to the xprogress.com website
- (IBAction) runXprogressComButton: (id) sender {
NSURL *url = [ [ NSURL alloc ] initWithString: @"http://www.xprogress.com/" ];
[[UIApplication sharedApplication] openURL:url];
}
- (void)dealloc {
// ------ Tutorial code starts here ------
[threadValueLabel release];
[threadProgressView release];
[threadStartButton release];
[testValueLabel release];
// ------ Tutorial code ends here ------
[super dealloc];
}
@end
@implementation TutorialProjectViewController
@synthesize threadValueLabel, threadProgressView, testValueLabel, threadStartButton;
// ------ Tutorial code starts here ------
- (IBAction) startThreadButtonPressed:(UIButton *)sender {
threadStartButton.hidden = YES;
threadValueLabel.text = @"0";
threadProgressView.progress = 0.0;
[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];
}
- (void)startTheBackgroundJob {
//注册一个监听者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Update) name:@"UpdateTableView" object:nil];
NSLog(@"1111111111");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
[NSThread sleepForTimeInterval:3];
[self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void)makeMyProgressBarMoving {
xx++;
// NSLog(@"22222222 %i",xx);
float actual = [threadProgressView progress];
threadValueLabel.text = [NSString stringWithFormat:@"%.2f", actual];
if (actual < 1) {
threadProgressView.progress = actual + 0.01;
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
}
if (xx==5||xx==15) {
//发出一个消息。此前注册的监听者可以发现这个消息,并且出发相应的方法
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateTableView" object:nil];
NSLog(@"xxxxxxxxxxxxxx==3");
}
if (xx==10) {
//取消监听。
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UpdateTableView" object:nil];
}
else threadStartButton.hidden = NO;
}
//受到监听的消息时,调用这个函数
-(void)Update{
NSLog(@"----------sdafs");
}
- (IBAction) testValueSliderChanged:(UISlider *)sender {
testValueLabel.text = [NSString stringWithFormat:@"%.2f", sender.value];
}
// ------ Tutorial code ends here ------
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// This function is for button which takes you to the xprogress.com website
- (IBAction) runXprogressComButton: (id) sender {
NSURL *url = [ [ NSURL alloc ] initWithString: @"http://www.xprogress.com/" ];
[[UIApplication sharedApplication] openURL:url];
}
- (void)dealloc {
// ------ Tutorial code starts here ------
[threadValueLabel release];
[threadProgressView release];
[threadStartButton release];
[testValueLabel release];
// ------ Tutorial code ends here ------
[super dealloc];
}
@end
- ios线程监听模式
- ios线程监听者模式
- ios线程监听者模式
- IOS中的几中观察监听模式
- IOS中的几中观察监听模式
- iOS监听模式系列之通知中心
- iOS监听模式系列之IOS中的几中观察监听模式
- iOS监听模式系列之iOS开发证书、秘钥
- iOS监听模式系列之本地通知Notification
- iOS监听模式系列之NSNotificationCenter的简单使用
- iOS监听模式系列之推送消息通知
- iOS监听模式之KVO、KVC的高阶应用
- iOS监听模式系列之键值编码KVC、键值监听KVO的简单介绍和应用
- 线程状态监听
- 监听者模式
- 监听者模式
- KVO(键值监听模式)
- 监听机制Observers模式
- adb push 用法
- 使用VuGen开发Vuser脚本——lr掌握过程之3
- 第十六周 任务二
- UITextView控件的用法详解
- Cache应用
- ios线程监听模式
- UITextView实现自动隐藏键盘,自动隐藏键盘
- Couldn't register xxx.xxx.xxx with the bootstrap server
- 关于8583报文
- 小波分析基础
- P81Demo2 ASSUME DS:C1
- 二叉查找树的创建,插入,搜索,查询后继
- C++课程教学改革问卷调查
- 自定义UINavigationController的后退按钮标题