programing

단순 UIA 경고 보기 추가

closeapi 2023. 5. 31. 15:56
반응형

단순 UIA 경고 보기 추가

"확인" 버튼이 하나 있는 간단한 UIA 경고 보기를 만드는 데 사용할 수 있는 스타터 코드는 무엇입니까?

알림을 표시하려면 다음을 수행합니다.

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ROFL" 
                                                    message:@"Dee dee doo doo." 
                                                    delegate:self 
                                                    cancelButtonTitle:@"OK" 
                                                    otherButtonTitles:nil];
[alert show];

    // If you're not using ARC, you will need to release the alert view.
    // [alert release];

단추를 클릭할 때 작업을 수행하려면 다음 위임 방법을 구현합니다.

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    // the user clicked OK
    if (buttonIndex == 0) {
        // do something here...
    }
}

그리고 당신의 대리인이 다음을 준수하는지 확인하십시오.UIAlertViewDelegate프로토콜:

@interface YourViewController : UIViewController <UIAlertViewDelegate> 

다른 답변은 이미 iOS 7 이상 버전에 대한 정보를 제공하지만 iOS 8에서는 더 이상 사용하지 않습니다.

iOS 8+에서 사용해야 합니다.UIAlertController이는 두 가지를 모두 대체하는 것입니다.UIAlertView그리고.UIActionSheet설명서:UIAlertController 클래스 참조입니다.그리고 NSHipster에 대한 좋은 기사.

간단한 알림 보기를 생성하려면 다음을 수행합니다.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                         message:@"Message"
                                                                  preferredStyle:UIAlertControllerStyleAlert];
//We add buttons to the alert controller by creating UIAlertActions:
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                   style:UIAlertActionStyleDefault
                                                 handler:nil]; //You can use a block here to handle a press on this button
[alertController addAction:actionOk];
[self presentViewController:alertController animated:YES completion:nil];

스위프트 3 / 4 / 5:

let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
//We add buttons to the alert controller by creating UIAlertActions:
let actionOk = UIAlertAction(title: "OK",
    style: .default,
    handler: nil) //You can use a block here to handle a press on this button

alertController.addAction(actionOk)

self.present(alertController, animated: true, completion: nil)

참고로 iOS 8에 추가되었기 때문에 이 코드는 iOS 7 이상에서는 작동하지 않습니다.그래서 안타깝게도 지금은 다음과 같은 버전 검사를 사용해야 합니다.

NSString *alertTitle = @"Title";
NSString *alertMessage = @"Message";
NSString *alertOkButtonText = @"Ok";

if (@available(iOS 8, *)) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
                                                        message:alertMessage
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:alertOkButtonText, nil];
    [alertView show];
}
else {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
                                                                             message:alertMessage
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    //We add buttons to the alert controller by creating UIAlertActions:
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:alertOkButtonText
                                                       style:UIAlertActionStyleDefault
                                                     handler:nil]; //You can use a block here to handle a press on this button
    [alertController addAction:actionOk];
    [self presentViewController:alertController animated:YES completion:nil];
}

스위프트 3 / 4 / 5:

let alertTitle = "Title"
let alertMessage = "Message"
let alertOkButtonText = "Ok"

if #available(iOS 8, *) {
    let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
    //We add buttons to the alert controller by creating UIAlertActions:
    let actionOk = UIAlertAction(title: alertOkButtonText,
        style: .default,
        handler: nil) //You can use a block here to handle a press on this button

    alertController.addAction(actionOk)
    self.present(alertController, animated: true, completion: nil)
}
else {
    let alertView = UIAlertView(title: alertTitle, message: alertMessage, delegate: nil, cancelButtonTitle: nil, otherButtonTitles: alertOkButtonText)
    alertView.show()
}

UPD: Swift 5에 대해 업데이트되었습니다.오래된 클래스 존재 검사를 Obj-C의 가용성 검사로 대체했습니다.

UIAlertView *myAlert = [[UIAlertView alloc] 
                         initWithTitle:@"Title"
                         message:@"Message"
                         delegate:self
                         cancelButtonTitle:@"Cancel"
                         otherButtonTitles:@"Ok",nil];
[myAlert show];

UIAlertView는 iOS 8에서 더 이상 사용되지 않습니다.따라서 iOS 8 이상에서 경고를 생성하려면 UIAertController를 사용할 것을 권장합니다.

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

    // Enter code here
}];
[alert addAction:defaultAction];

// Present action where needed
[self presentViewController:alert animated:YES completion:nil];

이것이 제가 그것을 실행한 방법입니다.

UIAlertView *alert = [[UIAlertView alloc]
 initWithTitle:@"Title" 
 message:@"Message" 
 delegate:nil //or self
 cancelButtonTitle:@"OK"
 otherButtonTitles:nil];

 [alert show];
 [alert autorelease];

이전의 두 가지 답변(사용자 "sudm-rf" 및 "Evan Mulawski")에 대한 보충으로 경고 보기를 클릭했을 때 아무것도 하지 않으려면 해당 보기를 할당하고 표시 및 해제하면 됩니다.위임 프로토콜을 선언할 필요는 없습니다.

다음은 UI 알림을 닫을 수 있는 '확인' 버튼이 하나만 있는 완전한 방법입니다.

- (void) myAlert: (NSString*)errorMessage
{
    UIAlertView *myAlert = [[UIAlertView alloc]
                          initWithTitle:errorMessage
                          message:@""
                          delegate:self
                          cancelButtonTitle:nil
                          otherButtonTitles:@"ok", nil];
    myAlert.cancelButtonIndex = -1;
    [myAlert setTag:1000];
    [myAlert show];
}

이 페이지는 Swift를 사용하는 경우 UIAert 컨트롤러를 추가하는 방법을 보여줍니다.

어레이 데이터를 사용한 간단한 경고:

NSString *name = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"Name"];

NSString *msg = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"message"];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:name
                                                message:msg
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];

언급URL : https://stackoverflow.com/questions/4463806/adding-a-simple-uialertview

반응형