在视图控制器和显示器之间传递变量



我正试图在视图控制器之间传递一个变量,然后显示新的视图

我已经实现了以下代码来传递一个变量(不使用segue)

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

然后显示视图

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"cameraView"];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:vc animated:YES completion:NULL];

然而,当新视图加载时,变量为空

这是我的视图相机类

//
//  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

我假设这是由初始化类的两个实例引起的,一个称为viewCamera,另一个名为VC.

有人能帮我找出哪里出了问题,并给我指明正确的方向吗。

提前感谢

如果您有一个在Interface Builder中定义的视图控制器,您不想用alloc/init来实例化它,而是用第二个代码段中使用的方法来实例化它。确保已在接口生成器中设置了视图控制器的类,然后将其强制转换为类以设置变量。

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];

注意,presentViewControllerpushViewController是显示视图控制器的两种不同方式。第一个以模式显示,另一个将其推送到导航控制器。这两个电话你都不要。


更新后更新:所以,YourViewControllerClass就是viewCameraViewController——然而,我要指出的是,这是一个名称奇怪的类;类名应该以大写字母开头。我建议将其命名为CameraViewController。如果在情节提要中定义了视图控制器,请不要使用initWithNib:,只需删除即可。

最新更新