Transferring data to another controller with segue. From the code

风格不统一 提交于 2019-12-23 20:20:14

问题


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:

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

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

I want to pass a value causing the transmission of the code


回答1:


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

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];

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

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.



来源:https://stackoverflow.com/questions/11464155/transferring-data-to-another-controller-with-segue-from-the-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!