【问题标题】:Transferring data to another controller with segue. From the code使用 segue 将数据传输到另一个控制器。从代码
【发布时间】:2012-07-12 22:19:58
【问题描述】:

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

我希望它出现在第一个视图的突出显示的文本视图中。

调用下一个视图的代码,我觉得应该是这样的:

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

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

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

完成控制器:

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

我想传递一个导致代码传输的值

【问题讨论】:

    标签: iphone objective-c xcode cocoa-touch storyboard


    【解决方案1】:

    问题是你实际上并没有使用那个segue,而是使用presentModalController

    请注意,通常,您可以向self 询问它的故事板。但是,当您连接了 segue 时,即使这样也没有必要:

    [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 的检查器面板中为 segues 提供了标识符。

    【讨论】:

    • 感谢您的快速回复!我意识到了错误,但是当我使用:“[self preformSegueWithIdentifier:@”FinishController“];”时,我收到一个错误:“'ViewController' 没有可见的@interface 声明选择器'preformSegueWithIdentifier:'”
    • 对不起,我遗漏了方法签名的一部分...它需要第二个参数(发送者),您通常将其作为“导致”segue 发生的东西(一个按钮,用于例如......但是按钮可以直接连接到故事板中的segues)。 developer.apple.com/library/ios/documentation/uikit/reference/…
    • [self performSegueWithIdentifier:@"Finish" sender:self];用过的!一切都很好!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多