View Controller和UIView之间的简单委托不起作用

时间:2013-05-08 22:01:48

标签: iphone ios ios4 delegates xcode4.2

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

Source ViewController's.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];
}

我搞砸了什么不是吗?

5 个答案:

答案 0 :(得分:5)

调试此方法的最佳方法是使用断点并查看调用的内容以及不调用的内容(并检查您的委托是否已正确设置)。在我的头脑中,我会说你忘记了代表的设置,或者如果你正在使用IB,你可能会忘记。

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

mySubview.delegate = self;

答案 1 :(得分:0)

在您的ProfileViewController.h中,您似乎缺少ImageViewer的声明

@property (nonatomic, weak)  ImageViewer *imageViewer;

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

self.imageViewer.delegate = self;

答案 2 :(得分:0)

您需要将目标代理设置为self。

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

答案 3 :(得分:0)

如果你有一个UIView在你初始化对象并调用load方法后加载它自己,你可以使用设置委托。

imageViewer.delegate = superView;

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

现在我的最后一个问题是,有更好的方法来完成我的工作吗?我觉得我已经过度复杂了。

答案 4 :(得分:0)

我在完全相同的情况下遇到完全相同的问题。 在做了一些脑力激荡之后,我发现了问题并成功解决了。

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

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

它将非常有把握,你也可以尝试通过打印对象来找到两个不同的对象。一个在init时,另一个在委托调用中。