我在 viewControllerA 中为另一个 viewControllerB 以编程方式创建视图和图像视图,然后转到 viewControllerB。我必须这样做,因为我使用的是 imagePickerController 中的图像。但是,通过这样做,touchesBegan 在 viewControllerB 中不起作用
我在 xib 的属性检查器和任何地方都以编程方式将 UserInteractionEnabled 设置为 true/yes!
我的 xib 只是一张空白画布,只有一个视图。我用连接检查器尝试了不同的设置,但仍然没有运气。
这是我的代码。
视图控制器A.m
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
view = [[UIView alloc] initWithFrame:screenRect];
image = [info valueForKey:UIImagePickerControllerEditedImage];
SSViewController *psView = [[SSViewController alloc] initWithNibName:@"SSViewController" bundle:nil];
psView.view = [[UIView alloc] initWithFrame:screenRect];
[psView.view setUserInteractionEnabled:YES];
[picker pushViewController:psView animated:YES];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(20, 20, 280, 280);
[imageView setUserInteractionEnabled:YES];
[psView.imageView setUserInteractionEnabled:YES];
[psView.view addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:psView action:@selector(endPS:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"done" forState:UIControlStateNormal];
button.frame = CGRectMake(140.0, 330.0, 80.0, 25.0);
[psView.view addSubview:button];
}
viewControllerB.h (SSViewController.h)
@interface SSViewController : UIViewController
{
IBOutlet UIImageView *imageView;
IBOutlet UIView *view;
}
@property (nonatomic,strong) IBOutlet UIImageView *imageView;
@property (nonatomic,strong) IBOutlet UIImage *image;
@property (nonatomic, strong) IBOutlet UIView *view;
- (IBAction)endPS:(id)sender;
@end
viewControllerB.m (SSViewController.m)
@implementation SSViewController
@synthesize imageView;
@synthesize image;
@synthesize view;
:
:
- (void)viewDidLoad
{
[view setUserInteractionEnabled:YES];
[imageView setUserInteractionEnabled:YES];
:
:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan");
: