2017-05-03 14 views
1

안녕하세요, IOS 앱에 푸시 알림 기능을 구현하려고합니다. 현재 푸시 알림을 통해 앱이 열리면 특정 viewcontroller를 열 수있는 코드가 있습니다. 내가이 일을하는 방식은 viewcontroller ontop을 밀어 넣는 것입니다.appcontrol에서 viewcontroller로 데이터를 전달하는 방법은 무엇입니까?

지금해야 할 일은 appcontrol에서 viewcontroller로 일부 데이터를 전달하는 것입니다. viewcontroller에서 viewcontroller로 데이터를 전달하는 것은 prepareforsegue를 사용하는 것임을 이해합니다. 나는이 일을 시도하고 그것은 작동하지 않았다

이 작업을 수행하는 방법을 연구했지만 stackoverflow에 대한 많은 답변을 신속하게 변환 할 수있는 능력이없는 구식 코드였습니다. 누군가 설명 할 수 있습니까? 나에게 appcontlegate에서 viewcontroller로 데이터를 어떻게 보내겠습니까? didFinishLaunchingWithOptions에

let storyboard = UIStoryboard(name: "Main", bundle: nil) 

let destinationViewController = storyboard.instantiateViewController(withIdentifier: "detailPin2") as! detailPinViewController 

let navigationController = self.window?.rootViewController as! UINavigationController 

navigationController.pushViewController(destinationViewController, animated: false) 
+0

AppDelegate의 VC를 * 표시하는 데 사용하는 코드를 표시하십시오. – DonMag

+0

수정 된 게시물. 코드가 지금 올라와야합니다 – JackieXY

+0

내가 빠진 것이 있습니까? 당신은 appdelegate의 함수 안에 있고 당신은 목적지 VC에 대한 포인터를 가지고 있습니다. 전달하려는 데이터로 VC의 멤버 함수를 호출 할 수 없습니까? – Spads

답변

1

OK입니다 VC 보여주는를 heres 코드

- 이미 수행 된 작업의 대부분을 가지고있다를 ...

segues를 사용하여, 아이폰 OS가보기 컨트롤러를 생성 는 didFinishLaunchingWithOptions에서

if let vc = segue.destination as? detailPinViewController { 
    vc.myVariable = "this is the data to pass" 
} 

, 당신이 창조 부분을하고있다 : 당신은 당신이 아마 이런 식으로 뭔가를했을 경우, 준비 그것에 액세스 할 수 있습니다 d "프레젠테이션"파트 ... "데이터 전달"파트를 추가하면됩니다.

let destinationViewController = storyboard.instantiateViewController(withIdentifier: "detailPin2") as! detailPinViewController 

destinationViewController.myVariable = "this is the data to pass" 

let navigationController = self.window?.rootViewController as! UINavigationController 

navigationController.pushViewController(destinationViewController, animated: false) 

이렇게하면됩니다.

-1
//Declare you variable somewhere within the app delegate scope 
@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    var myVariable: String = "Hello Swift" 


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     return true 
    } 
// some other app delegate’s methods.... 

} 

//In your view controller 
class ViewController: UIViewController { 

    @IBOutlet weak var myLabel: UILabel! 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    let myOtherVariable = appDelegate.myVariable 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     myLabel.text = myOtherVariable 
     var anotherVariable: String = appDelegate.myVariable // etc... 

    } 

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


}