gpt4 book ai didi

swift - UIAlertView 没有出现

转载 作者:行者123 更新时间:2023-11-28 09:17:04 25 4
gpt4 key购买 nike

大家好,当应用程序启动时,我无法让 LoginAlert 出现……应用程序构建成功,但警报 View 没有出现。我错过了什么?

提前感谢您提供任何和所有见解!

//  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 成为一个类变量。 presentViewController 不保留对 alertController 的引用。因为它现在在一个局部变量中,它会在 viewDidAppear 结束时被删除。

关于swift - UIAlertView 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27083089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com