How to pass values to another UINavigationViewController - ios

I have a UIViewController, and i have to pass a value (UIImage, NSString) to an UINavigationViewController, I am using
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
AppDetail *advc = [segue destinationViewController];
if ([[segue identifier] isEqualToString:#"showDetail"]) {
advc.appTitel = name;
advc.appIcon = icon;
advc.detailAppName = detileName;
advc.appDescription = description;
advc.appDeveloper = developer;
}
}
to pass the value to the UIViewController, it works perfectly if I pass it directly to a UIViewController, but I can't pass it to a UINavigationViewController,
Anyone know how I can do this?

I have this sample code from an Apple segue example. Perhaps it helps.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Room Create"]) {
// Prepare the settings view where the user inputs the 'serviceType' and local peer 'displayName'
UINavigationController *navController = segue.destinationViewController;
SettingsViewController *viewController = (SettingsViewController *)navController.topViewController;
viewController.delegate = self;
// Pass the existing properties (if any) so the user can edit them.
viewController.displayName = self.displayName;
viewController.serviceType = self.serviceType;
}
}

To pass value using Segue in your case with a UINavigationController try the code below :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"])
{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
AppDetail *advc = (AppDetail *)navController.topViewController;
advc.appTitel = name;
advc.appIcon = icon;
advc.detailAppName = detileName;
advc.appDescription = description;
advc.appDeveloper = developer;
}
}
If you don't use a UINavigationController, you need to use this following code :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
// Get reference to the destination view controller
AppDetail *advcc = segue.destinationViewController;
advc.appTitel = name;
advc.appIcon = icon;
advc.detailAppName = detileName;
advc.appDescription = description;
}
}

Related

PrepareForSegue method crashes app iOS

I have a UIButton that, when I click on it, pushes another view controller, but it crashes here
textViewController.message = self.selectedMessage; with error set message.
This is my button with code and method where it crashes:
- (IBAction)image:(id)sender {
[self performSegueWithIdentifier:#"image" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showLogin"]) {
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
} else {
[segue.identifier isEqualToString:#"showImage"];
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
ImageViewController *imageViewController = (ImageViewController *)segue.destinationViewController;
imageViewController.message = self.selectedMessage;
}
}
How can I fix the PrepareForSegue method so I can go to next VC when I press button. I put exception break point and it crashes at imageViewController.message = self.selectedMessage;. when I comment that line out everything works fine.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showLogin"]) {
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
} else {
ImageViewController *imageViewController = (ImageViewController *)segue.destinationViewController;
[imageViewController setHidesBottomBarWhenPushed:YES];
imageViewController.message = self.selectedMessage;
}
}
Try this .
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showLogin"]) {
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
} else {
[segue.identifier isEqualToString:#"showImage"];
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
ImageViewController *imageViewController = (ImageViewController *)segue.destinationViewController;
imageViewController.message = self.selectedMessage;
}
}
had to change it to this for it to work
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showLogin"]) {
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
} else if ([segue.identifier isEqualToString:#"showImage"]){
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
ImageViewController *imageViewController = (ImageViewController *)segue.destinationViewController;
imageViewController.message = self.selectedMessage;
}
}

No responed after pressing UICollectionViewCell in the UICollectionViewController

when I come the part of sending image from UICollectionViewController to another ViewController here is View A:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Detail Segue"])
{
PhotoDetailViewController *PhotoDetailVC = segue.destinationViewController;
NSIndexPath *path = [[self.collectionView indexPathsForSelectedItems] lastObject];
Photo *selectedPhoto = self.photos[path.row];
PhotoDetailVC.photo = selectedPhoto;
}
}
and I receive it in the ViewController (View B) using:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.imageView.image= self.photo.image;
}
I think the problem is that you didn't call performSegueWithIdentifier:sender: in any place in code which actually responsible for triggering navigation not prepareForSegue:sender: which is called when preparing for the navigation
so I suggest to implement collectionView:didSelectItemAtIndexPath: as the following
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
Photo *selectedPhoto = self.photos[path.row];
// this line is the one that is responsible for navigation
[self performSegueWithIdentifier:#"Detail Segue" sender:selectedPhoto];
}
and modify prepareForSegue:sender: as the following
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Detail Segue"]) {
PhotoDetailViewController *PhotoDetailVC = segue.destinationViewController;
PhotoDetailVC.photo = sender;
}
}

Send captured image to another UIViewController with prepareForSegue

I'm trying to send the captured image to another UIViewController.
So when I press captured button, it's taking a photo and I can save the image in my camera roll. But when I want to see image in another view I can't see it.
This is my code :
- (IBAction)captureButton:(id)sender
{
[self.cameraViewController captureImageWithCompletionHander:^(id data) {
self.image = ([data isKindOfClass:[NSData class]]) ? [UIImage imageWithData:data] : data;
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
[self performSegueWithIdentifier:#"savingSegue" sender:self];
}];
}
And this is the prepare for segue method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = [[PhotoSaveViewController alloc] init];
[pvc.imageView setImage:self.image];
}
}
If your second view is a PhotoSaveViewController then replace your prepareForSeque method with this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = (PhotoSaveViewController *)segue.destinationViewController;
if (pvc)
[pvc.imageView setImage:self.image];
}
}
Indeed, it's not your job to instantiate the destination UIViewController, your UINavigationController have already done that.
I guess that imageView on PhotoSaveViewController it is outlet and it is not set at this moment. You should declare there some other property like
#property (nonatomic, retain) UIImage *inputImage;
and assign to that in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = segue.destinationViewController;
pvc.inputImage = self.image;
}
}
Then do the following in the PhotoSaveViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView.image = self.inputImage;
}
Another approach you can create it from code totally. Assign Storyboard ID to the PhotoSaveViewController and then do the following:
- (IBAction)captureButton:(id)sender {
[self.cameraViewController captureImageWithCompletionHander:^(id data) {
self.image = ([data isKindOfClass:[NSData class]]) ? [UIImage imageWithData:data] : data;
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
PhotoSaveViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"PhotoSaveViewControllerID"];
dvc.inputImage = self.image;
[self.navigationController pushViewController:dvc animated:YES];
}];
}

how to pass the image using storyboard in ios7

I cant able to pass the image from 1 view to another view in ios7.
in current view
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
BehindContributors *destViewController = segue.destinationViewController;
[destViewController setImage:#"1.jpg"];
}
in destination view
self.image1.image=self.image;
did you try imageNamed method on ÙIImage
like so;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
BehindContributors *destViewController = segue.destinationViewController;
[destViewController setImage:[UIImage imageNamed:#"1.jpg"]];
}
What are you trying to pass image name(i.e. NSString object) or image itself(i.e. UIImage object).
If you are passing image name then use following
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
BehindContributors *destViewController = segue.destinationViewController;
[destViewController setImageName:#"1.jpg"];
}
}
Destination view should have property imageName of NSString type
then use it as
self.imageView.image = [UIImage imageName:imageName];
If you are passing image then use following
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
BehindContributors *destViewController = segue.destinationViewController;
[destViewController setImageData:[UIImage imageNamed:#"1.jpg"]];
}
}
Destination view should have property imageData of UIImage type
then use it as
self.imageView.image = imageData;

ios Storyboard - Push a title onto the View Controller

Is this piece of code suppose to set the title on the ViewController I am connecting to?
The 2 UIViewControllers are connected via a push segue - the first one is embedded in a NavigationController.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"settingsSegue"])
{
self.navigationItem.title = [[NSString alloc] initWithFormat:#"Custom Title"];
}
}
It does not work for me, but the syntax is right.
Advance Thanks:-)
The above answer works for me with one exception...
Change
self.title = myTitle;
to
self.navigationItem.title = myTitle;
Set the title in the viewDidLoad of the destination viewcontroller using a property in the destination VC:
if ([[segue identifier] isEqualToString:#"settingsSegue"]) {
MyDestinationViewController *mdvc = segue.destinationViewController;
mdvc.myTitle = [[NSString alloc] initWithFormat:#"Custom Title"];
}
Then in the viewDidLoad event in MyDestinationViewController.h:
#property (nonatomic,strong) NSString *myTitle;
In MyDestinationViewController.m:
#synthesize myTitle;
And finally in viewDidLoad:
self.title = myTitle;
You can set the title directly in the segue as well, there is no need to go through a property:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"settingsSegue"]) {
segue.destinationViewController.navigationItem.title = #"Custom Title";
}
}
Or, what I needed, to set the title of the pushed view controller to the title of the table cell that was clicked:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"settingsSegue"]) {
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:myIndexPath];
segue.destinationViewController.navigationItem.title = cell.textLabel.text;
}
}

Resources