因此,当在子视图上按下按钮时,我使用一个简单的委托从主视图控制器访问一个功能,该子视图生成为主视图顶部的叠加层。由于某种原因,源视图控制器中定义的函数没有被执行。我已经这样做了 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];
}
我搞砸了,不是吗?