简体   繁体   English

View Controller和UIView之间的简单委托无法正常工作

[英]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. 我已经做了100次,觉得自己只是在想些愚蠢的事情。 Here is the code how come this isn't working? 这是代码,这怎么不起作用?

Source ViewController's.h: 源ViewController.h:

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

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

Source viewController.m: 源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 目标视图的.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? 我搞砸了不是吗?

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. 我想说的是,如果您使用的是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;

It looks like in your ProfileViewController.h you are missing a declaration of ImageViewer 看起来在您的ProfileViewController.h中,您缺少ImageViewer的声明

@property (nonatomic, weak)  ImageViewer *imageViewer;

And in your ProfileViewController.m you need to set the delegate. 在您的ProfileViewController.m中,您需要设置委托。

self.imageViewer.delegate = self;

You need to set the destination delegate to self. 您需要将目标委托设置为self。

ImageViewer *imageViewer = [[ImageViewer alloc] initWithFrame:superView.view.bounds];
imageViewer.delegate = self;

When you have a UIView that loads its self after you hace initialized the object and called the load method you set the delegate by using. 在初始化对象并调用load方法后,如果有一个UIView加载其自身,则可以使用设置委托。

imageViewer.delegate = superView;

Where imageView is the newly initialized view not to be confused with the object self (self.delegate = superView isn't the same) and where superView is the is the parent view creating the UIView. 其中imageView是新初始化的视图,不要与对象self混淆(self.delegate = superView不相同),其中superView是创建UIView的父视图。

Now my last question would be is there a better way to do what I have done? 现在我的最后一个问题是,有没有更好的方法来做我所做的事情? I feel like I have over complicated the situation. 我觉得我已经把情况弄复杂了。

I am having the exact same problem in exactly same scenario. 我在完全相同的情况下遇到完全相同的问题。 After doing some brain storming i found the issue and resolved successfully. 经过一些头脑风暴后,我发现了问题并成功解决。

The problem is scope of UIView object. 问题是UIView对象的范围。 So to resolve this create a global object of view in .h file and use it as your target object. 因此,要解决此问题,请在.h文件中创建一个全局视图对象,并将其用作目标对象。

@interface profilePageViewController : parentViewController<PhotoUploadViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,ConnectionsDelegate>
{
    PhotoUploadView *photoUploadView;

And it will work very sure, You can also try to by printing the object to and you will find two different objects.One at time of init and other in delegate call. 这将非常肯定,您也可以尝试通过将对象打印到而找到两个不同的对象。一个在初始化时,另一个在委托调用中。

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

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