tailieunhanh - beginning iphone 3 development exploring the iphone sdk phần 4

Mục đích của MVC là làm cho các đối tượng thực hiện các loại mã như khác biệt với nhau càng tốt. Bất cứ đối tượng nào bạn viết nên được dễ dàng nhận dạng là thuộc vào một trong ba loại, với rất ít hoặc không có chức năng bên trong nó có thể được phân loại trong một trong hai. | 148 CHAPTER 7 Tab Bars and Pickers Delegates and Datasources Before we dive in and start building our application let s look at why pickers are so much more complex than the other controls you ve used so far. It s not just a matter of there being more configurable attributes to set in Interface Builder. In fact the picker actually has very few attributes that can be configured from within Interface Builder. With the exception of the date picker you can t use a picker by just grabbing one in Interface Builder dropping it on your content view and configuring it. You have to also provide it with both a picker delegate and a picker datasource. By this point you should be comfortable using delegates. We ve already used application delegates and action sheet delegates and the basic idea is the same here. The picker defers several jobs to its delegate. The most important of these is the task of determining what to actually draw for each of the rows in each of its components. The picker asks the delegate for either a string or a view that will be drawn at a given spot on a given component. The picker gets its data from the delegate. In addition to the delegate pickers need to have a datasource. In this instance the name datasource is a bit of a misnomer. The datasource tells the picker how many components it will be working with and how many rows make up each component. The datasource works similarly to the delegate in that its methods are called at certain prespecified times. Without a datasource and a delegate specified pickers cannot do their job and in fact won t even be drawn. It s very common for the datasource and the delegate to be the same object and just as common for that object to be the view controller for the picker s enclosing view which is the approach we ll be using in this application. The view controllers for each content pane will be the datasource and the delegate for their picker. .