【问题标题】:How to pass data from one View to other view in IOS using UIStoryboard segues?如何使用 UIStoryboard segues 将数据从一个视图传递到 IOS 中的另一个视图?
【发布时间】:2012-08-01 07:35:00
【问题描述】:

我正在使用 Xcode 4.3.1。我使用具有许多视图控制器的 Storyboard 创建了 UI。

问题

在我的应用程序中,我使用的是 ShakeGesture。虽然摇晃我正在做一些操作,但它工作正常。但是,当摇晃停止时,我需要将一些值传递给另一个视图控制器以显示它。我看过很多帖子说如何在单击按钮时传递值,但没有一个与我的问题有关。

如果你能解决我的问题就好了

我的问题是

摇动停止后如何将值从一个视图传递到另一个视图控制器?

我正在寻找任何示例或教程。非常感谢。

上述问题的确切函数

我在 First View Controller

中有一个按钮

当按钮触发时,它会弹出带有一些数据的pickerview。当用户选择它时,选择器值将替换按钮值。

然后当它停止时发生抖动,我必须将数据传递给其他视图控制器。

代码

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake)
{
        // I need to pass data here. Thats i am confusing

}

}

之前有人问过这个问题,但我无法得到解决方案,这就是我再次问的原因。为此道歉。 谢谢你们的帮助

【问题讨论】:

标签: iphone ios ios5 uistoryboard shake


【解决方案1】:

您需要在 SecondViewController 中创建字符串对象并设置 Property(nonatomic,retain) 并合成它,然后在推送控制器时添加以下代码。

SecondViewController *objSecondViewController=[[SecondViewController alloc] 
initWithNibName:@"SecondViewController" bundle:nil];             

objSecondViewController.strtitle=@"Your data";
[self.navigationController pushViewController:objSecondViewController animated:YES];
[objSecondViewController release];

【讨论】:

  • 我用这种方式累了,但它导致异常
【解决方案2】:

您可以在使用 prepareForSegue 调用的情节提要中显式传递数据。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"ContactsViewControllerSegue"]){
        ContactsViewController *cvc = (ContactsViewController *)[segue destinationViewController];
        cvc.contacts = self.contacts;
        cvc.delegate = self;
    }
}

如需完整教程,请查看此website

要以编程方式触发 segue,您可以查看Apple Docs,但这是他们关于方向更改的示例。

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !isShowingLandscapeView)
    {
        [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
        isShowingLandscapeView = YES;
    }
// remainder of example omitted....
}

所以你会在摇动停止时触发segue,然后在prepareForSegue方法中准备下一个viewController。

【讨论】:

  • 感谢您的回答.. 我很困惑当震动停止时如何准备 segue..
  • 我添加了用于以编程方式触发 segue 的代码示例。
  • 你知道用那个 segue 发送数据吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
相关资源
最近更新 更多