2016-07-29 70 views
0

我有一個關於swift中的UIAlertview的問題。我有一個警報視圖,允許用戶在「教師」和「學生」兩個選項中進行選擇,並根據點擊哪個按鈕將用戶帶到新屏幕。它工作正常,唯一的問題是我在每個新屏幕上都有一個後退按鈕,這會讓用戶返回到前一個屏幕,以防他們點擊錯誤的按鈕。這是問題發生的地方。如果用戶在naviagting之後單擊觸發alertview的按鈕回到頁面,則alertivew會記住用戶上次點擊哪個按鈕並基於該按鈕執行該按鈕。對不起,如果這是令人困惑的,我希望他們能夠在每次選擇之間進行選擇,並且alertview不根據他們以前的選擇執行。對不起,如果這是令人困惑的,所有的幫助是非常感激。swift中的UIAlertView/Xcode

這裏是我的代碼目前

@IBAction FUNC needToRegisterClicked(發件人:AnyObject){

let alertController = UIAlertController(title: "Please choose", message: "Teacher or Student?", preferredStyle: UIAlertControllerStyle.Alert) 

    alertController.view.tintColor = UIColor.blackColor() 

    alertController.addAction(UIAlertAction(title: "Teacher", style: .Default, handler: { (action) in 

     print("Teacher Chosen") 



     let ac = UIAlertController(title: "Enter Access Code", message: nil, preferredStyle: .Alert) 
     ac.addTextFieldWithConfigurationHandler { (textField: UITextField!) in 
      textField.keyboardType = UIKeyboardType.NumberPad } 



     ac.view.tintColor = UIColor.blackColor() 

     let submitAction = UIAlertAction(title: "Submit", style: .Default) { [unowned self, ac] (action: UIAlertAction!) in 
      let answer = ac.textFields![0] as! UITextField 
      if answer.text == "3280464" { 

       self.performSegueWithIdentifier("School Register", sender: nil) 

      } else { 

       let alert2 = UIAlertController(title: "Sorry!", message: "Incorrect access code", preferredStyle: UIAlertControllerStyle.Alert) 
       self.presentViewController(alert2, animated: true, completion: nil) 

       alert2.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil)) 


      } 
      // do something interesting with "answer" here 
     } 

     ac.addAction(submitAction) 

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






    })) 

    alertController.addAction(UIAlertAction(title: "Student", style: .Default, handler: { (action) in 

     print("Student Chosen") 

     self.performSegueWithIdentifier("Student", sender: nil) 


    })) 
    self.dismissViewControllerAnimated(true, completion: nil) 

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





} 

編輯:我究竟是如何去實例化一個新alertView每次按下按鈕?對不起,如果這是一個愚蠢的事情要問,我對整個事情是非常新的。

編輯:我嘗試了所有建議的選項,沒有運氣,但我現在才能夠自己弄清楚。我所需要做的就是從最後刪除self.dismissViewControllerAnimated(true,completion:nil)。感謝所有回覆的人,我衷心感謝您抽出時間。

+1

嘗試每個按鈕被按下,而不是使用同一個 –

+1

你可以發佈你的代碼時,實例化一個新的警報視圖,請 –

回答

0

我認爲你想要的是回到第一個ViewController,如果用戶在警報控制器中按錯誤的操作,所以我做的是: - 我添加了「學生」按鈕,其代碼與老師的按鈕 - 我創建了一個var,存儲一個布爾值,如果true爲老師,其他人是學生,當您選擇您是老師時,var會存儲一個真實值,否則,如果學生按鈕被按下,它存儲假,然後進入下一個視圖控制器 - 在第二的ViewController我創建了一個按鈕(回)

self.performSegueWithIdentifier("backToFirst", sender: self)

FirstViewController

import UIKit 

VAR isTeacher:BOOL!

類的ViewController:UIViewController的{

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

@IBAction func needToRegisterClicked(sender: AnyObject) { 

    let alertController = UIAlertController(title: "Please choose", message: "Teacher or Student?", preferredStyle: UIAlertControllerStyle.Alert) 

    alertController.view.tintColor = UIColor.blackColor() 

    //Button teacher 

    alertController.addAction(UIAlertAction(title: "Teacher", style: .Default, handler: { (action) in 

     print("Teacher Chosen") 

     isTeacher = true 

     let ac = UIAlertController(title: "Enter Access Code", message: nil, preferredStyle: .Alert) 
     ac.addTextFieldWithConfigurationHandler { (textField: UITextField!) in 
      textField.keyboardType = UIKeyboardType.NumberPad } 



     ac.view.tintColor = UIColor.blackColor() 

     let submitAction = UIAlertAction(title: "Submit", style: .Default) { [unowned self, ac] (action: UIAlertAction!) in 
      let answer = ac.textFields![0] as! UITextField 
      if answer.text == "3280464" { 

       self.performSegueWithIdentifier("School Register", sender: self) 

      } else { 

       let alert2 = UIAlertController(title: "Sorry!", message: "Incorrect access code", preferredStyle: UIAlertControllerStyle.Alert) 
       self.presentViewController(alert2, animated: true, completion: nil) 

       alert2.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil)) 


      } 
      // do something interesting with "answer" here 
     } 

     ac.addAction(submitAction) 

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

     } 
     //Ends Button Teacher 

     ) 


    ) 

    //Button Student 

    alertController.addAction(UIAlertAction(title: "Student", style: .Default, handler: { (action) in 

     print("Student Chosen") 

     isTeacher = false 

     let ac = UIAlertController(title: "Enter Access Code", message: nil, preferredStyle: .Alert) 
     ac.addTextFieldWithConfigurationHandler { (textField: UITextField!) in 
      textField.keyboardType = UIKeyboardType.NumberPad } 



     ac.view.tintColor = UIColor.blackColor() 

     let submitAction = UIAlertAction(title: "Submit", style: .Default) { [unowned self, ac] (action: UIAlertAction!) in 
      let answer = ac.textFields![0] as! UITextField 
      if answer.text == "3280464" { 

       self.performSegueWithIdentifier("School Register", sender: self) 

      } else { 

       let alert2 = UIAlertController(title: "Sorry!", message: "Incorrect access code", preferredStyle: UIAlertControllerStyle.Alert) 
       self.presentViewController(alert2, animated: true, completion: nil) 

       alert2.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil)) 


      } 
      // do something interesting with "answer" here 
     } 

     ac.addAction(submitAction) 

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

     } 


     ) 


    ) 

    //Ends Button Student 

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

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

}

SecondViewController

import UIKit 

類登錄:的ViewController {

@IBAction func Back(sender: AnyObject) { 

    self.performSegueWithIdentifier("backToFirst", sender: self) 

} 

@IBOutlet weak var navBar: UINavigationBar! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    if(isTeacher == true){ 

     navBar.topItem?.title = "Teacher Area" 

    }else{ 

     navBar.topItem?.title = "Student Area" 

    } 



} 

}

願望幫你