Question

I have a button on a view controller that presents a modal viewcontroller. In the init method of the viewcontroller I am initialising a UIImage view, on top of it a custom one and on top of the last one i have a button (finish). When I press the finish button I want to dismiss the modal view controller and also I want to pop the initial view controller (the one that presented the modal one). The problem is that I am receiving the following error:

2014-04-21 19:31:16.223 Signature[5343:60b] -[UIImageView setEnableSetNeedsDisplay:]: unrecognized selector sent to instance 0x16d73550

Here is the relevant code:

This is the action performed when I instantiate the modal view controller:

- (void) onSignButtonPressed: (id)sender
{

    PPSSignatureViewController *ppsSignatureViewController = [[[PPSSignatureViewController alloc] initWithNibName:nil bundle:nil documentViewController:self] autorelease];

    ppsSignatureViewController.modalPresentationStyle = UIModalPresentationFullScreen;

    [self presentViewController:ppsSignatureViewController animated:NO completion:^{
        printf("done!!!");
    }];

    [self.navigationController popViewControllerAnimated:YES];
}

This is the code that gets executed when the modal view controller is instantiated:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil documentViewController:(MuDocumentController*)dController
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
        PPSSignatureView *signatureView =
        [[[PPSSignatureView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:context] autorelease];
        signatureView.opaque = NO;
        signatureView.backgroundColor = [UIColor clearColor];

        UIGraphicsBeginImageContext(dController.view.bounds.size);
        [dController.view drawViewHierarchyInRect:dController.view.bounds afterScreenUpdates:YES];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        UIImageView *backView = [[UIImageView alloc] initWithImage:image];

        signatureView.frame = backView.bounds;

        UIButton *finishSignatureButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [finishSignatureButton addTarget:self
                                  action:@selector(finishSignatureButtonPressed:)
                        forControlEvents:UIControlEventTouchUpInside];
        [finishSignatureButton setTitle:@"Salveaza semnatura" forState:UIControlStateNormal];
        finishSignatureButton.frame = CGRectMake(signatureView.frame.size.width - 210, signatureView.frame.size.height - 50, 200, 40.0);
        finishSignatureButton.backgroundColor = [UIColor whiteColor];

        [backView addSubview:finishSignatureButton];

        self.view = backView;

        self.view.backgroundColor = [UIColor clearColor];

        [backView release];
        [finishSignatureButton release];
        [signatureView release];
        [image release];
    }

    return self;
}

And finally this is the code triggered by the finishSignatureButton:

- (void) finishSignatureButtonPressed: (id)sender
{
    printf("DOne");
    [self.presentingViewController dismissViewControllerAnimated:YES completion: nil];  
}

I need to know what am I doing wrong? I am a noob at objective C programming. I tried dismissing the view in multiple ways, none of them with success... Also, I do not know if what I am doing is correct and if the order is ok. It seems to matter the order of operation in the init method of the signature view controller. Any help will be much appreciated.

Was it helpful?

Solution

The biggest problem I see is this line:

self.view = backView;

You are attempting to replace your view controller's view property with a UIImageView - this is not okay. The view controller's view property should not be manipulated in this way. You should add subviews to it, not replace it entirely. Your application is attempting to invoke a method (setEnableSetNeedsDisplay:) on (what I assume to be) the view controller's view. Since you have replaced this with an instance of UIImageView, you're getting an exception as UIImageView does not implement this method.

Instead, do something like this:

[self.view addSubview:backView];

Note that you can also add any other subviews to the view in this way, so your button need not be a subview of the image, but it can be a direct subview of the view controller's view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top