ios线程监听者模式
来源:互联网 发布:知乎帐号 购买 编辑:程序博客网 时间:2024/06/07 23:42
#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开发证书、秘钥
- java中的监听者模式
- 监听者—观察者模式
- 监听者模式理解(Swing)
- 设计模式之监听者模式
- 设计模式(Javaee)之---监听者模式
- iOS监听模式系列之本地通知Notification
- 六大Nagios常见问题解决办法
- 项目经理该干什么?
- 从零开始学WCF(14)WCF安全性概述
- 华为2014校园招聘的机试题目
- JBoss配置
- ios线程监听者模式
- CListView常用用法
- WP8之读取SD卡
- uvc摄像头代码解析2
- meteor使用外置mongodb
- 逻辑人渴望控制那些让他们感兴趣的东西
- 为程序员量身定制的12个目标
- tomcat解压war包的一点例外
- String—常见功能