I have an iPad app I'm developing where a random number is generated when a user clicks a butt开发者_StackOverflowon, and it appears on a label over the button, so the user thinks that value is on the button. The user can click the button again to get another random number if they want it.
Is there a way for me to allow a user to drag a number from a source (say, a label) to another label - so that instead of a user having to click the button a number of times to get the number they want, they can simply drag it from a 'menu' onto the label.
Any help would be appreciated
Thanks
Baruugh
You can use a UITextField
, so that the user can enter a value by himself. Why you want to do something that complicated for entering values.
Edit: If you want the user to enter the value shown by the label than you can enable copy option for the same using UIMenuController
http://developer.apple.com/library/ios/documentation/iPhone/Reference/UIMenuController_Class/UIMenuController.html.
You need to handle touch events yourself. Subclass UILabel and override its touches...
methods.
First of all, you will update the position of the label in touchesMoved:withEvent:
method. In this method you are interested in the touch location in the superview, not in the label itself, so do something like [[touches anyObject] locationInView:self.superview]
.
And in touchesEnded:withEvent:
method, you will check the location of the label with the button, and if it overlaps, pass the value to the button. This can be achieved in many ways, but the best practice here IMO is to make a delegate pattern. Let the view controller who manages the superview be a delegate of the custom UILabel object. Send a message to the delegate in touchesEnded:withEvent:
method, and let the view controller handles the location check, and the button text update.
精彩评论