在两个不同的故事板之间传递数据

时间:2013-03-17 19:14:18

标签: ios xcode storyboard segue

我有两个问题。

第一个问题

在我的项目中,我有两个不同的故事板文件:A storyboardB storyboard

我想将数据(nsstring)从(A) controller of (A) storyboard传递到(B) controller of (B) storyboard

我是怎么做到的?

第二个问题

在第二个故事板中,我有两个与segue链接的控制器

当我用这条指令在代码中调用segue时:

[self.navigation controller performSegueWithIdentifier: @"secondViewSegue" sender:self];

我有一条消息:"has no segue with identifier 'secondViewSegue' "

为什么?

2 个答案:

答案 0 :(得分:3)

1 /这样做的一个好方法是制作一个单独的模型对象,可以从两个位置同等地解决。 的最简单方法是将属性添加到@interface文件的AppDelegate.h部分,例如

  @property (nonatomic, strong) NSString* sharedString;

要设置并获取它,您需要在任何需要它的文件中添加对AppDelegate的类型访问权限:

  #include AppDelegate.h

然后你可以使用......

  AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

  //to set (eg in A controller)
  appDelegate.sharedString = string;

  //to get (eg in B controller)
  NSString* string = appDelegate.sharedString;  

作为属性的替代方法,您可以在头文件中使用静态变量:

 static NSString* staticString;

#imports头文件的任何对象都可以访问。虽然不是Objective-C的方式。

对于更复杂的案例,您可能需要创建一个单例对象来访问模型数据。

2 /尝试:

  [self performSegueWithIdentifier: @"secondViewSegue" sender:self];

确保Segue是从您的viewController连接的,而不是它的导航控制器。

答案 1 :(得分:1)

在不同的故事板中,有两种方法可以在两个不同的视图控制器之间传递数据。我正在使用什么,我想分享

Swift 3

let storyboard = UIStoryboard(name: "StoryboardName2", bundle: nil)
    let controller :NextViewController  = storyboard.instantiateViewController(withIdentifier: "NextVCIdentifier") as! NextViewController
    controller.NameofUser = Self.SelectedUser

将下一个viewcontroller中的变量声明为

public var NameofUser:String = ""
相关问题