2017-05-01 8 views
1

경고를 한 후 튜토리얼을 따라 코드에 넣으려고했습니다. 몇 가지 다른 장소에 코드를 넣었지만 오류가 계속 악화되었습니다. 나는 마침내 그것을 바닥에 두었습니다. 왜냐하면 내가 가장 적은 오류를 얻었던 곳이기 때문입니다. 나는 xcode에 익숙하지 않기 때문에이 아주 기본적인 것이 나를 위해 매우 어렵다. 또한, 내가 이것을 넣었을 때 나는 모든 곳에서 오류를 얻었고, 나는 이것을 어떻게 고쳐야할지 모른다. 또한, 내가하려고하는 일은 이름 인 UILabel에 저장된 데이터를 가져 와서 경고의 "사라지는"클릭 가능한 부분에 표시되도록하고 싶지만 아무 것도 없습니다. 아이디어를 얻는 방법 또는 기본 코드를 코드에 추가 할 수없는 경우 시작하는 방법에 대해서도 설명합니다. 어떤 도움도 훌륭한 소스 코드가 될 것입니다. 모든 질문에 사과드립니다. 다시 한번 감사드립니다.UIClert의 데이터가 xcode 8의 UIClert 문제 인 경우 오류

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 방법을 추가하려고하는 것을 의미합니다. 그래서 아래 코드 중 하나를 제거하고 createAlert이 이미 viewDidAppear에 존재하면 호출하십시오.

두 번째 실수는 당신이 UIAlertActionHandler에 대한 }를 추가 깜빡하고 자동으로 경보를 해제합니다 경고 조치에 dismiss를 호출 할 필요가없는 것입니다.

또한 선택기 구문을 Swift3으로 변경해야하며 코드에 handleReceivedDataWithNotification을 추가하는 것을 잊어 버린 경우도 있습니다.

스위프트를 사용하여 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) 
    } 
} 
+0

예 그것은 대단히 감사합니다 작동합니다. 그래도 한 가지 더 질문이 있습니다. 이게 무슨 뜻입니까? 어떻게해야합니까? 대답과 함께 컨트롤러를 잘 작동하는 것?! 그래서 당신이 아래에 말한 것들을 할 필요가 있습니까? 그렇다면 어떻게해야합니까? 죄송합니다. xcode를 처음 사용하게되었습니다. 또한 정말로 도움이 된 모든 도움에 감사드립니다. "선택기 구문을 Swift3으로 변경해야하며 handleReceivedDataWithNotification을 코드와 함께 추가하는 것을 잊어 버렸습니다. Swift는 NSDictionary 대신 Swift 기본 사전 유형을 사용하므로 원하는대로 아래쪽으로 컨트롤러를 변경하십시오 산출." – john

+0

@john Welcome mate :) 질문은 무엇입니까? 귀하의 의견을 듣지 마십시오. –

+0

다시 한번 감사드립니다. 그래서 내가 묻는 것은 텍스트 상자 (짐 존, 마이클 제이크 등)에 어떤 이름을 입력했는지를 내 레이블에 저장하는 방법입니다. "기본 이름". 죄송합니다. xcode를 처음 사용하고 있으며이를 수행하는 방법을 모르겠습니다. 또한 Alert는 퇴근 후 홈 화면으로 돌아올 때 두 번 이상 나타납니다. 어떻게하면 그걸 막을 수있어 새로운 플레이어가 연결될 때 한 번만 나타납니다. 미안 모든 질문에 대해 나는 단지 이런 것들을 파악하는 것 같습니다. – 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) 
} 

}