简体   繁体   English

iOS:从其他视图的类更改视图的正确方法是什么?

[英]IOS: What is the correct way to change a view from the class of a different view?

I have a UIImageView subclass (in swift) set up so I can access touchesBegan/touchesMoved/touchesEnded. 我有一个UIImageView子类(快速设置),所以我可以访问touchesBegan / touchesMoved / touchesEnded。

When one of these methods is called, I need to change a property of a different, loaded view. 当调用这些方法之一时,我需要更改另一个已加载视图的属性。

It seems to me that I will now need to access the active view controller in order to set the properties of this other view. 在我看来,我现在将需要访问活动视图控制器以设置另一个视图的属性。 Is there are better way to go about this (such as event methods called in the view controller)? 有没有更好的方法可以解决此问题(例如在视图控制器中调用的事件方法)?

Note that I'm new to iOS and I am not extremely familiar with the event system yet, as most information I've found is written in Objective C and not in Swift. 请注意,我是iOS的新手,我对事件系统还不是很熟悉,因为我发现的大多数信息都是用Objective C编写的,而不是用Swift编写的。 (Don't worry, I'm looking through Apple's Documentation.) (不用担心,我正在浏览Apple的文档。)

Also, no, I can't change the UIImageView to a UIButton . 另外,不,我不能将UIImageView更改为UIButton Even if I changed to a UIButton , I need access to the individual touchesBegan , etc. methods and the same problem would persist. 即使更改为UIButton ,我也需要访问各个touchesBegan等方法,并且相同的问题仍然存在。

If I'm understanding you correctly, you need to reference the UIViewController from the UIImageVIew to push/present a new UIViewController . 如果我对您的理解正确,则需要从UIImageVIew引用UIViewController来推送/呈现新的UIViewController You have a few options: 您有几种选择:

  1. Fire an NSNotification from the image view and have an event listener on the view controller. 从图像视图NSNotification ,并在视图控制器上具有事件侦听器。

  2. Create a delegate on the image view that fires a selector on the view controller. 在图像视图上创建一个委托,该委托将触发视图控制器上的选择器。

  3. Hold a reference to the view controller on the image view, and push a new view controller from the image view with that reference. 在图像视图上保留对视图控制器的引用,然后使用该引用从图像视图中推送新的视图控制器。

  4. Modally present a new view controller on the application window's rootViewController . 在应用程序窗口的rootViewController上以模态形式呈现一个新的视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM