ios线程监听模式

#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值