使用segue将数据传输到另一个控制器。从代码 [英] Transferring data to another controller with segue. From the code

查看:129
本文介绍了使用segue将数据传输到另一个控制器。从代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将值从一个控制器传递到另一个控制器?我使用StoryBoard。

How to pass values ​​from one controller to another??? I use a StoryBoard.

我想要显示在第一个视图的突出显示的文本视图上。

I would like this to appear on the highlighted text view of the first view.

调用代码的下一个视图,我认为这样的东西应该像:

Call the next view of the code, I think something like this should look like:

UIStoryboard *finish = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    UIViewController *viewController = [finish instantiateViewControllerWithIdentifier:@"FinishController"];

     viewController.modalPresentationStyle = UIModalPresentationPageSheet;
     [self presentModalViewController:viewController animated:YES];

finishcontroller:

finishcontroller:

- (void)viewDidLoad
{
    self.lblFinishTitle.text=self.FinishTitle;
    self.lblFinishDesc.text = self.FinishDesc;
    self.lblFinishPoint.text=self.FinishPoint;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

第一个视图:

-(void) prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender
{
    if ([segue.identifier hasPrefix:@"FinishController"]) {
        FinishController *asker = (FinishController *) segue.destinationViewController;
        asker.FinishDesc = @"What do you want your label to say?";
        asker.FinishTitle = @"Label text";
        asker.FinishPoint = @"asdas";
    }
}

我想传递一个值,代码

推荐答案

问题是,你并没有真正使用segue,你使用 presentModalController

The issue is that you aren't actually using that segue, you are using presentModalController instead.

注意,通常,你可以问 self 但是,即使这是没有必要的,当你有一个segues连接:

Note that usually, you can just ask self for it's storyboard. However, even that is unnecessary when you have a segues connected:

[self preformSegueWithIdentifier:@"FinishController" sender:self];

然后调用prepareForSegue 。还要注意,你可以(应该)使用比segue标识符更权威的东西来确定你是否应该加载数据...你可以问segue的目标控制器是否是正确的类:

Then prepareForSegue will be called. Also note that you can (should) use something more authoritative than the segue identifier to determine if you should load the data... you can ask the segue's destination controller if it is the right class:

-(void) prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isKindOfClass:[FinishController class]]) {
        FinishController *asker = (FinishController *) segue.destinationViewController;
        asker.FinishDesc = @"What do you want your label to say?";
        asker.FinishTitle = @"Label text";
        asker.FinishPoint = @"asdas";
    }
}

你可能已经知道了在你的代码中),但是为了这个帖子的未来发现者的利益;当您在故事板中时,在Xcode的检查器面板中为segues赋予标识符。

You are probably already aware (since you used the identifier in your code), but for the benefit of future discoverers of this post; segues are given identifiers in the inspector panel of Xcode when you are in the storyboard.

这篇关于使用segue将数据传输到另一个控制器。从代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆