在我的 iPad 应用程序中,我有一个类似于导航栏的 leftBar,它垂直放置在左侧,这个栏有很多按钮和标题。
leftBar 添加到 mainView (我的应用程序有一个 RootViewController 并且 leftBar 添加到根视图)。leftBar 在所有应用程序导航视图中都是可见的。为了允许所有其他对象(控制器)自定义 Bar,我以两种方法创建了一个数据源和一个委托协议。
我的问题是:我是否正确使用委托和数据源模式?哪种方法是正确的(1 或 2)?或者这两种方法不好,有最好的解决方案吗?
#import <UIKit/UIKit.h>
@protocol ToolBarDataSource <NSObject>
@optional
/** Asks the data source of the origin of the view
@disscussion overide thid methode if yoy want différente origin
of the view, the default is '(0,0)'
*/
- (CGPoint)toolBarViewOrigin;
/** Asks the data source for the backgounnd image for the view
@Disscussion The toolBar view has a background image by default
implemente this methode to customize it
*/
- (UIImage *)toolBarBackgroundImage;
/** Asks the data source for the frame for the button spécified with the tag
@param buttonTag The button tag
*/
- (CGRect)toolBarButtonFrameWithTag:(NSUInteger)buttonTag;
/** Asks the data source for the button title spécified with the tag
@param buttonTag The button tag
*/
- (NSString *)toolBarTitleForButtonWithTag:(NSUInteger)buttonTag;
/** Asks the data source for the button title font spécified with the tag
@param buttonTag The button tag
*/
- (UIFont *)toolBarTitleFontForButtonWithTag:(NSUInteger)buttonTag;
/** Asks the data source for the button title text color spécified with the tag
@param buttonTag The button tag
*/
- (UIColor *)toolBarTitleColorForButtonWithTag:(NSUInteger)buttonTag;
/** Asks the data source to return the title to be displayed in the toolBar*/
- (NSString *)titleForToolBar;
/** Asks the data source to return the frame of the title label */
- (CGRect)toolBarTitleFrame;
@end
@protocol ToolBarDelegate <NSObject>
@optional
/** Tells the delegate that a button in the toolBar has been clicked
@param buttonTag The button tag
@disscussion Each button int the tollBar View is identified with a Tag.
@see Constant.h for more details for all the tags
*/
- (void)toolBarButtonClickedWithTag:(NSUInteger)buttonTag;
@end
@interface ToolBarVC : UIViewController
/** The object that acts as the delegate of the receiving toolBar view. */
@property (nonatomic, assign)id <ToolBarDelegate>toolBarDelegate;
/** The object that acts as the data source of the receiving toolBar view. */
@property (nonatomic, assign)id <ToolBarDataSource>toolBarDataSource;
@end
方法二:
在这个方法中,我在 delaget 方法中创建了这样的方法:
@protocol ToolBarDelegate <NSObject>
@optional
- (void)setToolBarTitle:(NSString *)title
font:(UIFont *)font
color:(UIColor *)color
frame:(CGRect)frame;
/** */
- (void)setBackButtonBackgroundImage:(UIImage *)image
title:(NSString *)title
color:(UIColor *)color
frame:(CGRect)frame;
/** */
- (void)setRightButtonBackgroundImage:(UIImage *)image
title:(NSString *)title
color:(UIColor *)color
frame:(CGRect)frame;
@end