Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Delegate Between View Controller and UIView Isn't Working

So I am using a simple delegate to access a function from a main view controller when a button is pressed on a subview that is generated as an overlay on top of the main view. For some reason the function that is defined in the source view controller isn't being executed. I have done this 100 times and feel like I am just missing something stupid. Here is the code how come this isn't working?

Source ViewController's.h:

#import <UIKit/UIKit.h>
#import "ProfileSettingsViewController.h"
#import "ImageViewer.h"

@interface ProfileViewController : UIViewController <UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, subViewDelegate>

Source viewController.m:

#import <Parse/Parse.h>
#import <QuartzCore/QuartzCore.h>
#import "ImageViewer.h"
#import "ProfileViewController.h"

@interface ProfileViewController () <UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, subViewDelegate>

@end

//where the ImageViewer object is defined    
@implementation ProfileViewController
{
    NSMutableArray *dataArray;
    ImageViewer *loadImageViewer;
}

//where the UIView is initialized
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer view] == _profilePic)
    {
        loadImageViewer = [ImageViewer alloc];
        [loadImageViewer loadImageIntoViewer:self imageToLoad:_profilePic.image];
    }
    else if ([gestureRecognizer view] == _coverPhoto)
    {

    }
}

Destination View's.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@protocol subViewDelegate
-(void)photoFromSubview:(id)sender;
@end

@interface ImageViewer : UIView

@property (nonatomic, assign) id <subViewDelegate> delegate;
@property (nonatomic, copy) UIImage *mainImage;
@property (nonatomic, copy) UIViewController *parentView;

- (void)fromCameraRoll;
- (void)takePhoto;
- (void)removeImageViewer;
- (void)captureImage:(id)sender;
- (void)uploadPhoto:(id)sender;
- (UIImage *)addBackground;
- (ImageViewer *)loadImageIntoViewer:(UIViewController *)superView imageToLoad:(UIImage *)imageToLoad;

@end

Destination View's.m

#import "ImageViewer.h"

@implementation ImageViewer : UIView

//synthesize properties
@synthesize mainImage = _mainImage;
@synthesize parentView = _parentView;

//initialize the image viewer
- (ImageViewer *)loadImageIntoViewer:(UIViewController *)superView imageToLoad:(UIImage *)imageToLoad
{
    //create a new view with the same frame size as the superView
    ImageViewer *imageViewer = [[ImageViewer alloc] initWithFrame:superView.view.bounds];
    _mainImage = imageToLoad;
    _parentView = superView;

    //if something's gone wrong, abort!
    if(!imageViewer)
    {
        return nil;
    }

    //add all components and functionalities to the program
    UIImageView *background = [[UIImageView alloc] initWithImage:[imageViewer addBackground]];
    background.alpha = 0.85;
    [imageViewer addSubview:background];
    [imageViewer addSubview:[imageViewer addImageToView:_mainImage superView:superView.view]];
    [imageViewer addSubview:[imageViewer addButtonToView:(superView.view.bounds.origin.x + 10.0) yPos:(superView.view.bounds.origin.x + 10.0) buttonAction:@selector(back:) buttonTitle:@"Back"]];
    [imageViewer addSubview:[imageViewer addButtonToView:(superView.view.bounds.origin.x + 10.0) yPos:((superView.view.center.y/2) + 270.0) buttonAction:@selector(captureImage:) buttonTitle:@"Camera"]];
    [imageViewer addSubview:[imageViewer addButtonToView:(superView.view.bounds.origin.x + 105.0) yPos:((superView.view.center.y/2) + 270.0) buttonAction:@selector(uploadPhoto:) buttonTitle:@"Upload"]];
    [imageViewer addSubview:[imageViewer addButtonToView:(superView.view.bounds.origin.x + 200.0) yPos:((superView.view.center.y/2) + 270.0) buttonAction:@selector(rotatePhoto:) buttonTitle:@"Rotate"]];
    [superView.view addSubview:imageViewer];

    return imageViewer;
}

//call delegate method
- (void)captureImage:(id)sender
{
    [self.delegate photoFromSubview:self];
}

I have screwed something up haven't I?

like image 513
ScottOBot Avatar asked May 08 '13 22:05

ScottOBot


People also ask

How to represent uiactivityindicatorview in SwiftUI?

Here is an example of how we can represent UIActivityIndicatorView in SwiftUI: Let’s display the spinner in ContentView: If you run this code, you’ll see the following result: Every SwiftUI view that represents a UIKit view or view controller undergoes following steps that conclude its lifecycle:

Does SwiftUI integrate with UIView?

Although SwiftUI has a limited set of tools, their lack is compensated by seamless integration of SwiftUI views with UIView and UIViewController. In this article we’ll cover:

How to integrate a view controller into SwiftUI view hierarchy?

The process of integrating a view controller into SwiftUI view hierarchy is next: Declare a SwiftUI view that conforms to UIViewControllerRepresentable. Implement the two required methods makeUIViewController () and updateUIViewController (). Let’s get started and wrap a font picker into a SwiftUI view:

How do I create an instance of a view controller?

Create an instance of a view controller or a view. Use the information from context to wire up the coordinator, and set up the initial appearance of your view controller or view. Conveniently, we can think of this method as viewDidLoad.


1 Answers

The best way to debug this is to use breakpoints and see what gets invoked, and what doesn't (and check if your delegate is properly set). Of the top of my head, I would say you either forgot the set the delegate or possibly an outlet if you're using IB.

Edit: Ok, It seems to me now that your delegate property is in the wrong class. That property should be in your subview, and when creating that subview from your superview, you should set the delegate properly, something like this:

mySubview.delegate = self;
like image 53
Adis Avatar answered Sep 23 '22 06:09

Adis