5

因此,当在子视图上按下按钮时,我使用一个简单的委托从主视图控制器访问一个功能,该子视图生成为主视图顶部的叠加层。由于某种原因,源视图控制器中定义的函数没有被执行。我已经这样做了 100 次,感觉就像我错过了一些愚蠢的东西。这是代码为什么这不起作用?

源 ViewController 的.h:

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

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

源 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)
    {

    }
}

目标视图的.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

目的地视图的.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];
}

我搞砸了,不是吗?

4

5 回答 5

5

调试此问题的最佳方法是使用断点并查看调用了什么以及不调用什么(并检查您的委托是否设置正确)。在我的脑海中,我想说你要么忘记了代表的集合,要么如果你使用的是 IB,可能会忘记一个出口。

编辑:好的,现在在我看来,您的委托属性在错误的类中。该属性应该在您的子视图中,并且从您的超级视图创建该子视图时,您应该正确设置委托,如下所示:

mySubview.delegate = self;
于 2013-05-08T22:06:36.950 回答
0

您需要将目标委托设置为自己。

ImageViewer *imageViewer = [[ImageViewer alloc] initWithFrame:superView.view.bounds];
imageViewer.delegate = self;
于 2013-05-08T23:15:46.523 回答
0

当您有一个 UIView 在您初始化对象并调用 load 方法后加载其自身时,您可以使用设置委托。

imageViewer.delegate = superView;

其中 imageView 是新初始化的视图,不要与对象 self 混淆(self.delegate = superView 不一样),而 superView 是创建 UIView 的父视图。

现在我的最后一个问题是有没有更好的方法来做我所做的事情?我觉得我把情况复杂化了。

于 2013-05-09T12:26:18.197 回答
0

我在完全相同的情况下遇到了完全相同的问题。经过一番头脑风暴后,我发现了问题并成功解决。

问题是 UIView 对象的范围。因此,要解决此问题,请在 .h 文件中创建一个全局视图对象并将其用作目标对象。

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

它会非常肯定地工作,您也可以尝试通过将对象打印到,您会发现两个不同的对象。一个在 init 时,另一个在委托调用中。

于 2013-12-19T11:09:35.993 回答
0

看起来在您的 ProfileViewController.h 中您缺少 ImageViewer 的声明

@property (nonatomic, weak)  ImageViewer *imageViewer;

在您的 ProfileViewController.m 中,您需要设置委托。

self.imageViewer.delegate = self;
于 2013-05-08T22:23:08.940 回答