繁体   English   中英

从导入到Storyboard项目的xib项目加载ViewController代码。

[英]Load a ViewController code from a xib project imported to a storyboard project.

我一直在使用xibs的示例项目中试验一些代码。 在该项目中,下面的方法获取通过UIImagePickerController选取的照片,并显示另一个VC模态初始化其与图像的滚动视图。

这在xib项目中运行良好,但现在我将它集成到一个storyboard项目中,这部分引发了一个异常:

SSPhotoCropperViewController *photoCropper =
        [[SSPhotoCropperViewController alloc] initWithPhoto:nonRawImage
                                                   delegate:self
                                                     uiMode:SSPCUIModePresentedAsModalViewController
                                            showsInfoButton:YES]; 

错误是:

'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/24FB2532-BB1D-4573-8551-386FAA154022/BubbleBoss.app> (loaded)' with name 'SSPhotoCropperViewController''

这似乎是在说它寻找不存在的xib文件。 我创建了一个模仿的xib故事板版本,并将我的所有动作和插座连接起来。

如何在下面的方法中显示故事板VC,将scrollview照片设置为nonRawImage,如下所示将最小和最大缩放传递给它?

问题方法:

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];

    [self dismissViewControllerAnimated:NO completion:nil];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = info[UIImagePickerControllerOriginalImage];

        UIImage *nonRawImage=[self scaleAndRotateImage:image];

        SSPhotoCropperViewController *photoCropper =
        [[SSPhotoCropperViewController alloc] initWithPhoto:nonRawImage
                                                   delegate:self
                                                     uiMode:SSPCUIModePresentedAsModalViewController
                                            showsInfoButton:YES];
        [photoCropper setMinZoomScale:0.25f];
        [photoCropper setMaxZoomScale:3.00f];
        UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:photoCropper];
        [self presentViewController:nc animated:YES completion:nil];



        //  photoPreviewImageView.image = image;
        if (_newMedia)
            UIImageWriteToSavedPhotosAlbum(image,
                                           self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }
}

来自SSPhotoCropperViewController.m代码

- (id) initWithPhoto:(UIImage *)aPhoto
            delegate:(id<SSPhotoCropperDelegate>)aDelegate
{
    return [self initWithPhoto:aPhoto
                      delegate:aDelegate
                        uiMode:SSPCUIModePresentedAsModalViewController
               showsInfoButton:YES];
}

- (id) initWithPhoto:(UIImage *)aPhoto
            delegate:(id<SSPhotoCropperDelegate>)aDelegate
              uiMode:(SSPhotoCropperUIMode)uiMode
     showsInfoButton:(BOOL)showsInfoButton
{
    if (!(self = [super initWithNibName:@"SSPhotoCropperViewController" bundle:nil])) {
        return self;
    }

    self.photo = aPhoto;
    self.delegate = aDelegate;
    _uiMode = uiMode;
    _showsInfoButton = showsInfoButton;

    self.minZoomScale = 0.5f;
    self.maxZoomScale = 3.0f;

    self.infoMessageTitle = @"In order to crop the photo";
    self.infoMessageBody = @"Use two of your fingers to zoom in and out the photo and drag the"
                           @" green window to crop any part of the photo you would like to use.";
    self.photoCropperTitle = @"Crop Photo";

    return self;
}

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.photo = nil;
        self.delegate = nil;
    }
    return self;
}


- (void) didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction) infoButtonTapped:(id)sender
{
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:self.infoMessageTitle
                                                 message:self.infoMessageBody
                                                delegate:nil
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil];
    [av show];

}


#pragma -
#pragma mark - View lifecycle

- (void) viewDidLoad
{
    [super viewDidLoad];

    //
    // setup view ui
    //
    UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                        target:self
                                                                        action:@selector(saveAndClose:)];
    self.navigationItem.rightBarButtonItem = bi;


    if (_uiMode == SSPCUIModePresentedAsModalViewController) {
        bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                           target:self
                                                           action:@selector(cancelAndClose:)];
        self.navigationItem.leftBarButtonItem = bi;

    }

    if (!_showsInfoButton) {
        [self.infoButton setHidden:YES];
    }

    self.title = self.photoCropperTitle;

    //
    // photo cropper ui stuff
    //
    [self setScrollViewBackground];
    [self.scrollView setMinimumZoomScale:self.minZoomScale];
    [self.scrollView setMaximumZoomScale:self.maxZoomScale];

    [self.cropRectangleButton addTarget:self
                                 action:@selector(imageTouch:withEvent:)
                       forControlEvents:UIControlEventTouchDown];
    [self.cropRectangleButton addTarget:self
                                 action:@selector(imageMoved:withEvent:)
                       forControlEvents:UIControlEventTouchDragInside];

    if (self.photo != nil) {
        [self loadPhoto];
    }
}

- (void) viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}


#pragma -
#pragma UIScrollViewDelegate Methods

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}


#pragma -
#pragma Private Methods

- (void) loadPhoto
{
    if (self.photo == nil) {
        return;
    }

    CGFloat w = self.photo.size.width;
    CGFloat h = self.photo.size.height;
    CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
    self.scrollView.contentSize = imageViewFrame.size;

    UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
    iv.image = self.photo;
    [self.scrollView addSubview:iv];
    self.imageView = iv;

}

好的,我已将方法调用更改为

SSPhotoCropperViewController *photoCropper =
    [[SSPhotoCropperViewController alloc] initWithPhoto:photo
                                               delegate:self
                                                 uiMode:SSPCUIModePresentedAsModalViewController
                                        showsInfoButton:YES];
    [photoCropper setMinZoomScale:0.25f];
    [photoCropper setMaxZoomScale:3.00f];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:photoCropper];

但我不知道如何处理以下内容:

- (id) initWithPhoto:(UIImage *)aPhoto
            delegate:(id<SSPhotoCropperDelegate>)aDelegate
              uiMode:(SSPhotoCropperUIMode)uiMode
     showsInfoButton:(BOOL)showsInfoButton
{
   if (!(self = [super initWithNibName:@"SSPhotoCropperViewController" bundle:nil])) {
       return self;
    }

    self.photo = aPhoto;
    self.delegate = aDelegate;
    _uiMode = uiMode;
    _showsInfoButton = showsInfoButton;

    self.minZoomScale = 0.5f;
    self.maxZoomScale = 3.0f;

    self.infoMessageTitle = @"In order to crop the photo";
    self.infoMessageBody = @"Use two of your fingers to zoom in and out the photo and drag the"
                           @" green window to crop any part of the photo you would like to use.";
    self.photoCropperTitle = @"Crop Photo";

    return self;
}

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.photo = nil;
        self.delegate = nil;
    }
    return self;
}

您应该在故事板中为视图控制器提供一个标识符,然后按如下方式对其进行实例化:

YourViewControllerClass *viewController =
             [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]   
                       instantiateViewControllerWithIdentifier:@"ViewController"];

可以使用Identity Inspector中的Storyboard ID设置标识符。

确保将故事板中的SSPhotoCropperViewController标识符设置为SSPhotoCropperViewController。

改变你的方法:

- (id) initWithPhoto:(UIImage *)aPhoto
            delegate:(id<SSPhotoCropperDelegate>)aDelegate
              uiMode:(SSPhotoCropperUIMode)uiMode
     showsInfoButton:(BOOL)showsInfoButton
{
    //if (!(self = [super initWithNibName:@"SSPhotoCropperViewController" bundle:nil])) {
    if (!(self = [[UIStoryboard storyboardWithName:@"YourStoryBoardName" bundle:nil]   
                   instantiateViewControllerWithIdentifier:@"SSPhotoCropperViewController"])) {
        return self;
    }

    self.photo = aPhoto;
    self.delegate = aDelegate;
    _uiMode = uiMode;
    _showsInfoButton = showsInfoButton;

    self.minZoomScale = 0.5f;
    self.maxZoomScale = 3.0f;

    self.infoMessageTitle = @"In order to crop the photo";
    self.infoMessageBody = @"Use two of your fingers to zoom in and out the photo and drag the"
                           @" green window to crop any part of the photo you would like to use.";
    self.photoCropperTitle = @"Crop Photo";

    return self;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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