【问题标题】:Load a ViewController code from a xib project imported to a storyboard project.从导入到情节提要项目的 xib 项目中加载 ViewController 代码。
【发布时间】:2014-01-05 09:43:05
【问题描述】:

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

这在 xib 项目中运行良好,但现在我将它集成到情节提要项目中,这部分会引发异常:

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,将滚动视图照片设置为 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;
}

【问题讨论】:

  • 你为什么要分配一个新的 UINavigationController?您在裁剪器中使用推送到更多视图控制器?请从 SSPhotoCropperViewController 中添加 initWithPhoto 方法
  • 我在上面添加了更多代码。谢谢。

标签: ios iphone objective-c xcode uiviewcontroller


【解决方案1】:

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

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

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

【讨论】:

  • 这非常适合加载视图。谢谢你。如何像原始方法一样将图像、最大和最小缩放传递给它? (我在上面添加了更多代码,抱歉,我没有意识到会有更多代码)
  • @DelightedD0D 我在下面给你写了一个完整的答案
【解决方案2】:

确保将情节提要中的 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;
}

【讨论】:

  • 接受了 ColinE 的回答,因为他首先回答了我最初的问题。但如果没有你的帮助,我无法让它工作,谢谢! +1
猜你喜欢
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 2012-09-14
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
相关资源
最近更新 更多