简体   繁体   English

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

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

I have been experimenting with some code in a sample project that uses xibs. 我一直在使用xibs的示例项目中试验一些代码。 In that project the below method takes a photo that was picked via UIImagePickerController and displays another VC modally initializing its scrollview with the image. 在该项目中,下面的方法获取通过UIImagePickerController选取的照片,并显示另一个VC模态初始化其与图像的滚动视图。

This works fine in the xib project but now that im integrating it into a storyboard project this section throws an exception: 这在xib项目中运行良好,但现在我将它集成到一个storyboard项目中,这部分引发了一个异常:

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

The error is: 错误是:

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

Which seems to be saying that its looking for the xib file that isnt there. 这似乎是在说它寻找不存在的xib文件。 I have created a mimicked storyboard version of the xib and hooked up all my actions and outlets the same. 我创建了一个模仿的xib故事板版本,并将我的所有动作和插座连接起来。

How do I display a storyboard VC in the below method, set the scrollview photo to nonRawImage, pass it the min and max zoom as is done below? 如何在下面的方法中显示故事板VC,将scrollview照片设置为nonRawImage,如下所示将最小和最大缩放传递给它?

Problem Method: 问题方法:

-(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
    }
}

Code from SSPhotoCropperViewController.m 来自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;

}

Ok ive got the method call changed to 好的,我已将方法调用更改为

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

But Im not sure what to do with the below: 但我不知道如何处理以下内容:

- (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;
}

You should give your view controller an identifier within the storyboard, then instantiate it as follows: 您应该在故事板中为视图控制器提供一个标识符,然后按如下方式对其进行实例化:

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

The identifier can be set using Storyboard ID from within the Identity Inspector. 可以使用Identity Inspector中的Storyboard ID设置标识符。

Make sure you set the SSPhotoCropperViewController's Identifier in the storyboard to SSPhotoCropperViewController. 确保将故事板中的SSPhotoCropperViewController标识符设置为SSPhotoCropperViewController。

Change your method: 改变你的方法:

- (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.

相关问题 能够从Storyboard和ViewController加载xib - Be able to load xib from both Storyboard and ViewController 将Swift ViewController添加到旧项目中。 故事板问题 - Adding Swift ViewController to an old project. Storyboard problems 在现有的XIB项目中添加情节提要 - Add a Storyboard in a existing XIB Project 将Storyboard,XIB和自定义代码组合在一个项目中是可以的 - It is okay to combine Storyboard, XIB and custom code in one project 如何从 xib 到 storyboard 的视图控制器? - How to segue from a xib to a viewcontroller from a storyboard? 我有一个使用 XIB 文件的音乐播放器项目,如何将它们转换为故事板/视图控制器? - I have a music player project that is using XIB files, how to convert them to storyboard/Viewcontroller? 从ViewController(故事板)调用NavigationController(xib) - Call NavigationController (xib) from ViewController (storyboard) ViewController链接到其他项目的故事板吗? - ViewController linked to storyboard of a different project? 更新Xcode项目时出错。 从Xamarin.Studio打开XIB时参数无效 - Error updating Xcode project. Invalid parameter when open XIB from Xamarin.Studio 从Storyboard ViewController返回到以前使用的.xib ViewController - Going back from Storyboard ViewController to a previously used .xib ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM