简体   繁体   中英

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.

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. (Don't worry, I'm looking through Apple's Documentation.)

Also, no, I can't change the UIImageView to a UIButton . Even if I changed to a UIButton , I need access to the individual touchesBegan , etc. methods and the same problem would persist.

If I'm understanding you correctly, you need to reference the UIViewController from the UIImageVIew to push/present a new UIViewController . You have a few options:

  1. Fire an NSNotification from the image view and have an event listener on the view controller.

  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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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