gpt4 book ai didi

swift - 如何在自定义类中使用presentViewController?

转载 作者:行者123 更新时间:2023-11-30 13:45:40 25 4
gpt4 key购买 nike

我刚刚更新了我的代码,但alertWindow不会显示。我在 Xcode 中添加断点,它显示 viewDelegate 为零。所有委托(delegate)均为零。这是我的代码:

class LoginViewController:UIViewController,AlertDelegate
{
override func viewDidLoad() {
super.viewDidLoad()

let BackGroundImage:UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width , self.view.frame.height))

let image: UIImage = UIImage(named: "backgroundLogin.jpg")!

BackGroundImage.image = image
self.view.addSubview(BackGroundImage)
//self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundLogin.jpg")!)

username.text = "User name"
password.text = "Password"
usernameWarning.text = ""
passwordWarning.text = ""

self.view.bringSubviewToFront(username)
self.view.bringSubviewToFront(password)
self.view.bringSubviewToFront(usernameText)
self.view.bringSubviewToFront(passwordText)
self.view.bringSubviewToFront(LoginButton)
self.view.bringSubviewToFront(RegisterButton)

self.view.bringSubviewToFront(usernameWarning)
self.view.bringSubviewToFront(passwordWarning)


}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.identifier
{


case .Some("RegisterSegue"):
let registerViewController = segue.destinationViewController as! RegisterViewController

if let temp_register_username = usernameText!.text
{
registerViewController.register_name = temp_register_username
if let temp_register_pass = passwordText!.text
{
registerViewController.register_password = temp_register_pass
}
}
case .Some("loginSegue"):
let loginViewController = segue.destinationViewController as! FunctionsViewController
loginViewController.client = client



default:
super.prepareForSegue(segue, sender: sender)



}
}


override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == "loginSegue"
{
if usernameText.text != ""
{
if passwordText.text != ""
{


let encrypt = encryptMsg(send: passwordText.text!)

let encrypt_pass = encrypt.encryptSendMsg()

let sendMsg = AppConfig.AddHead("login", body: usernameText.text!+"|"+encrypt_pass)
let (_,_) = client.connect(timeout: 1)
let (success,_) = client.send(str: sendMsg)
if success
{
let data = client.read(1024*10)
if let d = data
{
if let str = String(bytes: d, encoding: NSUTF8StringEncoding)
{
if str[0...2] == AppConfig.TransmissionAgreementConfiguration["successLogin"]
{
AppConfig.StoreUserID(str)

let defaults = NSUserDefaults()

defaults.setObject(usernameText.text!, forKey: "userName")

let acceptThread = AcceptThread()
acceptThread.start()

return true
}
else if str[0...2] == AppConfig.TransmissionAgreementConfiguration["errLoginUsernameNotExist"]
{
usernameWarning.text = "User name doesn't exist"
usernameWarning.reloadInputViews()

passwordWarning.text = ""
passwordWarning.reloadInputViews()
return false

}
else if str[0...2] == AppConfig.TransmissionAgreementConfiguration["errLoginUsernameAlreadyOnline"]
{
usernameWarning.text = "User account already be online"
usernameWarning.reloadInputViews()

passwordWarning.text = ""
passwordWarning.reloadInputViews()
return false
}
else if str[0...2] == AppConfig.TransmissionAgreementConfiguration["errLoginPassword"]
{
usernameWarning.text = ""
usernameWarning.reloadInputViews()

passwordWarning.text = "Password Incorrect"
passwordWarning.reloadInputViews()
return false
}
}
}
}

return false

}
else
{
usernameWarning.text = ""
usernameWarning.reloadInputViews()

passwordWarning.text = "password cannot be empty"
passwordWarning.reloadInputViews()
return false
}

}
else
{
usernameWarning.text = "username cannot be empty"
usernameWarning.reloadInputViews()
return false
}
}
else
{
return true
}

}

func presentWindow(title:String,msg:String)
{

let alertController = UIAlertController(title: "\(title):", message: "\(msg)", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler{
(textField: UITextField!) -> Void in
textField.placeholder = "Message"
}

let cancelAction = UIAlertAction(title: "Ignore", style: UIAlertActionStyle.Cancel, handler: nil)

let okAction = UIAlertAction(title: "Reply", style: UIAlertActionStyle.Default,
handler: {
action in
let reply_msg = alertController.textFields!.first! as UITextField
print("I replies \(reply_msg)")

})
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)

}
@IBAction func logoutUnwindSegue(segue: UIStoryboardSegue) {
// Intentionally left blank
}

var client:TCPClient = TCPClient(addr:"localhost",port:8889)


var AppConfig=TransmissionAgreement(first:"test")
@IBOutlet weak var usernameWarning: UILabel!

@IBOutlet weak var passwordWarning: UILabel!

@IBOutlet weak var username: UILabel!
@IBOutlet weak var password: UILabel!

@IBOutlet weak var usernameText: UITextField!
@IBOutlet weak var passwordText: UITextField!

@IBOutlet weak var LoginButton: UIButton!

@IBOutlet weak var RegisterButton: UIButton!
}

class AcceptThread: NSThread {
override func main() {
let server:TCPServer = TCPServer(addr:"127.0.0.1",port: 7000)

let (success,_) = server.listen()
if success
{

while !cancelled
{
if let my_client = server.accept()
{

let clientReadThread = ClientReadThread(client: my_client)
clientReadThread.start()
clientThreads.append(clientReadThread)
}
}

for clientThread in clientThreads {
clientThread.cancel()
}
}
}

var clientThreads = Array<ClientReadThread>()
var AppConfig=TransmissionAgreement(first:"test")
}

@objc protocol AlertDelegate: class {
func presentWindow(title:String,msg:String)
}

@objc protocol ClientReadThreadDelegate: class {
func clientReadThread(clientReadThread: ClientReadThread, didReceiveMessage message: String)

}


class ClientReadThread:NSThread{
init(client: TCPClient) {
self.client = client

super.init()
}




override func main() {
while !cancelled, let readValue = client.read(1024*10) {
if let message = String(bytes: readValue, encoding: NSUTF8StringEncoding) {

let head = message[0...2]
// print("head is \(head)")


if head! == AppConfig.TransmissionAgreementConfiguration["add"]
{
let defaults = NSUserDefaults()
let myname = defaults.stringForKey("userName")


//store data
let context = CoreDataService.sharedCoreDataService.mainQueueContext
let friend = NSEntityDescription.insertNewObjectForNamedEntity(Friends.self, inManagedObjectContext: context)
friend.name = message.substringFromIndex(3)

try! context.save()

let sendMsg = AppConfig.AddClientHead("agreeAdd", body:myname)

let (_,_) = client.send(str:sendMsg)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate?.clientReadThread(self, didReceiveMessage: message)
})


}
else if head! == AppConfig.TransmissionAgreementConfiguration["chat"]
{
let search = message.substringFromIndex(3)
let resultController = try? catFriend.sharedCatFriendsService.catBlack(search)
try! resultController!.performFetch()

let flag = resultController?.sections?.count

if flag <= 1//friend
{

dispatch_async(dispatch_get_main_queue(), { () -> Void in

let client_info = message.split("|")
let client_name = client_info[1]

//self.alertDelegate?.presentWindow(client_name, msg: client_info[2])
self.viewDelegate?.presentWindow(client_name, msg: client_info[2])

})


}
else//black, not show the meg
{
//do nothing
}

}

}
}
}

var AppConfig=TransmissionAgreement(first:"test")
let client: TCPClient
var viewDelegate:LoginViewController?
var delegate: ClientReadThreadDelegate?
var alertDelegate: AlertDelegate?
private var resultController: NSFetchedResultsController?
}

最佳答案

presentViewContollerUIViewController 上的一种方法,因此您需要一个 View Controller 的实例才能呈现您的警报 View 。

通常完成此操作的方法是让您启动 ClientReadThread 的 View Controller 实现 AlertDelegate 协议(protocol)。 ClientReadThread 没有理由实现 AlertDelegate 本身。它会是它自己的委托(delegate)吗?

此外,您的委托(delegate)应该标记为weak,否则您将最终陷入保留周期。

class MyViewController : UIViewController, AlertDelegate {
private var clientReadThread: ClientReadThread?

override viewDidLoad() {
super.viewDidLoad();
clientReadThread = ClientReadThread(someTcpClient)
clientReadThread.viewDelegate = self
}

func presentWindow(title:String,msg:String) {
// Your code goes here.
}
}

您的另一个选择是将 UIViewController 的实例传递到 ClientReadThread 中,并使用它来呈现警报 View 。虽然那不是我愿意走的路。 ClientReadThread 不需要知道它的警报是如何处理的,这应该委托(delegate)给另一个类。

关于swift - 如何在自定义类中使用presentViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34972401/

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