iphone - 使用 segue 将数据传输到另一个 Controller 。从代码

标签 iphone objective-c xcode cocoa-touch storyboard

如何将值从一个 Controller 传递到另一个???我使用 Storyboard。

storyboard

我希望它出现在第一个 View 的突出显示 TextView 中。

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

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

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

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

完成 Controller :

- (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.
}

第一 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

请注意,通常情况下,您可以向 self 询问 Storyboard。然而,当你有一个 segues 连接时,即使那样也是不必要的:

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

然后调用 prepareForSegue 。另请注意,您可以(应该)使用比 segue 标识符更权威的东西来确定您是否应该加载数据......您可以询问 segue 的目标 Controller 它是否是正确的类:

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

您可能已经知道(因为您在代码中使用了标识符),但为了这篇文章的 future 发现者的利益;当您在 Storyboard 中时,在 Xcode 的检查面板中会为 segues 提供标识符。

关于iphone - 使用 segue 将数据传输到另一个 Controller 。从代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11464155/

相关文章:

iphone - 如何将核心数据导出到 CSV

iphone - git commit 不会将文件添加到 xcodeproj

objective-c - 获取cocoa脚本中选择框的值

ios - 显示键盘时,Admob 广告不会出现在 UITableView 的页脚中

ios - Amazon Device Farm 的 KIF .xctest 目录

xcode - 如何在 XCode 中移动代码行

ios - 我怎样才能在 ios 上静音传入的文本

iphone - 如何在 Objective-C 中解析文本文件?

iphone - cocos3d 中的简单多边形

ios - Xcode 10 (10A255) 错误 : <unknown>:0: error: duplicate input file