간단한 UIAlertView 추가
하나의 "OK"버튼이있는 간단한 UIAlertView를 만드는 데 사용할 수있는 시작 코드는 무엇입니까?
경고를 표시하려면 다음을 수행하십시오.
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 및 이전 버전에 대한 정보를 제공하지만 UIAlertView
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는 iOS 8에서 더 이상 사용되지 않습니다. 따라서 iOS 8 이상에서 경고를 생성하려면 UIAlertController를 사용하는 것이 좋습니다.
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];
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok",nil];
[myAlert show];
두 개의 이전 답변 (사용자 "sudo rm -rf"및 "Evan Mulawski")에 대한 보충으로, 경고보기를 클릭했을 때 아무것도하지 않으려면 할당, 표시 및 해제 만하면됩니다. 위임 프로토콜을 선언 할 필요가 없습니다.
다음은 UIAlert를 닫는 '확인'버튼 하나만있는 완전한 메소드입니다.
- (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];
}
This page shows how to add an UIAlertController if you are using Swift.
Simple alert with array data:
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];
For Swift 3:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
참고URL : https://stackoverflow.com/questions/4463806/adding-a-simple-uialertview
'Programing' 카테고리의 다른 글
다차원 배열의 모든 열 값을 합하는 방법은 무엇입니까? (0) | 2020.08.12 |
---|---|
문자열에서 가장 오른쪽에있는 n 개의 문자 만 추출 (0) | 2020.08.12 |
1D numpy 배열에서 Numpy를 사용하여 로컬 최대 값 / 최소값 찾기 (0) | 2020.08.12 |
pip 설치 후 virtualenvwrapper.sh는 어디에 있습니까? (0) | 2020.08.12 |
C #, .NET에서 사운드를 재생하는 방법 (0) | 2020.08.12 |