2016-02-13 6 views
0

Я пытаюсь создать UIAlertController с двумя вариантами «Отмена» и «Выход». Я хочу, чтобы кнопка «Отмена» отменила оповещение и кнопку «Выход», чтобы выполнить связанный с ней сеанс, который я установил в раскадровке.UIAlertController - Переход на другой сегмент

Мой код:

class HomeVC: UIViewController { 

@IBAction func SignOutBtn(sender: UIButton) { 

    let alertController = UIAlertController(title: "Alert", 
     message: "Are you sure you want to log out?", 
     preferredStyle: .Alert) 

    let cancelAction = UIAlertAction(title:"Cancel", 
     style: .Cancel) { (action) -> Void in 
      print("You selected the Cancel action.") 
    } 

    let submitAction = UIAlertAction(title:"Log out", 
     style: .Default) { (action) -> Void in 
      print("You selected the submit action.") 
      self.presentedViewController 
    } 

    alertController.addAction(submitAction) 
    alertController.addAction(cancelAction) 


    self.presentViewController(alertController, animated: true, completion: nil) 
} 

} 
+0

Так что вопрос ... –

+0

@Rohit КП я не в состоянии непосредственно перейти обратно к моему VC, если я выбираю «Выйти» – SwiftBeginner

ответ

0

Ну, похоже, вам не хватает действий, которые вы хотите выполнять внутри блоков. (также, вы можете отклонить оповещения контроллера, внутри блоков, а также.)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in 
    print("You selected the Cancel action.") 
    alertController.dismissViewControllerAnimated(true, completion: nil) 
}) 
let submitAction = UIAlertAction(title: "Log out", style: .Default, handler: { (action) -> Void in 
    print("You selected the submit action.") 
    alertController.dismissViewControllerAnimated(true, completion: {() -> Void in 
     // Perform your custom segue action you need. 
    }) 
}) 
+0

Спасибо. Как выполнять программные изменения? Я использовал ctrl +, перетащил его в VC на раскадровку, как мне это назвать? – SwiftBeginner

+0

Спасибо. Как выполнять программные изменения? Я использовал ctrl +, перетащил его в VC на раскадровку, как мне это назвать? - Я попытался добавить self.performSegueWithIdentifier («segue», отправитель: ноль), но какая бы кнопка я нажимал, он держит меня на том же VC – SwiftBeginner

+0

см. ниже @ pbush25. – Lirik

0

Если вы создали SEGUE от одного вида к другому, в обработчике кнопки для журнала из, то вы можете вызовите self.performSegueWithIdentifier("storyboadIdentifier"), который вызовет метод prepareForSegue, чтобы вы могли изменить или передать информацию по segue, если это необходимо.

+0

См. Мой ответ ниже для обновленного кода. Сеге по-прежнему, похоже, не называется. – SwiftBeginner

0

@ pbush25

class HomeVC: UIViewController { 

@IBAction func SignOutBtn(sender: UIButton) { 

    let alertController = UIAlertController(title: "Alert", 
     message: "Are you sure you want to log out?", 
     preferredStyle: .Alert) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in 
     print("You selected the Cancel action.") 
     alertController.dismissViewControllerAnimated(true, completion: nil) 

    }) 
    let submitAction = UIAlertAction(title: "Log out", style: .Default, handler: { (action) -> Void in 
     print("You selected the submit action.") 
     alertController.dismissViewControllerAnimated(true, completion: {() -> Void in 
      self.performSegueWithIdentifier("segue", sender: nil) 
     }) 
    }) 

    alertController.addAction(submitAction) 
    alertController.addAction(cancelAction) 


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


} 
Смежные вопросы