Monday, September 10, 2012

Lập trình iPhone - Đối tượng UIAlertView - Hiển thị hộp thông báo và nhập liệu.

UIAlertView

I – Giới thiệu:

Dùng class UIAlertView để hiển thị nội dung thông báo cho người dùng, hoặc dùng để cho khách hàng nhập liệu



II – Cấu trúc:

A – Khởi tạo đối tượng UIAlertView

– initWithTitle:message:delegate:cancelButtonTitle:otherButton Titles:

B – Gán thuộc tính cho UIAlertView

• delegate property
• alertViewStyle property
• title property
• message property
• visible property

C – Cấu hình các button bên trong UIAlertView

• – addButtonWithTitle:
• numberOfButtons property
• – buttonTitleAtIndex:
• – textFieldAtIndex:
• cancelButtonIndex property
• firstOtherButtonIndex property

D – Gọi 1 UIAlertView

– show

D – Hủy 1 UIAlertView

– dismissWithClickedButtonIndex:animated:

E – Thuộc tính của UIAlertView

alertViewStyle : Kiểu của 1 alert view khi hiển thị trên ứng dụng
cancelButtonIndex: Vị trí của nút Cancel trên UIAlertView
delegate: Đối tượng nhận giá trị từ UIAlertView, thường có giá trị là nil
firstOtherButtonIndex: Giá trị của nút đầu tiên bên trong UIAlertView. Giá trị này mặc định là 0
numberOfButtons: Tổng số lượng button bên trong 1 UIAlertView
title: Tiêu đề của UIAlertView, mỗi view có 1 title duy nhất
visible: Nếu có giá trị là YES, View sẽ được hiển thị cho người dùng thấy, ngược lại là NO


F – Các hàm hỗ trợ


addButtonWithTitle: Thêm button vào trong View với title cho trước. Hàm này trả về mã giá trị của nút vừa thêm
buttonTitleAtIndex: Trả về title của 1 button trong AlertView
dismissWithClickedButtonIndex: Vô hiệu hóa 1 button trong AlertView
initWithTitle:message:delegate:cancelButtonTitle:otherButton Titles: Khởi tạo 1 đối tượng UIAlertView
textFieldAtIndex: Truy cập đến 1 textfied nào đó bên trong UIAlertView. Thường dùng để lấy giá trị khách hàng nhập vào.

G – Các kiểu UIAlertView

1 - UIAlertViewStylePlainTextInput


2 – UIAlertViewStyleSecureTextInput



3 – UIAlertViewStyleLoginAndPasswordInput


4 – UIAlertViewStyleDefault


III - Cách sử dụng:

B1: Khai báo trong file .h

PHP Code:
UIAlertView *thongbao3

B2: Khai báo code gọi UIAlertView trong file .m

PHP Code:
thongbao3 = [[UIAlertView alloc]

    
initWithTitle:@"TRUNG TÂM ĐÀO TẠO MẠNG MÁY TÍNH NHẤT NGHỆ" 
    
    
message:@"Đăng nhập kho tài liệu của Nhất Nghệ" 

    
delegate:self
 
    cancelButtonTitle
:@"Thoát"
 
    
otherButtonTitles:@"Đăng nhập"nil
];
thongbao3.alertViewStyle UIAlertViewStyleLoginAndPasswordInput;    

[
thongbao3 show]; 
B3: Viết code truy cập giá trị bên trong các UIAlertView

PHP Code:
-(void)alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSIntegerbuttonIndex {

    if( 
alertView == thongbao3 ){
        if(
buttonIndex==1){
            
NSString *un = [[alertView textFieldAtIndex:0text];
            
NSString *pa = [[alertView textFieldAtIndex:1text];
            
NSLog(@"Username:%@",un);
             
NSLog(@"Password":%@",pa ); 
        }
        
        }

No comments:

Post a Comment