简体   繁体   中英

Pass Variable Between View Controller and Display

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM