大家好我有一個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
}
我怎麼會給loginAlert一類? (對不起,我有點初學者...) – user3708224 2014-12-06 21:32:32
這不是一個問題。當你呈現ViewController的時候,它的retainCount會增加,所以它的局部變量沒有關係。 – iMemon 2014-12-08 12:01:06