viewControllerA で別の viewControllerB のビューと imageview をプログラムで作成してから、viewControllerB に移動します。imagePickerController の画像を使用しているため、これを行う必要があります。ただし、これを行うと、viewControllerB で touchesBegan が機能しなくなります。
xib の属性インスペクターとプログラムでどこでも UserInteractionEnabled を true/yes に設定しました。
私のxibは、1つのビューを持つ単なる空白のキャンバスです。接続インスペクターでさまざまな設定を試しましたが、まだうまくいきません。
これが私のコードです。
viewControllerA.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");
: