UIAlertAction에 대한 쓰기 처리기
제가 발표하는 것은UIAlertView어떻게 핸들러를 작성해야 할지 모르겠어요이것이 제 시도입니다.
let alert = UIAlertController(title: "Title",
message: "Message",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Okay",
style: UIAlertActionStyle.Default,
handler: {self in println("Foo")})
Xcode에 문제가 많이 있습니다.
문서에는 다음과 같이 나와 있습니다.convenience init(title title: String!, style style: UIAlertActionStyle, handler handler: ((UIAlertAction!) -> Void)!)
지금은 모든 블록/폐쇄가 제 머리 위에 있습니다.어떤 제안이든 대단히 감사합니다.
핸들러에 셀프 대신 (경고: UIAertAction!)을 넣습니다.이렇게 하면 코드가 다음과 같이 표시됩니다.
alert.addAction(UIAlertAction(title: "Okay",
style: UIAlertActionStyle.Default,
handler: {(alert: UIAlertAction!) in println("Foo")}))
이것이 Swift에서 핸들러를 정의하는 올바른 방법입니다.
Brian이 아래에서 지적했듯이 이러한 핸들러를 정의하는 더 쉬운 방법도 있습니다.그의 방법을 사용하는 것은 책에서 논의되고 있습니다, Closures라는 섹션을 보세요.
함수는 Swift의 1등급 객체입니다.따라서 폐쇄를 사용하지 않으려면 적절한 서명을 사용하여 함수를 정의한 다음 이를 다음과 같이 전달할 수도 있습니다.handler논쟁.관찰:
func someHandler(alert: UIAlertAction!) {
// Do something...
}
alert.addAction(UIAlertAction(title: "Okay",
style: UIAlertActionStyle.Default,
handler: someHandler))
swift 2를 사용하면 이와 같이 간단하게 수행할 수 있습니다.
let alertController = UIAlertController(title: "iOScreator", message:
"Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
self.pressed()
}))
func pressed()
{
print("you pressed")
}
**or**
let alertController = UIAlertController(title: "iOScreator", message:
"Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
print("pressed")
}))
위의 모든 대답은 정확합니다. 저는 단지 할 수 있는 다른 방법을 보여주고 있을 뿐입니다.
주 제목, 두 가지 작업(저장 및 삭제) 및 취소 버튼이 있는 UIAlertAction을 원한다고 가정합니다.
let actionSheetController = UIAlertController (title: "My Action Title", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
//Add Cancel-Action
actionSheetController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
//Add Save-Action
actionSheetController.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
print("handle Save action...")
}))
//Add Discard-Action
actionSheetController.addAction(UIAlertAction(title: "Discard", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
print("handle Discard action ...")
}))
//present actionSheetController
presentViewController(actionSheetController, animated: true, completion: nil)
이것은 swift 2(Xcode 버전 7.0 베타 3)에서 작동합니다.
Swift 4에서:
let alert=UIAlertController(title:"someAlert", message: "someMessage", preferredStyle:UIAlertControllerStyle.alert )
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: {
_ in print("FOO ")
}))
present(alert, animated: true, completion: nil)
swift 3.0의 구문 변경
alert.addAction(UIAlertAction(title: "Okay",
style: .default,
handler: { _ in print("Foo") } ))
이것이 내가 xcode 7.3.1로 하는 방법입니다.
// create function
func sayhi(){
print("hello")
}
단추 만들기
let sayinghi = UIAlertAction(title: "More", style: UIAlertActionStyle.Default, handler: { action in
self.sayhi()})
알림 컨트롤에 버튼 추가
myAlert.addAction(sayhi);
전체 코드, 이 코드는 2개의 버튼을 추가할 것입니다.
@IBAction func sayhi(sender: AnyObject) {
let myAlert = UIAlertController(title: "Alert", message:"sup", preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:nil)
let sayhi = UIAlertAction(title: "say hi", style: UIAlertActionStyle.Default, handler: { action in
self.sayhi()})
// this action can add to more button
myAlert.addAction(okAction);
myAlert.addAction(sayhi);
self.presentViewController(myAlert, animated: true, completion: nil)
}
func sayhi(){
// move to tabbarcontroller
print("hello")
}
경고 생성, xcode 9에서 테스트됨
let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: self.finishAlert))
self.present(alert, animated: true, completion: nil)
그리고 함수
func finishAlert(alert: UIAlertAction!)
{
}
인 스위프트
let alertController = UIAlertController(title:"Title", message: "Message", preferredStyle:.alert) let Action = UIAlertAction.init(title: "Ok", style: .default) { (UIAlertAction) in // Write Your code Here } alertController.addAction(Action) self.present(alertController, animated: true, completion: nil)목표 C에서
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *OK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:OK]; [self presentViewController:alertController animated:YES completion:nil];
Swift 5에서는 다음과 같이 할 수 있습니다.
let handler = { (action: UIAlertAction!) -> Void in
//do tasks
}
그런 다음 작업을 수행할 때 다음과 같이 대체합니다.
let action = UIAlertAction(title: "Do it", style: .default, handler: handler)
언급URL : https://stackoverflow.com/questions/24190277/writing-handler-for-uialertaction
'source' 카테고리의 다른 글
| 16진수 표현 문자열에서 토이() (0) | 2023.08.09 |
|---|---|
| 두 열의 최소값으로 정렬 (0) | 2023.08.09 |
| jQuery로 입력 필드를 비우는 방법 (0) | 2023.08.09 |
| TSLint 자동 수정 경고 (0) | 2023.08.09 |
| Oracle regexp의 불균형한 '^' 및 '$' 앵커 (0) | 2023.08.09 |