2010-08-16 28 views
22

Tôi đang cố gọi và cảnh báo khi nhấn một nút. tôi sử dụng điều này:Phát hiện nút bấm bằng UIAlertView

-(IBAction)Add { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"add button pressed" 
                message:@"Add to record" 
                delegate:nil  
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil ]; 
    [alert show]; 
    [alert release];  
} 

ok, không có vấn đề ở đây, hai nút xuất hiện, OK và hủy. Bây giờ tôi muốn phát hiện nút nào được nhấn tôi sử dụng:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    // the user clicked one of the OK/Cancel buttons 
    if (buttonIndex == 0) 
    { 
     //just to show its working, i call another alert view 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well" 
                 message:@"no error"                           delegate:nil 
               cancelButtonTitle:@"IWORKS" 
               otherButtonTitles:@"NO PRB", nil]; 
     [alert show]; 
     [alert release];  


    } 
    else 
    { 
     NSLog(@"cancel");  
    } 
} 

giờ đây là vấn đề. tôi không thể phát hiện nút nào được nhấn; cảnh báo thứ 2 không hiển thị. Tôi đã kiểm tra thông qua mã một vài lần, có vẻ như không có bất kỳ vấn đề với nó. cũng không có lỗi/cảnh báo.

+2

Hiển thị cảnh báo trực tiếp sau một cảnh báo khác có lẽ không phải là một ý tưởng hay - chỉ là một mẹo nhỏ. – vakio

Trả lời

32

Để phát hiện nút nhấp, chế độ xem cảnh báo phải có đại biểu được liên kết, ví dụ:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add button pressed" 
               message:@"Add to record" 
               delegate:self // <------ 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK", nil]; 
+2

+1: Những gì tôi đã được nửa chừng bằng văn bản. Tôi cũng đề cập rằng bộ điều khiển xem cần thực hiện 'UIAlertViewDelegate' (mặc dù tôi tin rằng nó đưa ra một cảnh báo nếu nó không.) – sdolan

7

NútIndex là 0 là nút hủy. Tôi muốn giới thiệu cách sử dụng:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
if (buttonIndex == 0) 
{ 
     NSLog(@"cancel"); 
} 
else 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];                            
    [alert show]; 
    [alert release];   
} 
} 
+2

Hoặc tốt hơn,' if (buttonIndex == alertView.cancelButtonIndex) ... ' – mojuba

1

Nếu bạn thích mã của bạn được sạch hơn và không phụ thuộc vào đại biểu, bạn nên cố gắng thực hiện các khối UIAlertView:

https://github.com/steipete/PSAlertView

Blocks chỉ được hỗ trợ trên iOS 4 thiết bị mặc dù.

13

Đây là mã của bạn mà tôi đã sử dụng và thêm một số mã của tôi. **

-(IBAction) Add 
{ 

      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"add button pressed" 

                 message:@"Add to record" 
                 delegate:nil  
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil]; 

     alert.tag=101;//add tag to alert 
     [alert show]; 
     [alert release];  
} 

Bây giờ khi bạn nhấn nút tình trạng báo động, nó sẽ gọi clickedButtonAtIndex nhưng nên có một định danh cho mỗi cảnh báo. Vì vậy, thêm thẻ và sau đó thêm

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex** 

{ 

// the user clicked one of the OK/Cancel buttons 
if(alert.tag=101)  // check alert by tag 
{  

if (buttonIndex == 0) 

    { 

    //just to show its working, i call another alert view 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well" 

                message:@"no error" 
                delegate:nil 
              cancelButtonTitle:@"IWORKS" 
              otherButtonTitles:@"NO PRB", nil]; 

    [alert show]; 
    [alert release];  

} 

else 

{ 
    NSLog(@"cancel");  
} 
} 
} 

Hy vọng điều đó sẽ hữu ích.

2

tôi cảm thấy nếu bạn muốn hiển thị một cái nhìn cảnh báo mới trên nút nhấp chuột trường hợp một cái nhìn cảnh báo hiện tại, nó sẽ là tốt hơn để sử dụng

- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 

} 

phương pháp đại biểu thay vì

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

} 
2
1) 
.h file 
@interface MyClassViewController:<UIAlertViewDelegate> 

2) 
.m file 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" 
               message:@"some message" 
               delegate:self // must be self to call clickedButtonAtIndex 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK", nil]; 


3) 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == [alertView cancelButtonIndex]) { 
    NSLog(@"The cancel button was clicked from alertView"); 
    } 
else { 

} 
} 
Các vấn đề liên quan