2014-11-22 373 views
1

大家好我有一個LoginAlert出現應用程序啓動時出現問題......應用程序成功建立,但沒有出現警報視圖。我錯過了什麼?UIAlertView沒有出現

非常感謝您的寶貴意見!

// TimelineTableViewController.swift 


import UIKit 

class TimelineTableViewController: UITableViewController { 


    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 


    override func viewDidAppear(animated: Bool) { 
     if ((PFUser.currentUser()) != nil){ 
      var loginAlert:UIAlertController = UIAlertController(title: "Sign Up/Login",message: "Please Sign up or Login", 
       preferredStyle: UIAlertControllerStyle.Alert) 

      loginAlert.addTextFieldWithConfigurationHandler({ 
       textfield in 
       textfield.placeholder = "Your username" 
      }) 

      loginAlert.addTextFieldWithConfigurationHandler({ 
       textfield in 
       textfield.placeholder = "Your password" 
       textfield.secureTextEntry = true 
      }) 

      loginAlert.addAction(UIAlertAction(title: "Login", style: UIAlertActionStyle.Default, handler:{ 
       alertAction in 
       let textFields:NSArray = loginAlert.textFields! as NSArray 
       let usernameTextfield:UITextField = textFields.objectAtIndex(0) as UITextField 
       let passwordTextfield:UITextField = textFields.objectAtIndex(1) as UITextField 

       PFUser.logInWithUsernameInBackground(usernameTextfield.text, password: passwordTextfield.text){ 
        (user:PFUser!, error:NSError!)->Void in 
        if ((user) != nil){ 
         println("Login successful") 
        }else{ 
         println("Login Failed") 
        } 

       } 

      })) 

      loginAlert.addAction(UIAlertAction(title: "Sign Up", style: UIAlertActionStyle.Default, handler:{ 
       alertAction in 
       let textFields:NSArray = loginAlert.textFields! as NSArray 
       let usernameTextfield:UITextField = textFields.objectAtIndex(0) as UITextField 
       let passwordTextfield:UITextField = textFields.objectAtIndex(1) as UITextField 

       var poster:PFUser = PFUser() 
       poster.username = usernameTextfield.text 
       poster.password = passwordTextfield.text 

       poster.signUpInBackgroundWithBlock{ 
        (success:Bool!, error:NSError!)->Void in 
        if !(error != nil){ 
         println("Sign Up Successful") 
        }else{ 
         let errorString = error.userInfo!["error"] as String 
         println(errorString) 
        } 
       } 

      })) 

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

     } 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    } 

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

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Potentially incomplete method implementation. 
     // Return the number of sections. 
     return 0 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete method implementation. 
     // Return the number of rows in the section. 
     return 0 
    } 

回答

0

使loginAlert類變量。 presentViewController不保留對alertController的引用。因爲它現在在局部變量中,所以它將在viewDidAppear結尾處被刪除。

+0

我怎麼會給loginAlert一類? (對不起,我有點初學者...) – user3708224 2014-12-06 21:32:32

+0

這不是一個問題。當你呈現ViewController的時候,它的retainCount會增加,所以它的局部變量沒有關係。 – iMemon 2014-12-08 12:01:06

0

您的警告視圖代碼正常工作。我想問題是你的病情if ((PFUser.currentUser()) != nil)。在viewDidAppear(animated: Bool)方法中添加書籤以調試問題。

以下代碼我已經過測試,您可以通過創建一個新樣本來檢查它。

override func viewDidAppear(animated: Bool) { 
    var loginAlert:UIAlertController = UIAlertController(title: "Sign Up/Login",message: "Please Sign up or Login", 
     preferredStyle: UIAlertControllerStyle.Alert) 

    loginAlert.addTextFieldWithConfigurationHandler({ 
     textfield in 
     textfield.placeholder = "Your username" 
    }) 

    loginAlert.addTextFieldWithConfigurationHandler({ 
     textfield in 
     textfield.placeholder = "Your password" 
     textfield.secureTextEntry = true 
    }) 

    loginAlert.addAction(UIAlertAction(title: "Login", style: UIAlertActionStyle.Default, handler:{ 
     alertAction in 
     let textFields:NSArray = loginAlert.textFields! as NSArray 
     let usernameTextfield:UITextField = textFields.objectAtIndex(0) as UITextField 
     let passwordTextfield:UITextField = textFields.objectAtIndex(1) as UITextField 
     print("Login with Username: \(usernameTextfield.text) -- Password: \(passwordTextfield.text)") 
    })) 

    loginAlert.addAction(UIAlertAction(title: "Sign Up", style: UIAlertActionStyle.Default, handler:{ 
     alertAction in 
     let textFields:NSArray = loginAlert.textFields! as NSArray 
     let usernameTextfield:UITextField = textFields.objectAtIndex(0) as UITextField 
     let passwordTextfield:UITextField = textFields.objectAtIndex(1) as UITextField 
     print("Signup with Username: \(usernameTextfield.text) -- Password: \(passwordTextfield.text)") 
    })) 

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

Screeshot of the output in iOS8/iPhone6 Simulator

好運:)