Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将数据从ViewController传递到嵌入式View Controller_Ios_Swift_Segue_Viewcontroller - Fatal编程技术网

Ios 如何将数据从ViewController传递到嵌入式View Controller

Ios 如何将数据从ViewController传递到嵌入式View Controller,ios,swift,segue,viewcontroller,Ios,Swift,Segue,Viewcontroller,我有一个视图控制器,用户点击按钮并移动到第二个视图,我想向它传递一些数据,但第二个视图有一个导航视图控制器,所以prepareforsgue()在这里不起作用。我应该先将数据传递到导航视图,然后再传递到第二视图还是执行其他操作?如果您的数据大小适中,您可以使用NSNotificationCenter传递数据 -(IBAction)moveToSecondViewButtonClicked:(UIButton*)sender { SecondViewController *secondVi

我有一个视图控制器,用户点击按钮并移动到第二个视图,我想向它传递一些数据,但第二个视图有一个导航视图控制器,所以prepareforsgue()在这里不起作用。我应该先将数据传递到导航视图,然后再传递到第二视图还是执行其他操作?

如果您的数据大小适中,您可以使用
NSNotificationCenter
传递数据

-(IBAction)moveToSecondViewButtonClicked:(UIButton*)sender {
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    secondViewController.data = data;
    [self.navigationController pushViewController:secondViewController animated:YES];
}
发件人类别

- (void)sendNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:YOUR_DATA];
}
- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveNotification:) 
        name:@"myNotification"
        object:nil];
}

- (void)receiveNotification:(NSNotification *)notification
{
    if ([[notification name] isEqualToString:@"myNotification"]) {

        //doSomething here.
    }
}
接收器类别

- (void)sendNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:YOUR_DATA];
}
- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveNotification:) 
        name:@"myNotification"
        object:nil];
}

- (void)receiveNotification:(NSNotification *)notification
{
    if ([[notification name] isEqualToString:@"myNotification"]) {

        //doSomething here.
    }
}

希望这有帮助

如果您的数据大小适中,您可以使用
NSNotificationCenter
传递数据

发件人类别

- (void)sendNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:YOUR_DATA];
}
- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveNotification:) 
        name:@"myNotification"
        object:nil];
}

- (void)receiveNotification:(NSNotification *)notification
{
    if ([[notification name] isEqualToString:@"myNotification"]) {

        //doSomething here.
    }
}
接收器类别

- (void)sendNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:YOUR_DATA];
}
- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveNotification:) 
        name:@"myNotification"
        object:nil];
}

- (void)receiveNotification:(NSNotification *)notification
{
    if ([[notification name] isEqualToString:@"myNotification"]) {

        //doSomething here.
    }
}

希望这有助于

不要从viewcontroller切换到UIViewController。必须将viewcontroller转换为新UIViewController的UINavigationController。您可以在prepareForSegue方法中传递数据

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.

    if segue.identifier == "showNavigationController"{
        let navController : UINavigationController  = segue.destinationViewController as! UINavigationController
        let viewController : UIViewController = navController.viewControllers[0] as! UIViewController
        viewController.data = yourData
    }
}

不要从viewcontroller切换到UIViewController。必须将viewcontroller转换为新UIViewController的UINavigationController。您可以在prepareForSegue方法中传递数据

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.

    if segue.identifier == "showNavigationController"{
        let navController : UINavigationController  = segue.destinationViewController as! UINavigationController
        let viewController : UIViewController = navController.viewControllers[0] as! UIViewController
        viewController.data = yourData
    }
}
据此: ,以下解决方案更为正确:使用UINavitaionController的“topViewController”

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    let segueID = segue.identifier ?? ""

    switch segueID
    {
        case "toMyNextVC":
            let nc = segue.destinationViewController as! UINavigationController
            let destinationVC = nc.topViewController as! MyNextViewController
            destinationVC.varToPass = myVarToPass

        default:
            break
    }
}
据此: ,以下解决方案更为正确:使用UINavitaionController的“topViewController”

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    let segueID = segue.identifier ?? ""

    switch segueID
    {
        case "toMyNextVC":
            let nc = segue.destinationViewController as! UINavigationController
            let destinationVC = nc.topViewController as! MyNextViewController
            destinationVC.varToPass = myVarToPass

        default:
            break
    }
}
我想它可以帮助你,我想它可以帮助你。