簡體   English   中英

touches開始在以編程方式創建的視圖和圖像視圖上不起作用

[英]touchesBegan not functioning on programmatically created view and imageview

我先在另一個視圖控制器B的viewControllerA中以編程方式創建視圖和imageview,然后再轉到viewControllerB。 我必須這樣做,因為我正在使用imagePickerController中的圖像。 但是,這樣做,touchesBegan在viewControllerB中不起作用

我已經在xib的屬性檢查器中以及通過編程方式將UserInteractionEnabled設置為true / yes了!

我的xib只是一張空白畫布,只有一個視圖。 我用連接檢查器嘗試了不同的設置,但還是沒有運氣。

這是我的代碼。

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");

:

您做錯了很多事情:

  1. 不要將視圖控制器推到UIImagePickerController 如果希望以模態形式展示它(希望如此),則需要將其關閉(在viewControllerA中),然后將新的視圖控制器(viewControllerB)推到自己的導航控制器上。 您使用此高度自定義的導航控制器的事實很可能是沒有調用touchesBegan原因。
  2. 為什么要手動為viewControllerB創建視圖? 如果您是從XIB文件中加載文件,那么它將為您准備好一個完美的視圖。 並且不要在viewControllerB.h中定義view屬性,它已經在UIViewController.h中定義。
  3. 不要忘記在viewControllerB.m中調用[super viewDidLoad] 這是一個經典錯誤,將引起您很多頭痛。

編輯:

我個人會做的就是在viewControllerB上添加如下方法:

viewControllerB.h

@interface SSViewController : UIViewController
{
   - (void)setImage:(UIImage *)image;
}
@end

viewControllerB.m

@implementation SSViewController
{
   - (void)setImage:(UIImage *)image
   {
      // In case the image view already exists, remove it first.
      [_imageView removeFromSuperview];
      _imageView = [[UIImageView alloc] initWithImage:image];
      _imageView.frame = CGRectMake(20, 20, 280, 280);
      [self.view addSubview:_imageView];
   }
}
@end

這樣,您可以在viewControllerA.m中創建viewControllerB.m並調用此setImage:方法,以使viewControllerB完成所有工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM