0

I am attempting to pass a variable between to view controllers and then show the new view

I have implemented the following code to pass a variable (without using segue)

    viewCameraViewController *viewCamera = [[viewCameraViewController alloc] initWithNibName:@"viewCameraViewController" bundle:nil];
    viewCamera.str1 = self.str;
    [self.navigationController pushViewController:viewCamera animated:YES];

and then this to show the view

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"cameraView"];

    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:vc animated:YES completion:NULL];

however when the new view loads the variable is null

here is my viewCamera class

//
//  viewCameraViewController.m
//  WebView
//
//  Created by Admin on 31/10/2015.
//  Copyright (c) 2015 Admin. All rights reserved.
//

#import "viewCameraViewController.h"



@interface viewCameraViewController ()

@end

@implementation viewCameraViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.\

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)takePhoto:(UIButton *)sender {

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"Device has no camera"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles:nil];
        [myAlertView show];

    } else {

    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController: picker animated:YES completion:NULL];
    }
}

-(IBAction)selectPhoto:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController: picker animated:YES completion:NULL];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];


}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}



@end

im assuming that this is caused by initialising two instances of the class, one called viewCamera and the other called VC.

can anyone help identify where i am going wrong here and point me in the right direction.

Thanks in advance

4
  • you expect that vc and viewCamera to be the same object ? Commented Nov 8, 2015 at 10:49
  • no i dont expect them to be the same thing, however i cant make the viewCamera Display but i can pass the variable, and the opposite for vc Commented Nov 8, 2015 at 10:51
  • what is the class of"cameraView" ? Commented Nov 8, 2015 at 10:53
  • edited the question to include copy of viewCamera class Commented Nov 8, 2015 at 10:57

1 Answer 1

1

If you have a View Controller that is defined in Interface Builder, you don't want to instantiate it with alloc/init, but rather with the method used in your second snippet. Make sure that you have set the class of your view controller in interface builder, and then cast it to your class to set the variable.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
YourViewControllerClass *vc = (YourViewControllerClass *)[storyboard instantiateViewControllerWithIdentifier:@"cameraView"];

vc.yourStringVariable = self.str;
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];

Note that presentViewController and pushViewController are two different ways of displaying a view controller. The first one presents it modally, the other one pushes it to a navigation controller. You don't want both of these calls.


Update after your update: so, YourViewControllerClass is viewCameraViewController - however, I'd point out that's an oddly named class; class names should start with upper case. Name it CameraViewController instead, is my suggestion. And don't use initWithNib: if you have your view controller defined in a storyboard, just remove that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.