How to pass data from one View to other view in IOS using UIStoryboard segues?

倾然丶 夕夏残阳落幕 提交于 2019-12-19 10:44:12

问题


I am using Xcode 4.3.1. I have created the UI using Storyboard which has many View Controllers.

Issue

In my app i am using ShakeGesture. While shaking i am doing some operations, its working fine. But, when shaking get stopped i need to pass some values to another View controller to display it. I have seen many post it says how to pass value when button clicked,but no one related with my problem.

It would be great if you solve my issue

My question is

How to pass values from one View to another View controller after shaking get stopped?

I am looking for any examples or tutorials. Much appreciated.

Exact function for the above question

I am having an button in First View Controller

When the button triggered it will popup pickerview with some datas. When the user selects it the picker value replaces the button value.

Then Shaking happens when it stopped i have to pass the data to other view controller.

Code

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake)
{
        // I need to pass data here. Thats i am confusing

}

}

This question was asked before but i couldn't get the solution thats why i am asking again. Apologise for that. Thanks for your help guys


回答1:


You can pass the data explicitly and in storyboard that is with the prepareForSegue call.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"ContactsViewControllerSegue"]){
        ContactsViewController *cvc = (ContactsViewController *)[segue destinationViewController];
        cvc.contacts = self.contacts;
        cvc.delegate = self;
    }
}

For a full tutorial check out this website.

To programmatically trigger the segue you can check the Apple Docs, but here is their example on orientation change.

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !isShowingLandscapeView)
    {
        [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
        isShowingLandscapeView = YES;
    }
// remainder of example omitted....
}

So you will trigger the segue when shaking stops, and then prepare the next viewController in the prepareForSegue method.




回答2:


You need to create string object in SecondViewController and set Property(nonatomic,retain) and synthesize it and after below code add when you push controller.

SecondViewController *objSecondViewController=[[SecondViewController alloc] 
initWithNibName:@"SecondViewController" bundle:nil];             

objSecondViewController.strtitle=@"Your data";
[self.navigationController pushViewController:objSecondViewController animated:YES];
[objSecondViewController release];


来源:https://stackoverflow.com/questions/11792567/how-to-pass-data-from-one-view-to-other-view-in-ios-using-uistoryboard-segues

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