简体   繁体   中英

UIView subclass with its own controller - Design pattern

As far as i know, to follow MVC pattern guidelines, you shouldn't have controllers inside a view (ie. server requests, delegates, etc.), however some of the Apple's sample codes have animations inside the view (mainly CABasicAnimation instances in order to animate its layer).

My question is if having some controller logic inside your view violates the MVC design pattern, and if so, what's the best alternative, for example if we want a UIView that it will always animate (ie. bounce) when doing some action, and you don't want to implement that logic for each UIViewController that has an instance of the view.

You can simply create a UIViewController that contains the logic of animation of the view, beeing the view owner.

After this you use it like a singleton, and add its view to all the places you want, by having the controller always animating the view.

BUT to answer at your first question, yes it breaks the pattern if you put controller code inside a view.

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