iOS Passing Data Between View Controllers pt1 – prepareForSegue Example

Learn Xcode 4.2 Tutorial iOS iPad iPhone 1.1 Introduction to Xcode

iOS Passing Data Between View Controllers pt1 – prepareForSegue Example

Storyboarding has extremely simplified iOS programming within Xcode. In this tutorial, we detail iOS passing data between view controllers with the method prepareForSegue. When a segue is triggered, but before the visual transition occurs, the storyboard runtime calls the current view controller’s prepareForSegue:sender: method so that it can pass any needed data to the view controller that is about to be displayed. See resources section to download the the initial tutorial assets and you can download the final project in iOS Passing Data Between View Controllers pt2 – prepareForSegue Example.


Video Tutorial


Notes

performSegueWithIdentifier:sender:

Initiates the segue with the specified identifier from the view controller’s storyboard file.

- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
Parameters
identifier
The string that identifies the segue inside the storyboard file.In Interface Builder, you can associate an identifier string with each segue using the inspector. This string is used only for locating the segue inside the storyboard. This is the string that you pass to this parameter.This method throws an exception if there is no segue with the specified identifier.

sender
The object that you want to use to initiate the segue. This object is made available for informational purposes during the actual segue.
Discussion

Apps normally do not need to trigger segues directly. Instead, you configure an object in Interface Builder associated with the view controller, such as a control embedded in its view hierarchy, to trigger the segue. However, you can call this method to trigger a segue programmatically, perhaps in response to some action that cannot be specified in the storyboard resource file. For example, you might call it from a custom action handler used to process shake or accelerometer events.

The view controller that receives this message must have been loaded from a storyboard. If the view controller does not have an associated storyboard, perhaps because you allocated and initialized it yourself, this method throws an exception.

Availability
  • Available in iOS 5.0 and later.

follow us:
twitter: http://www.twitter.com/mybringback
facebook: http://www.facebook.com/mybringback


Resources

Download: Initial Project

Download Xcode


Source Code Required

</pre>
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"hello"]) {

NSString *intro = @"Hello my friend.";

ViewController2 *vc = [segue destinationViewController];

vc.introString = intro;

} else if ([segue.identifier isEqualToString:@"hola"]) {

NSString *intro = @"Hola mi amigo.";

ViewController2 *vc = [segue destinationViewController];

vc.introString = intro;

} else if ([segue.identifier isEqualToString:@"bam"]) {

UIImage *image = [UIImage imageNamed:@"12.png"];

ViewController2 *vc = [segue destinationViewController];

vc.introImage = image;

}

}
<pre>

 

</pre>
- (void)viewDidLoad

{

[label setText:introString];

[imageView setImage:introImage];

[super viewDidLoad];

// Do any additional setup after loading the view.

}

Author: jake

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>