Reputation: 101
This is my first question on Stack Overflow, after reading many results from Googling various programming problems.
My question is about iOS development. Basically I have a button that, when pressed, creates another button on the view. I want the user to be able to drag that button around the form. From searching, I found code that shows how to move a button, but that requires having an outlet connected to it (so you can set the center property). Since the button has been created programatically in another method, I can't figure out how to set the center property.
Here's my addButton: method:
-(IBAction)addButton:(id)sender{
CGRect frame = CGRectMake(5.0, 25.0, 40.0, 40.0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(moveButton:) forControlEvents:UIControlEventTouchDragInside];
[button setTitle:@"Test Button" forState:UIControlStateNormal];
button.frame = frame;
[myView addSubview:button];
}
And what I have typed into the touchesMoved: method:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
}
I'm pretty new to iOS development, so I'm not really sure how to solve this.
Upvotes: 2
Views: 1188
Reputation: 27147
You should make the second button a property of your class. First declare the property in the .h+.m file just like the first one was automatically by xCode. Then at the end of your example code add self.secondButtonPropertyNameHere = button;
Once it's a property of the class, any method can set it's properties.
Edit:
Instead of a UIButton property make an NSMutableArray property. Then just add the new button to the array so you can keep a reference to them. Just remember if you go to remove one call [buttonToRemove removeFromSuperview] and remove from the array.
Edit(2):
Your initial question was about making a second button and moving it around. The property of UIButton will work well for that. Because you're moving one object on the view receiving the touches* events. If I were you I would stick with this approach until you are comfortable with the touches* events.
When you are ready to move on. You can manage multiple runtime created UIViews (in your case you want to use buttons) you have to choices:
1) In the touchesBegan: of your view controller iterate thru your array of buttons to see if perhaps the touch fell on one of them and then track the touch until the touchesEnded: call.
2) Subclass the UIView and let it handle it's own touches* events.
The second is much easier to manage.
Edit(3):
You will need to set [button setUserInteractionEnabled:NO]
if you wish the superview to handle the touches* events instead of the buttons.
Upvotes: 1
Reputation: 5722
I've done something similar, but in a different way: you can create buttons, then move them around in a rect (actually a scaled view of the later real sized view) and change the size too, with 4 sliders and other buttons for fine tuning. It's very simple by just setting the frame of the target button to new values all the time. I didn't do it with dragging because the button could get too small for the finger.
Upvotes: 0