Swift3 xcode 8中UILabel数据到UIAlert的问题,错误

Swift3 xcode 8中UILabel数据到UIAlert的问题,错误,swift3,uilabel,xcode8,alert,Swift3,Uilabel,Xcode8,Alert,我发出了一个警告,并尝试按照教程进行操作,并将其放入我的代码中。我把代码放在了几个不同的地方,但是错误越来越严重。我最终还是把它放在了底部,因为那是我得到最少错误的地方。我是xcode新手,所以这个非常基本的东西对我来说非常困难。此外,当我把它放进去的时候,我会发现到处都是错误,我不知道如何修复它。此外,我试图做的是获取保存在UILabel中的数据,UILabel是一个名称,我希望它显示在警报的可单击部分,即“解除”警报,但我不知道如何做,甚至不知道如何在无法将基本警报添加到代码中时开始。任何帮

我发出了一个警告,并尝试按照教程进行操作,并将其放入我的代码中。我把代码放在了几个不同的地方,但是错误越来越严重。我最终还是把它放在了底部,因为那是我得到最少错误的地方。我是xcode新手,所以这个非常基本的东西对我来说非常困难。此外,当我把它放进去的时候,我会发现到处都是错误,我不知道如何修复它。此外,我试图做的是获取保存在UILabel中的数据,UILabel是一个名称,我希望它显示在警报的可单击部分,即“解除”警报,但我不知道如何做,甚至不知道如何在无法将基本警报添加到代码中时开始。任何帮助都是很棒的源代码,甚至更好。很抱歉问了这么多问题。提前再次感谢

import UIKit
import MultipeerConnectivity


class ViewController: UIViewController, MCBrowserViewControllerDelegate {

@IBOutlet weak var input: UITextField!

@IBOutlet weak var output: UILabel!

@IBAction func action(_ sender: Any) {
    output.text = input.text
    UserDefaults.standard.set(input.text, forKey: "MyName")
    input.text = ""
}

var currentPlayer:String!

var appDelegate:AppDelegate!

override func viewDidLoad() {
    super.viewDidLoad()
    appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.MPCHandler.setupPeerWithDisplayName(UIDevice.current.name)
    appDelegate.MPCHandler.setupSession()
    appDelegate.MPCHandler.advertiseSelf(true)

    NotificationCenter.default.addObserver(self, selector: Selector(("peerChangedStateWithNotification:")), name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil)

    NotificationCenter.default.addObserver(self, selector: Selector(("handleReceivedDataWithNotification:")), name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil)
}

@IBAction func connect(_ sender: Any) {

    if appDelegate.MPCHandler.session != nil{
        appDelegate.MPCHandler.setupBrowser()
        appDelegate.MPCHandler.browser.delegate = self

        self.present(appDelegate.MPCHandler.browser, animated: true, completion: nil)

    }
}

func peerChangedStateWithNotification(notification:NSNotification){
    let userInfo = NSDictionary(dictionary: notification.userInfo!)

    let state = userInfo.object(forKey: "state") as! Int

    if state != MCSessionState.connecting.rawValue{
        self.navigationItem.title = "Connected"
    }

}

func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
    appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
}

func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
    appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(_ animated: Bool) {
    if let x = UserDefaults.standard.object(forKey:"myName") as?
        String
    {
        output.text = x
    }
}

}


 func viewDidAppear(_animated: Bool) {
createAlert(title: "HI", message: "ARE YOU READY")

}

 func createAlert (title: String, message:String)
{


let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

alert.addAction(UIAlertAction(title: "HI", style: UIAlertActionStyle.default, handler: { (action) in alert.dismiss(animated: true, completion: nil);))

    self.present(alert,animated: true, completion:nil)

}
}

您最可能遇到的错误是
对“ViewDidAspect”的重新声明无效
表示您试图在
ViewController中添加两次
ViewDidAspect
方法。因此,从代码中删除下面的一个方法,并在已经存在的
viewdide
one中调用
createAlert

您的第二个错误是您忘记为
UIAlertActionHandler
添加
}
,并且没有必要在警报操作时调用
Disclease
,它将自动解除警报

您还需要将选择器语法更改为Swift3 one,并且忘记在代码中添加
HandlerReceivedDataWithNotification

现在使用Swift使用Swift本机字典类型,而不是使用
NSDictionary
,因此将控制器更改为以下类型,以获得所需的输出

class ViewController: UIViewController, MCBrowserViewControllerDelegate {

    @IBOutlet weak var input: UITextField!

    @IBOutlet weak var output: UILabel!      

    var currentPlayer:String!

    var appDelegate:AppDelegate!

    override func viewDidLoad() {
        super.viewDidLoad()
        appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.MPCHandler.setupPeerWithDisplayName(UIDevice.current.name)
        appDelegate.MPCHandler.setupSession()
        appDelegate.MPCHandler.advertiseSelf(true)

        NotificationCenter.default.addObserver(self, selector: #selector(peerChangedStateWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(handleReceivedDataWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func connect(_ sender: Any) {

        if appDelegate.MPCHandler.session != nil{
            appDelegate.MPCHandler.setupBrowser()
            appDelegate.MPCHandler.browser.delegate = self

            self.present(appDelegate.MPCHandler.browser, animated: true, completion: nil)

        }
    }

    @IBAction func action(_ sender: Any) {
        output.text = input.text
        UserDefaults.standard.set(input.text, forKey: "MyName")
        input.text = ""
    }

    func peerChangedStateWithNotification(_ notification: Notification) {
        let userInfo = notification.userInfo!

        let state = userInfo["state"] as! Int

        if state != MCSessionState.connecting.rawValue{
            self.navigationItem.title = "Connected"
        }

    }

    func handleReceivedDataWithNotification(_ notification: Notification) {
        let userInfo = notification.userInfo!
        print(userInfo)
    }

    func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
        appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
    }

    func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
        appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
    }

    override func viewDidAppear(_ animated: Bool) {
        if let x = UserDefaults.standard.string(forKey: "myName") {
            output.text = x
        }
        else {
            output.text = "Default Name" //Set here default Name
        }
        self.createAlert(title: "HI", message: "ARE YOU READY")
    }

    func createAlert (title: String, message:String)
    {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: output.text, style: UIAlertActionStyle.default, handler: { (action) in

        }))
        self.present(alert,animated: true, completion:nil)
    }
}

您最可能遇到的错误是
对“ViewDidAspect”的重新声明无效
表示您试图在
ViewController中添加两次
ViewDidAspect
方法。因此,从代码中删除下面的一个方法,并在已经存在的
viewdide
one中调用
createAlert

您的第二个错误是您忘记为
UIAlertActionHandler
添加
}
,并且没有必要在警报操作时调用
Disclease
,它将自动解除警报

您还需要将选择器语法更改为Swift3 one,并且忘记在代码中添加
HandlerReceivedDataWithNotification

现在使用Swift使用Swift本机字典类型,而不是使用
NSDictionary
,因此将控制器更改为以下类型,以获得所需的输出

class ViewController: UIViewController, MCBrowserViewControllerDelegate {

    @IBOutlet weak var input: UITextField!

    @IBOutlet weak var output: UILabel!      

    var currentPlayer:String!

    var appDelegate:AppDelegate!

    override func viewDidLoad() {
        super.viewDidLoad()
        appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.MPCHandler.setupPeerWithDisplayName(UIDevice.current.name)
        appDelegate.MPCHandler.setupSession()
        appDelegate.MPCHandler.advertiseSelf(true)

        NotificationCenter.default.addObserver(self, selector: #selector(peerChangedStateWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(handleReceivedDataWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func connect(_ sender: Any) {

        if appDelegate.MPCHandler.session != nil{
            appDelegate.MPCHandler.setupBrowser()
            appDelegate.MPCHandler.browser.delegate = self

            self.present(appDelegate.MPCHandler.browser, animated: true, completion: nil)

        }
    }

    @IBAction func action(_ sender: Any) {
        output.text = input.text
        UserDefaults.standard.set(input.text, forKey: "MyName")
        input.text = ""
    }

    func peerChangedStateWithNotification(_ notification: Notification) {
        let userInfo = notification.userInfo!

        let state = userInfo["state"] as! Int

        if state != MCSessionState.connecting.rawValue{
            self.navigationItem.title = "Connected"
        }

    }

    func handleReceivedDataWithNotification(_ notification: Notification) {
        let userInfo = notification.userInfo!
        print(userInfo)
    }

    func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
        appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
    }

    func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
        appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)
    }

    override func viewDidAppear(_ animated: Bool) {
        if let x = UserDefaults.standard.string(forKey: "myName") {
            output.text = x
        }
        else {
            output.text = "Default Name" //Set here default Name
        }
        self.createAlert(title: "HI", message: "ARE YOU READY")
    }

    func createAlert (title: String, message:String)
    {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: output.text, style: UIAlertActionStyle.default, handler: { (action) in

        }))
        self.present(alert,animated: true, completion:nil)
    }
}
}


}

是的,很有效,非常感谢。我还有一个问题,这意味着什么……我该怎么做。你用答案放的控制器似乎工作正常?!那么我需要做你下面说的事情吗?如果需要,怎么做?对不起,我对xcode很陌生。也非常感谢你的帮助,真的很有帮助。“您还需要将选择器语法更改为Swift3 one,并且您忘了在代码中添加HandlerReceivedDataWithNotification。现在使用Swift使用Swift本机字典类型而不是NSDictionary,因此请将控制器更改为以下一种,以获得所需的输出。”@john Welcome mate:)这里有什么问题?不要听到你的评论。再次感谢。所以,我要问的是,如何才能使我的文本框中键入的任何名称(吉姆·约翰、迈克尔·杰克等)保存在我的标签中。注册它的“默认名称”。对不起,我是xcode新手,不知道怎么做。此外,离开主屏幕后返回主屏幕时,警报会多次弹出。我怎样才能阻止它,使它只在新玩家连接时弹出一次。很抱歉,我似乎无法解决所有问题。@john如果您想显示弹出窗口一次,请在
viewDidLoad
中调用方法
createAlert
,而不是
viewDidAppear
。要更改标签文本,可以使用textField委托方法didEndEditing搜索。谢谢!。所以我把我在第二个答案中所做的写在你的下面。我试着用驼峰法处理“viewDidLoad”和“viewDidAppear”,但我不断地出错,我不知道发生了什么。我还尝试输入一个didEndEditing,这样任何用户都可以在其中输入名称,文本将保存在标签中。因此出现在弹出窗口中,但这给了我更多的问题,所以我把它拿了出来。我也不知道该把它确切地放在哪里。它似乎干扰了ViewDidDisplay功能。对于所有的问题我很抱歉,我只是想纠正一下。是的,非常感谢。我还有一个问题,这意味着什么……我该怎么做。你用答案放的控制器似乎工作正常?!那么我需要做你下面说的事情吗?如果需要,怎么做?对不起,我对xcode很陌生。也非常感谢你的帮助,真的很有帮助。“您还需要将选择器语法更改为Swift3 one,并且您忘了在代码中添加HandlerReceivedDataWithNotification。现在使用Swift使用Swift本机字典类型而不是NSDictionary,因此请将控制器更改为以下一种,以获得所需的输出。”@john Welcome mate:)这里有什么问题?不要听到你的评论。再次感谢。所以,我要问的是,如何才能使我的文本框中键入的任何名称(吉姆·约翰、迈克尔·杰克等)保存在我的标签中。注册它的“默认名称”。对不起,我是xcode新手,不知道怎么做。此外,离开主屏幕后返回主屏幕时,警报会多次弹出。我怎样才能阻止它,使它只在新玩家连接时弹出一次。很抱歉,我似乎无法解决所有问题。@john如果您想显示弹出窗口一次,请在
viewDidLoad
中调用方法
createAlert
,而不是
viewDidAppear
。要更改标签文本,可以使用textField委托方法didEndEditing搜索。谢谢!。所以我在第二次回答中记下了我所做的