简体   繁体   中英

Same UIPickerView in different ViewControllers

I'm new to objective-c and iOS app development, and I have next question: Is it possible to have one picker view in different views, I mean it should have same data in it and behave equally. Currently i have added 3 picker views, each in different view, and now i have triple copy of same code. So to avoid code duplicating, is there way to create custom view and put this in each of 3 view controllers and just initialize it. Thanks.

Sure. You could even do it all from a separate NSObject inherited class.

Then make that picker class handle all of the picker stuff via its methods.

Then the view controller that wants to use it only has to have an instance of this picker object and it directly calls the needed methods.

That or you can directly create an UIPickerView inherited object and automatically setup its data when it's initialized and use that instead of UIPickerView.

Some quick example code can be found by googling. Here's one: http://iphonedevsdk.com/forum/iphone-sdk-development/46378-subclassing-uipickerview-question.html

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