大家好我无法在启动应用程序时显示LoginAlert ...应用程序已成功构建但警报视图未显示。我错过了什么?
提前感谢您提供任何见解!
// 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 :(得分:0)
使loginAlert成为一个类变量。 presentViewController不保留对alertController的引用。因为它现在位于局部变量中,所以它将在viewDidAppear的末尾删除。
答案 1 :(得分: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)
}
祝你好运:)