2013-06-04 4 views
2

В моем приложении iPad у меня есть leftBar, как и панель навигации, она помещается влево по вертикали, на этой панели есть много кнопок и заголовков.Лучшая практика делегирования && шаблонов источника данных

В mainView добавлен левый элемент (мое приложение имеет RootViewController, а leftBar добавляется в представление root). Левая панель видна во всех видах навигации приложений. Чтобы разрешить всем другим объектам (контроллерам) настраивать панель, я создал источник данных и протоколы делегатов в двух методах.

Мой вопрос: правильно ли я использую правильные варианты делегирования и данных? Какой правильный метод (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:

в этом Methode я создал в delaget Methode как это:

@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 

ответ

2

вы делаете это правильно

Способ 1

Плюсы:

  • Чтобы настроить все и вся, у меня есть метод.
  • дает гибкость для будущих проектов
  • лучше документированы

Против

  • Для настройки панели инструментов вы должны сделать и установить все методы.
  • Слишком много кода для этого

Метод 2

Pros:

  • Сигле метод установки моя панель [Это то, что я буду искать]

Против:

  • Чтобы изменить одно свойство это не представляется возможным

Так что все это имеет значение как вы должны использовать его.

+0

Спасибо за ваш ответ. И что вы думаете о присвоении имени делегата/DataSource? – samir

+0

методы источника данных предназначены для настройки элемента управления, а делегаты должны быть действиями или результатами. Имя не имеет значения, но в коде яблока используется следующий способ. См. Метод делегатов Datasource tableview. –