2017-05-01 2 views
1

アラートが発生しました。私はチュートリアルに従い、自分のコードに入れようとしました。私はコードをいくつかの異なる場所に置いたが、エラーはちょうど悪化し続けていた。私は最終的にそれを底に置いた。なぜなら、私は少しの間違いがあったからだ。私はxcodeの初心者ですので、この非常に基本的なことは私のために非常に難しいです。さらに、私はこれを入れると、どこにでもエラーが発生し、これを修正する方法はわかりません。さらに、私がやろうとしていることは、UILabelに保存されているデータを名前で取得することです。アラートのクリック可能な部分にそのデータを表示して、アラートを「破棄」したいのですが、私のコードに基本的な警告を追加することができないときに、それを行う方法、または始めることさえできます。どんな助けでも素晴らしいソースコードになるでしょう。すべての質問を申し訳ありません。もう一度おねがいします。xcode 8でUILlelのデータがUIAlertに発行されました。エラー

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) 

} 
} 

答えて

0

あなたはおそらく取得しているエラーがInvalid redeclaration of 'viewDidAppear'はあなたViewControllerで二回viewDidAppearメソッドを追加しようとしていることです。したがって、あなたのコードから以下のいずれかのメソッドを削除して、がすでに存在していれば、viewDidAppearが呼び出されます。

2度目の間違いは、UIAlertActionHandler}を追加することを忘れていて、dismissに電話する必要はありません。アラートが自動的に解除されます。

また、セレクタ構文をSwift3に変更する必要があります。また、コードにhandleReceivedDataWithNotificationを追加することを忘れてしまったことがあります。

スウィフトを使用して、NSDictionaryの代わりにSwiftネイティブ辞書タイプを使用しているので、希望する出力を得るために、コントローラを以下のものに変更してください。

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

はいそれはどうもありがとうございました作品。私はもう一つの質問がありますが これはどういう意味ですか...私はそれをやりますか?あなたは答えを入れコントローラが正常に動作するようだ!私はあなたが下で言ったことをする必要がありますか?申し訳ありませんが、私はxcodeの新機能です。また、本当に助けてくれた皆さんのご協力に感謝します。 "セレクタ構文をSwift3に変更する必要があります。また、handleReceivedDataWithNotificationをコードに追加することを忘れてしまった SwiftではNSDictionaryではなくSwiftネイティブ辞書型を使用していますので、出力。" – john

+0

@johnようこそメイト:)ここでの質問は何ですか?あなたのコメントを取得しないでください。 –

+0

もう一度ありがとうございます。だから私が求めているのは、どのような名前が私のテキストボックスに入力されているか(ジムジョン、マイケルジェイクなど)が私のラベルに保存されている場所にする方法です。デフォルト名。申し訳ありませんが、私はxcodeを初めて利用しており、これを行う方法はわかりません。また、アラートは、ホーム画面を離れるとホーム画面に戻るときに複数回ポップアップします。どうすればそれを止めることができるので、新しいプレーヤーが接続するときに一度だけポップアップする。私はちょうどこれらの事を把握することができないすべての質問を申し訳ありません。 – 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) 
} 

}

関連する問題