您的位置:首页 > 移动开发 > IOS开发

ios线程监听者模式

2013-10-30 09:58 381 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: