2017-05-01 8 views
1

Ich machte eine Warnung und ich versuchte, ein Tutorial zu folgen und es in meinen Code. Ich habe den Code an ein paar verschiedenen Stellen eingegeben, aber die Fehler wurden immer schlimmer. Ich habe es schließlich ganz unten gestellt, weil ich am Ende die wenigsten Fehler bekommen habe. Ich bin neu bei xcode, also ist diese sehr einfache Sache sehr schwierig für mich. Außerdem bekomme ich überall Fehler, wenn ich das einfüge, und ich habe keine Ahnung, wie ich das beheben kann. Außerdem versuche ich, die Daten zu speichern, die in meinem UILabel gespeichert sind. Das ist ein Name, und ich möchte, dass dies im klickbaren Teil der Warnung angezeigt wird, der die Warnung "abweist", aber ich habe keine Idee, wie man das macht oder sogar loslegen kann, wenn ich meinem Code keinen Grundalarm hinzufügen kann. Jede Hilfe wäre toller Quellcode noch besser. Entschuldigung für alle Fragen. Nochmals vielen Dank im Voraus.UILabel Daten zu UIAlert Problem in xcode 8, Fehler

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) 

} 
} 

Antwort

0

Der Fehler, den Sie wahrscheinlich bekommen ist Invalid redeclaration of 'viewDidAppear' bedeutet, dass Sie viewDidAppear Methode zweimal in Ihrem ViewController hinzufügen möchten. So entfernen Sie die unten eine Methode Formular Ihren Code und rufen Sie createAlert innerhalb der bereits vorhandenen viewDidAppear.

Ihr zweiter Fehler ist, dass Sie vergessen haben, } für UIAlertActionHandler hinzuzufügen, und es ist nicht erforderlich, dismiss bei Alarmaktion zu rufen, wird die Warnung automatisch ausgeblendet.

Sie müssen auch Ihre Wählersyntax in Swift3 ändern und Sie haben auch vergessen, handleReceivedDataWithNotification mit Ihrem Code hinzuzufügen.

Jetzt mit Swift verwenden Swift native Wörterbuch Typ anstelle von NSDictionary, so ändern Sie Ihren Controller mit unter einem, um Ihre gewünschte Ausgabe zu erhalten.

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) 
    } 
} 
+0

Ja es funktioniert danke Ihnen sehr. Ich habe noch eine Frage was bedeutet das..wie mache ich es. Der Controller, den Sie mit der Antwort einsetzen, scheint gut zu funktionieren ?! also muss ich die Dinge tun, die du unten gesagt hast, wenn ja wie? Entschuldigung, ich bin sehr neu in xcode. Auch vielen Dank für all Ihre Hilfe, es hat wirklich geholfen. „Sie müssen auch zu swift3 einer Ihrer Wähler Syntax ändern und auch vergessen haben, handleReceivedDataWithNotification mit Ihrem Code hinzuzufügen. Jetzt mit Swift Verwendung Swift nativer Wörterbuch statt NSDictionary geben, so ändern Sie Ihre Controller mit unter einem gewünschten erhalten Ausgabe." – john

+0

@john Willkommen Kumpel :) Was ist die Frage hier? Bekommen Sie Ihren Kommentar nicht. –

+0

Danke nochmal. Was ich also frage, ist, wie es dahin gelangen kann, wo der Name in meiner Textbox (jim john, michael jake usw.) in meinem Etikett gespeichert ist. m seinen "Standardnamen". Sorry, ich bin neu bei xcode und weiß nicht, wie ich das machen soll. Außerdem wird die Warnmeldung beim Verlassen des Startbildschirms mehrmals angezeigt. Wie kann ich das stoppen, so dass es nur einmal erscheint, wenn ein neuer Spieler sich verbindet? Entschuldigung für all die Fragen, die ich einfach nicht verstehen kann. – john

0
import UIKit 
import MultipeerConnectivity 


class ViewController: UIViewController, MCBrowserViewControllerDelegate { 

@IBOutlet weak var input: UITextField! 

@IBOutlet weak var output: UILabel! 

@IBAction func dick(_ 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(ViewController.peerChangedStateWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil) 

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.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) 

    } 
} 

@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) 
} 
*func viewdidloadOverride; func viewDidAppear*(_ animated: Bool) { 
    if let x = UserDefaults.standard.string(forKey: "myName") { 
     output.text = x 
    } 
    else { 
     output.text = "x" //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) 
} 

}