在View Controller和Display

时间:2015-11-08 10:44:12

标签: ios objective-c iphone xcode

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

我已经实现了以下代码来传递变量(不使用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];

但是当新视图加载变量时为null

这是我的viewCamera类

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

任何人都可以帮助确定我在哪里出错,并指出我正确的方向。

提前致谢

1 个答案:

答案 0 :(得分:1)

如果您有一个在Interface Builder中定义的View Controller,您不希望使用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是显示视图控制器的两种不同方式。第一个以模态方式呈现,另一个将其推送到导航控制器。你不想要这两个电话。

更新后更新:所以YourViewControllerClassviewCameraViewController - 但是,我会指出这是一个奇怪的命名类;类名应以大写字母开头。将其命名为CameraViewController,是我的建议。如果您在故事板中定义了视图控制器,请不要使用initWithNib:,只需删除它即可。