3 回答
TA贡献1966条经验 获得超4个赞
第一
第二
第三
在视图控制器中使用依赖项注入的示例
@implementation BookPickerViewController-(void) doSomething { // I need to do something with the BookWarehouse so I'm going to look it up // using the BookWarehouse class method (comparable to a global variable) BookWarehouse *warehouse = [BookWarehouse getSingleton]; ...}
@implementation BookPickerViewController-(void) initWithWarehouse: (BookWarehouse*)warehouse { // myBookWarehouse is an instance variable myBookWarehouse = warehouse; [myBookWarehouse retain];}-(void) doSomething { // I need to do something with the BookWarehouse object which was // injected for me [myBookWarehouse listBooks]; ...}
@implementation BookPickerViewController-(void) initWithWarehouse: (BookWarehouse*)warehouse andCheckoutController:(CheckoutController*)checkoutController { myBookWarehouse = warehouse; myCheckoutController = checkoutController;}-(void) handleCheckout { // We've collected the user's book picks in a "bookPicks" variable [myCheckoutController handleCheckout: bookPicks]; ...}
@interface MyBookWarehouse : NSObject <BookWarehouse> { ... } @end@implementation MyBookWarehouse { ... } @end@interface MyCheckoutController : NSObject <CheckoutController> { ... } @end@implementation MyCheckoutController { ... } @end...-(void) applicationDidFinishLoading { MyBookWarehouse *myWarehouse = [[MyBookWarehouse alloc]init]; MyCheckoutController *myCheckout = [[MyCheckoutController alloc]init]; BookPickerViewController *bookPicker = [[BookPickerViewController alloc] initWithWarehouse:myWarehouse andCheckoutController:myCheckout]; ... [window addSubview:[bookPicker view]]; [window makeKeyAndVisible];}
-(void) testBookPickerController { MockBookWarehouse *myWarehouse = [[MockBookWarehouse alloc]init]; MockCheckoutController *myCheckout = [[MockCheckoutController alloc]init]; BookPickerViewController *bookPicker = [[BookPickerViewController alloc] initWithWarehouse:myWarehouse andCheckoutController:myCheckout]; ... [bookPicker handleCheckout]; // Do stuff to verify that BookPickerViewController correctly called // MockCheckoutController's handleCheckout: method and passed it a valid // list of books ...}
TA贡献1812条经验 获得超5个赞
@class Editor;@protocol EditorDelegate// called when you're finished. updated = YES for 'save' button, NO for 'cancel'- (void)editor: (Editor*)editor finishedEditingModel:(id)model updated:(BOOL)updated; @end// this is an abstract class@interface Editor : UIViewController { id model; id <EditorDelegate> delegate;}@property (retain) Model * model;@property (assign) id <EditorDelegate> delegate;...define methods here...@end@interface AmountEditor : Editor...define interface here...@end@interface TextEditor : Editor...define interface here...@end// TransactionController shows the transaction's details in a table view@interface TransactionController : UITableViewController <EditorDelegate> { AmountEditor * amountEditor; TextEditor * textEditor; Transaction * transaction;}...properties and methods here...@end
- (void)viewDidLoad { amountEditor.delegate = self; textEditor.delegate = self;}- (void)editAmount { amountEditor.model = self.transaction; [self.navigationController pushViewController:amountEditor animated:YES];}- (void)editNote { textEditor.model = self.transaction; [self.navigationController pushViewController:textEditor animated:YES];}- (void)editor:(Editor*)editor finishedEditingModel: (id)model updated:(BOOL)updated { if(updated) { [self.tableView reloadData]; } [self.navigationController popViewControllerAnimated:YES];}
- 3 回答
- 0 关注
- 439 浏览
添加回答
举报