将数据传递到嵌入在导航控制器中的另一个VC

时间:2013-11-21 10:35:42

标签: ios objective-c

是否可以将数据从一个ViewController发送到另一个ViewController,其中第二个ViewController嵌入在导航控制器中? (反之亦然)

注意:抱歉,我错过了应该了解更多细节。这里的问题是app结构如下:ViewControllerOne> UINvaigationController> ViewControllerTwo。所以我想将数据传递给ViewControllerTwo。我试图通过PrepareForSegue传递它,但我收到此错误:

  

- [UINavigationController setString:]:无法识别的选择器发送到实例0x98b71c0

以下是代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
   VCC *vc2 = (VCC*)segue.identifier;
   vc2.string = @"something";
}

4 个答案:

答案 0 :(得分:1)

在您的案例中传递数据的一个例子是:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"segueName"]) {

    SecondClassName *destTableViewController = [[segue.destinationViewController viewControllers] objectAtIndex: 0];
    destTableViewController.yourDataInSecondClass=yourDataInFirstClass;

}}

护理:您必须为连接First VC和导航控制器的segue命名,并使用此名称更改上面代码中的segueName。

每次选择更改VC时,都会自己调用prepareForSegue方法。

答案 1 :(得分:0)

Ofcourse。将viewcontroller引用保存在哪里(不是最佳解决方案)。例如,在AppDelegate中,[[UIApplication sharedApplication]委托] .viewController1和[[UIApplication sharedApplication]委托] .viewController2

这取决于你的结构如何获得viewcontrollers,但它并不难。

获取navigationController中的所有viewcontrollers:self.navigationController.viewControllers。它返回的是带有ViewControllers的NSArray。

答案 2 :(得分:0)

实际上,此查询有很多方法 -

其中一个解决方案 -

在FirstViewController中 -

关于UIButton行动 -

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.stringForPassing = @"PassString";
[self.navigationController pushViewController:secondViewController animated:YES];

在SecondViewController.h中

@property (nonatomic,copy)NSString *stringForPassing;

在SecondViewController.m中

@synthesize stringForPassing;

现在您可以在SecondViewController中访问stringForPassing。

愿这对你有所帮助。提前谢谢。

答案 3 :(得分:0)

您只需使用属性即可在控制器之间发送数据。

假设您有2个Viewcontrollers,FirstViewController& SecondViewController。你想发一个字符串@“hello world”到SecondViewController

然后

1. create a @property(nonatomic,retain) NSString * nameStr ; in SecondViewController 

2. synthesize it .

3. suppose your navigating to secondViewController on click of a button .

-(void) onButtonClicked
{
     SecondViewController * secondVC = [[SecondViewController alloc] init];

     secondVC.nameStr = @"hello , world";

     [self.navigationController pushToViewController:secondVc];
}

现在您可以通过调用self.nameStr

来获取SecondViewController中的值
相关问题