tags:

views:

499

answers:

2

Hi im trying to add a button on the point where users tapped on the screen.

Here is the code in my UIView file

- (void)drawRect:(CGRect)rect {
    // Drawing code
    NSLog(@"drawRect, %i, %i", firstTouch.x, firstTouch.y);
    [tagButton drawRect:CGRectMake(firstTouch.x, firstTouch.y, 100, 200)];
    [self addSubview:tagButton];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    firstTouch = [touch locationInView:self];

    [self setNeedsDisplay];
}

this is log from firstTouch

2009-10-01 17:27:23.743 text[2521:207] drawRect, 0, 1080311808

How do i get the x, y point of the touch, and create a uibutton on that point?

any help would be appreciated, thanks


This is what i come up with, it adds the button to the view. but when i pop this view, it crashes. i cant seem to find any problem. Can anyone see what the problem is?

UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     [newButton setFrame:CGRectMake(self.firstTouch.x, self.firstTouch.y, width, 17)];
     [newButton setTitle:[textField text] forState:UIControlStateNormal];
     [newButton setFont:[UIFont boldSystemFontOfSize:12]];
     [newButton setShowsTouchWhenHighlighted:YES];
     [newButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     [newButton setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
     [newButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
     [newButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
     [newButton setBackgroundColor:[UIColor clearColor]];

     UIImage *bgImg = [[UIImage imageNamed:@"button_greyblue.png"] stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0];
     [newButton setBackgroundImage:bgImg forState:UIControlStateNormal];


     [self setTagButton:newButton];
     [self.view addSubview:tagButton];
     [newButton release];
A: 

You get those values because you are using %i.

The firstTouch.x and .y values are actually floats and so you should use %f in NSLog.

For adding a button, you will have to alloc a new button each time (experts, correct me if i am wrong).

Other than that, the rest of your code looks good enough to add a button on tapped point.

Oh yes, you can get firstTouch.x and firstTouch.y in touchBegan method itself and add a button right there.

Chintan Patel
i have came across a new problem, can you please have a look?
vicky
A: 

I don't think you want to call drawRect directly. In touchesBegan capture your touchLocation as you have then while still in touchesBegan update the frame of your tagButton. There is a sample app called MoveMe that Apple provides which is quite simple and helps illustrate this.

Also, those points are expressed as floats not ints so use %f instead of %i


to respond to your new "problem": you seem to be over-releasing newButton. Remember the rules...if you get the object by alloc, new or copy then you need to release it. If not, then you don't. So your app is crashing because you're over-releasing that button. remove [newButton release] and things should be fine...

Meltemi
Thanks for your reply, i have came across a new problem, can you please have a look?
vicky