最佳实践代表团&&数据源模式

时间:2013-06-04 10:19:03

标签: objective-c cocoa

在我的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

Methode 2:

在这个方法中,我在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

1 个答案:

答案 0 :(得分:2)

你做对了

方法1

优点:

  • 设置我得到的每一个方法。
  • 为未来的项目提供灵活性
  • 更好的文件记录

缺点

  • 要设置工具栏,您必须制作并设置所有方法。
  • 代码太多

方法2

优点:

  • Sigle方法将设置我的工具栏[这就是我要找的]

缺点:

  • 要更改单个属性,

所以重要的是你应该如何使用它