Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nnavick

macrumors regular
Original poster
Oct 13, 2010
100
0
hi all,
I created a method that creates button

PHP:
	CGRect buttonRect = CGRectMake(0, 0, 64, 64);
	UIButton *HamburgerButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
	[HamburgerButton setFrame:buttonRect];	
	[HamburgerButton setTitle:@"" forState:UIControlStateNormal];;
	UIImage *buttonImage = [UIImage imageNamed:@"Hamburger.png"];
	[HamburgerButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
forControlEvents:UIControlEventTouchUpInside];
	HamburgerButton.center=CGPointMake(x1=25+arc4random()% 260, y1=79+arc4random()% 350);
	[self  addSubview:HamburgerButton];
	[HamburgerButton release];

How do I connect the "vitual unInterFace Builder" Button(think NSbutton but not sure)to an IBaction?
I mean,I can't write in the .h -(IBaction) bla bla ; and then connect it visualy to the button,it's need to be via code but I don't know how to do it.

Thanks
 
hi all,
I created a method that create button

PHP:
	CGRect buttonRect = CGRectMake(0, 0, 64, 64);
	UIButton *HamburgerButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
	[HamburgerButton setFrame:buttonRect];	
	[HamburgerButton setTitle:@"" forState:UIControlStateNormal];;
	UIImage *buttonImage = [UIImage imageNamed:@"Hamburger.png"];
	[HamburgerButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
forControlEvents:UIControlEventTouchUpInside];
	HamburgerButton.center=CGPointMake(x1=25+arc4random()% 260, y1=79+arc4random()% 350);
	[self  addSubview:HamburgerButton];
	[HamburgerButton release];

How do I connect the "vitual unInterFace Builder" Button(think NSbutton but not sure)to an IBaction?
I mean,I can't write in the .h -(IBaction) bla bla ; and then connect it visualy to the button,it's need to be via code but I don't know how to do it.

Thanks


Take a look at the documentation for addTarget:Action:
 
I found what you sent me,but I don't understand where to put the code and where to write and call the function

PHP:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents




Code:
addTarget - Who will respond to the action.

action:(SEL)action - which method do you want executed when the ControlEvent happens

forControlEvents: - On which particular event do you want the action executed.

Example:
Code:
[myButton addTarget:self action:@selector(myMethodCallHere:) forControlEvents:UIControlEventTouchUpInside];
 
Code:
addTarget - Who will respond to the action.

action:(SEL)action - which method do you want executed when the ControlEvent happens

forControlEvents: - On which particular event do you want the action executed.

okkkk,and one more question(hope so),where do I write the code?
between the code I pasted on the main post or ?and how I call the method down?

Is it fine?

PHP:
- (void)addTarget:(id)HamburgerButton action:(SEL)ScoreUp forControlEvents:(UIControlEvents)UIControlEventTouchUpInside {
	ScoresCounter.text=[NSString stringWithFormat:@"%i",scores++];
}
 
okkkk,and one more question,where do I write the code?
between the code I pasted on the main post or ?


It depends, I would suggest for you when you create the button.


Also, from the code given I don't see why you would need to retain the button. You could drop both the retain/release.
 
PHP:
- (void)addTarget:(id)HamburgerButton action:(SEL)ScoreUp forControlEvents:(UIControlEvents)UIControlEventTouchUpInside {
	ScoresCounter.text=[NSString stringWithFormat:@"%i",scores++];
}


No, it's an instance method. You may want to look at UICatalog example from apple.


Edit:

Let me clarify a little.

addTarget:action:forControlEvents is an instance method of UIControl, which UIButton inherits from. So you would pass your addTarget:action:forControlEvents to your instance of UIButton.
 
No, it's an instance method. You may want to look at UICatalog example from apple.


Edit:

Let me clarify a little.

addTarget:action:forControlEvents is an instance method of UIControl, which UIButton inherits from. So you would pass your addTarget:action:forControlEvents to your instance of UIButton.

Could you write a little example with the code I posted up the the other codes such as the Score++ line

thanksss
 
Yes if you are still stuck after looking at the UICatalog example.

http://developer.apple.com/library/...on/Intro.html#//apple_ref/doc/uid/DTS40007710.

COMEEE ONN,I found in the UICatalog and found this code

PHP:
	 [HamburgerButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

and the method I created is

PHP:
- (void)action:(id)sender {
    ScoresCounter.text=[NSString stringWithFormat:@"%i",scores++];
}

Thank you very much!!!!!!!!
 
COMEEE ONN,I found in the UICatalog and found this code

PHP:
	 [HamburgerButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

and the method I created is

PHP:
- (void)action:(id)sender {
    ScoresCounter.text=[NSString stringWithFormat:@"%i",scores++];
}

Thank you very much!!!!!!!!


That's looking better!

Most important part:
Do you understand how this works and what everything does.
 
That's looking better!

Most important part:
Do you understand how this works and what everything does.

i know what is the method behind sender but not sure if I understood is well
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.