【问题标题】:How to select UIImage in storyboard from View Controller如何从视图控制器中选择情节提要中的 UIImage
【发布时间】:2017-04-28 23:49:54
【问题描述】:

我目前正在尝试选择我在视图控制器的情节提要中创建的UIImageView

在objective c中有没有相当于android的findViewbyId的方法?

【问题讨论】:

  • 在 iOS 中,您需要为此使用标签。可以使用“viewwithtag”检索这些
  • iOS 中的概念不同。您想将其设为IBOutlet。去谷歌上查询。要使其“可选择”,请设置一个 `TapgestureRecognizer.谷歌一下。
  • @shallowThought 谢谢!我实际上正在制作动画,但我以后肯定需要知道这一点。我在 iOS 中工作的最大问题是我不知道任何东西的术语,这让我很难找到答案。这就是为什么我通过 SO 提出了这么简单的问题。
  • 这个概念在 iOS 中是一样的。您可以使用 viewWithTag:20 方法。
  • @Airagale:在这种情况下,谷歌搜索"iOS equivalent to XYAndroid feature"

标签: ios objective-c uiimageview storyboard iboutlet


【解决方案1】:

为什么不只是 make an IBOutlet 用于图像视图?

然后你可以通过类似的方式访问它

self.imageView

其中 imageView 是您命名的插座。

【讨论】:

  • 谢谢!我是来自 android dev 的 iOS 新手,这是最简单的答案。这不像 android 的 findViewById 但它最终的工作原理是一样的。我正在使用 [self imageView];避免警告“未使用属性访问结果 - getter 不应用于副作用”
【解决方案2】:

在您的UIImageView 上放置一个UIButton

【讨论】:

  • 你能解释一下这如何让我在视图控制器中操作 UIImageView 吗?
  • 如果你想操作它你应该输出它,我以为你想检测触摸。
  • 对不起,我应该指定的。查看 shallowthought 对这个问题的评论——我可能会先尝试一下,然后再在这样的区域覆盖一个按钮。
【解决方案3】:

按照 Samantha 的建议创建 IBOutlet 后,您需要向 UIImageView 添加手势识别器。

我建议在 viewDidLoad 中添加手势识别器:

-(void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *singlePress =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewPressed)];
    [singleTap setNumberOfTapsRequired:1];
    [_imageView addGestureRecognizer:singleTap];
}

- (void)imageViewPressed {
    //Perform imageView action here
}

【讨论】:

    【解决方案4】:

    使用 viewWithTag

       UIImageView *yourImage = (UIImageView*)[self.view viewWithTag:20];
    

    【讨论】:

      猜你喜欢
      • 2012-02-02
      • 2014-05-04
      • 2017-12-16
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 2011-12-23
      • 2013-12-04
      相关资源
      最近更新 更多