Article: Hiển thị trạng thái của một tác vụ trên iphone 816

me.yahoo.com/a/L6mfLnl7ke3ouPpR7TsuvOiQPC4- 1
Updated about 1 year ago

Trong trường hợp viết 1 chương trình ứng dụng cho IPhone, chúng ta muốn hiển thị trạng thái hiện tại của một tác vụ đang xảy ra của chương trình ví dụ như thông báo chương trình đang loading, getting...

bài viết này nhằm trình bày 2 vấn đề sau:

-Cập nhật Interface trong khi chương trình đang xử lý một tác vụ.
-Hiển thị message box hiển thị trạng thái của tác vụ đang xử lý.

 


Sau đây là đoạn code chi tiết của chương trình:
1.Khi main thread đang handle sự kiện nào đó, ví dụ ở đây là sự kiện click button, chương trình sẽ tạo ra một thread mới, thread này có nhiệm vụ cập nhật Interface
- (IBAction)buttonPress:(id)sender {
    [NSThread detachNewThreadSelector:@selector(startThread:) toTarget:self withObject:@"printing..."];
    //do some thing
}

2.Trong hàm startThread, sẽ hiện thực việc hiển thị message thông báo trạng thái chương trình:

- (void)startThread:(NSString*)msg {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //self.view.userInteractionEnabled = NO;
    if(self.userDisableView == nil) {
        CGRect viewBounds = [ [UIScreen mainScreen ] applicationFrame];
        viewBounds.origin.y = 0;
        UIView *myView = [[UIView alloc] initWithFrame:viewBounds];
        myView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
       
        self.labelReport = [[UILabel alloc] initWithFrame:CGRectMake(50.0f,10.0f, 100.0f, 36.0f)];
        self.labelReport.textAlignment = UITextAlignmentCenter ;
        self.labelReport.backgroundColor = [UIColor clearColor];
        self.labelReport.textColor = [UIColor whiteColor ];
        [myView addSubview:self.labelReport];
       
        NSArray *images = [NSArray arrayWithObjects:
                           [UIImage imageNamed:@"ajax-loader_31835_001.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_002.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_003.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_004.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_005.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_006.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_007.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_008.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_009.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_0010.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_0011.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_0012.gif"],
                           nil];
       
        UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(87.0, 42.0, 25.0, 25.0)];
       
        [imageView setAnimationImages:images];
        [imageView setAnimationRepeatCount:0];
        imageView.animationDuration  = 2;
       
        self.loadingImage = imageView;
        [imageView release];
        [self.loadingImage startAnimating];
       
       
        UIButton *myButton = [ UIButton buttonWithType: UIButtonTypeCustom ];
        myButton.frame = CGRectMake(50.0, 200.0, 200.0, 80.0);
        myButton.backgroundColor = [UIColor clearColor];
        [myButton setBackgroundImage:[UIImage imageNamed:@"Placard2.png"] forState:UIControlStateNormal];

       
        [myButton addSubview:self.loadingImage];
        [myButton addSubview:self.labelReport];
               
       
       
        self.btnLoading = myButton;
       
        [myButton release];
        [myView addSubview:self.btnLoading];
       
        self.userDisableView = myView;
       
        [myView release];
       
    }
    
    self.labelReport.text = msg;
    [self.view addSubview:self.userDisableView];
    [self.view bringSubviewToFront:self.userDisableView];
    
    [pool release];
}

Trong chương trình Iphone, cấu trúc của hàm cho một thread như sau:
function xxx: mn {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //do something
    [pool release];    
}
->giải phóng vùng nhớ sau khi kết thúc thread(kết thúc hàm).
Tạo message thông báo như sau:
    -Tạo một view để hiện thị lên phía trên view chính:
        CGRect viewBounds = [ [UIScreen mainScreen ] applicationFrame];
        viewBounds.origin.y = 0;
        UIView *myView = [[UIView alloc] initWithFrame:viewBounds];
        myView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    -Tạo một label hiển thị message:
        self.labelReport = [[UILabel alloc] initWithFrame:CGRectMake(50.0f,10.0f, 100.0f, 36.0f)];
        self.labelReport.textAlignment = UITextAlignmentCenter ;
        self.labelReport.backgroundColor = [UIColor clearColor];
        self.labelReport.textColor = [UIColor whiteColor ];
        [myView addSubview:self.labelReport];
    -Tạo một hình animation nào đó để thống báo rằng trái đất đang quay hay máy tính mình chưa ngẻo chẳng hạn:
        NSArray *images = [NSArray arrayWithObjects:
                           [UIImage imageNamed:@"ajax-loader_31835_001.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_002.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_003.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_004.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_005.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_006.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_007.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_008.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_009.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_0010.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_0011.gif"],
                           [UIImage imageNamed:@"ajax-loader_31835_0012.gif"],
                           nil];
       
        UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(87.0, 42.0, 25.0, 25.0)];
       
        [imageView setAnimationImages:images];
        [imageView setAnimationRepeatCount:0];
        imageView.animationDuration  = 2;
       
        self.loadingImage = imageView;
        [imageView release];
        [self.loadingImage startAnimating];
    -Tạo một button có viền xung quang, và background trong suốt(transperency) có thể nhìn xuyện qua:
        UIButton *myButton = [ UIButton buttonWithType: UIButtonTypeCustom ];
        myButton.frame = CGRectMake(50.0, 200.0, 200.0, 80.0);
        myButton.backgroundColor = [UIColor clearColor];
        [myButton setBackgroundImage:[UIImage imageNamed:@"Placard2.png"] forState:UIControlStateNormal];
       
    -Công việc cuối cùng add label message, và hình animation lên button sau đó cho button lên view:
        [myButton addSubview:self.loadingImage];
        [myButton addSubview:self.labelReport];
       
        self.btnLoading = myButton;
       
        [myButton release];
        [myView addSubview:self.btnLoading];

Comments

You must login to be able to comment

Uploaded files

You must login to be able to upload

Nhà tài trợ:

Mọi người đều tự do viết bài, sửa bài của người khác, và bình luận ở trang web này. Bạn muốn chủ động tạo bài mới để chia sẻ kinh nghiệm với mọi người? Xin click link ở dưới.

Create new content