将数据传送到与另一个赛格瑞控制器。 从代码(Transferring data to anoth

2019-09-19 10:28发布

如何从一个控制器将值传递给另一??? 我用故事板。

我想这出现在第一个视图中突出显示的文本视图。

调用的代码的下一个观点,我觉得这样的事情应该是这样的:

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

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

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

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";
    }
}

我想通过造成代码的传输值

Answer 1:

问题是,你实际上并没有使用SEGUE,您正在使用presentModalController代替。

请注意,通常情况下,你可以问self为它的故事板。 然而,即使这是不必要的,当你有一个塞格斯连接:

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

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

-(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的Inspector面板标识,当你在故事板。



文章来源: Transferring data to another controller with segue. From the code