I have a uiviewcontroller. In this view, there is a button, when clicked on that button, it should popup, something like "dialog box", which ask user to enter some value. Once user enter the value, it will have another customized (with background image) "confirm diaglog box", which has 2 buttons, yes and no. This "confirm dialog box" might be r开发者_如何学JAVAe-use by others classes. I have read apple docs about "delegate and data sources" and several questions about delegate functions on SO, but I still not sure if I should use delegate function. Any thoughts?
Delegates are very useful, but they go hand in hand with protocols. Protocols allow you to define a sort of "contract" to ensure that the delegate will respond to those methods. For instance, you could make a protocol named UIConfirmBoxDelegate
and have a method:
- (void)confirmBox:(UIConfirmBox*)box didChoose:(ButtonEnum)button;
Your delegated object would then have to implement both your protocol and this method. The first parameter is the object that is sending the delegated message (it seems to be the convention, a very useful one at that) and the second parameter is an enum value indicating which button was clicked. This should allow you to easily add more buttons in the future (perhaps Yes, No, Cancel?).
You can (and likely should) read more about protocols and defining enums (that is the second parameter of the method mentioned above).
To answer a new question in the comments: How to get value of a UISlider
using delegation. You could use a delegate that is fired each time the slider is manipulated. Just have a new delegate function, such as:
- (void)confirmBox:(UIConfirmBox*)box didChangeSlider:(UISlider*)slider;
and pass the confirm box and slider "whole sale" to the delegate to do with as needed. You could also just have it pass the value if you don't want to pass a pointer to the entire slider. Just send that message to your delegate inside your IBAction
responding to your slider's value changes and you should be set to go.
精彩评论