2014-11-22 2 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

+0

이것은 문제가되지 않습니다. presentViewController의 retainCount가 증가 할 것이므로 로컬 변수와 상관 없습니다. – iMemon

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

행운을 빕니다 :)

관련 문제