在我的iPad应用程序中,我有一个leftBar,如导航栏,它垂直放置在左侧,该栏有许多按钮和标题。

将leftBar添加到mainView(我的应用程序具有RootViewController,并且leftBar添加到根视图)。 leftBar在所有应用程序导航视图中都是可见的。为了允许所有其他对象(控制器)自定义Bar,我用两种方法创建了Data Source和Delegate协议。

我的问题是:我是否正确使用委托和数据源模式?哪种方法正确(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

最佳答案

你做对了

方法1

优点:


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


缺点


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


方法2

优点:


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


缺点:


更改单个属性是不可能的


因此,重要的是您应该如何使用它。

关于objective-c - 最佳实践委派&&数据源模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16915306/

10-14 20:33