I'm currently trying to add a button to my app. If the user push on it, it should display an alert.
I want to add the button to a section header of a tableview. Here is my code in tableView:viewForHeaderInSection:
UIImageView *headerView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
...
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
return headerView;
And in the same class, I have the method :
-(void)showMyAlert{
NSLog(#"HERE SHOW ALERT");
...
}
The button is correctly displayed but the showMyAlert method is never called when I push on it.
Does anyone have an idea of what is wrong with my code ?
Thanks if you could help me :)
Change your UIImageView as UIView and return it as the header view.
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
Make sure img is not nil. If that is nil, your button will have no image and you will see nothing.
Modified below lines of code, Try like this:-
[button addTarget:self action:#selector(showMyAlert:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)showMyAlert:(id)sender{
NSLog(#"HERE SHOW ALERT");
...
}
Try this, in viewDidLoad,
-(void)viewDidLoad
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
[headerView addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
self.tableView.tableHeaderView = headerView;
}
Related
I create UIView and then I add UIButton as subview with action, but this action is never called.
Does anybody know why?
Here is my code:
- (void)viewDidLoad{
UIView *roundResultView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 150)];
UIImageView *_popUpBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NotificationBg.png"]];
_popUpBg.frame = CGRectMake(0, 0, roundViewWidth, roundViewHeight);
UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 100, 30)];
[closeButton addTarget:self action:#selector(closeAndBack) forControlEvents:UIControlEventTouchUpInside];
[closeButton setTitle:#"Back") forState:UIControlStateNormal];
[roundResultView addSubview:_popUpBg];
[roundResultView addSubview:closeButton];
[self.view addSubview:roundResultView];
}
-(void)closeAndBack{
NSLog(#"closeAndBack"); //never called
}
I have checked this:
NSLog(#"%#", [closeButton allControlEvents]);
NSLog(#"%#", [closeButton allTargets]);
and it prints:
... Sedmica[2192:14f03] 64
... Sedmica[2192:14f03] {(
<ViewController: 0x8631e10>
)}
Update your code with this and kindly let me know :
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[closeButton addTarget:self
action:#selector(closeAndBack:)
forControlEvents:UIControlEventTouchDown];
[closeButton setTitle:#"Back"
forState:UIControlStateNormal];
closeButton.frame = CGRectMake(30.0, 30.0, 100.0, 30.0);
[self.view addSubview:closeButton];
- (IBAction)closeAndBack:(UIButton *)button{
NSLog(#"Button clicked.");
}
I have a UIBarButtonItem that I want hugging the very left of the screen. I've tried putting a fixed barbutton with width=0 before the button and other configurations but there's always several pixels between the button and x=0.
I've also tried changing the imageInset of the UIButton
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.frame = CGRectMake(0, 0, 60, 35);
[moreButton setImage:[UIImage imageNamed:#"Morebutton"] forState:UIControlStateNormal];
moreButton.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 8);
[moreButton addTarget:self action:#selector(pressedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *moreBar = [[[UIBarButtonItem alloc] initWithCustomView:moreButton] autorelease];
But tapping near the edge of the screen doesn't trigger the selector (probably because only the image is moved but not the button itself) and increasing the size of the button doesn't affect the gap between the button and the left side.
Thanks
try to put your button to the container view and change position of button on the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.contentMode = UIViewContentModeScaleAspectFit;
button.frame= CGRectMake(-4.0, 0.0, 30, 30);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cellTextSize.width+10, buttonImage.size.height)] autorelease];
v.backgroundColor = [UIColor clearColor];
[v addSubview:button];
return [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];
is there any way to change the button image inside UIToolbar?
I am actually using this way but it doesn't work.
[myButton setImage:[UIImage imageNamed:#"blue.png"]];
For your information, "myButton" is an IBOulet UIBarButtonItem.
#property (nonatomic, strong) IBOutlet UIBarButtonItem *myButton;
Use the following cole. i hope it will use to you.
UIImage *image = [UIImage imageNamed:#"buttonImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:myTarget action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Thanks,.
// add images to bar button item of toolbar in iphone
to set button type as custum:
button=[UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 40, 40)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
// add the image on toolbar:
[toolbar insertSubview:[[[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"image.png"]] autorelease] atIndex:1];
This is my code.
- (void)viewWillAppear:(BOOL)animated
{
customView = [[UIView alloc] init];
UIButton *btnAdd = [[UIButton alloc] initWithFrame:CGRectMake(410, -20, 30, 40)];
[btnAdd setBackgroundColor:[UIColor blackColor]];
[btnAdd setBackgroundImage:[UIImage imageNamed:#"oie_815362uTrAOBMX.png"] forState:UIControlStateNormal];
[btnAdd addTarget:nil action:#selector(addCustomer:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnAdd];
UIButton *btnJump = [[UIButton alloc] initWithFrame:CGRectMake(450, -20, 30, 40)];
[btnJump setBackgroundColor:[UIColor blueColor]];
[btnJump setBackgroundImage:[UIImage imageNamed:#"oie_8153842yVgkR6XD.jpg"] forState:UIControlStateNormal];
[btnJump addTarget:nil action:#selector(showMenue:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnJump];
UIButton *btnBarItem = [[UIButton alloc] initWithFrame:CGRectMake(300, -20, 100, 40)];
[btnBarItem setBackgroundColor:[UIColor greenColor]];
[btnBarItem setBackgroundImage:[UIImage imageNamed:#"oie_99342oliBc5Sy.jpg"] forState:UIControlStateNormal];
[btnBarItem addTarget:nil action:#selector(showScanner:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnBarItem];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, -10, 30, 30)];
[imageView setImage:[UIImage imageNamed:#"oie_972931iW4SHiZU.jpg"]];
[customView addSubview:imageView];
UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, -20, 80, 40)];
[mainLabel setText:#"Kunden"];
[mainLabel setBackgroundColor:[UIColor lightGrayColor]];
[mainLabel setTextColor:[UIColor blackColor]];
[mainLabel setFont:[UIFont fontWithName:#"Arial" size:18]];
[customView addSubview:mainLabel];
[self.navigationItem setTitleView:customView];
}
- (IBAction)showMenue:(id)sender
{
NewPopUpmenu *options = [[NewPopUpmenu alloc] initWithNibName:#"PopupMenu" bundle:nil];
[options.view setFrame:CGRectMake(720, 0, 300, 200)];
[self.view addSubview:options.view];
}
Note: Problem is that when i press on my "jump" button it doesn't access it's selector Method (showMenue:).
You are specifying a target of nil for each button's target-action pair. If your action method is implemented in the same class, your target should be self:
[btnAdd addTarget:self
action:#selector(addCustomer:)
forControlEvents:UIControlEventTouchUpInside];
// ...
// Same for other buttons...
// ...
Technically, if you specify nil for the target parameter, the responder chain is searched for an object that responds to the action. The problem you are having could possibly be down to the fact that your showeMenu: method isn't the signature that your action is expecting. The action is looking for a method signature that looks like this:
- (void)showeMenu:(id)sender;
Yet you have specified the IBAction return type in the implementation.
First: Sorry for my bad English!
I have a class in which I create UIButton. The UIButton I add a UIView. In the ViewController I would like to add these custom tabbar. This works well so far. However, there are problems with addTarget: action: forControlEvents: - How can I run from the ViewController an instance method in the class? The class is of type NSObject. My code:
TabBar *tabBar = [[TabBar alloc] init];
[tabBar tabBarButton];
[tabBar.home addTarget:tabBar action:#selector(switchView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tabBar.tabView];
And here is my Class:
#implementation TabBar
#synthesize tabView, home, list, charts, favorits, more;
-(void)tabBarButton{
tabView = [[UIView alloc] initWithFrame:CGRectMake(0, 360, 320,56)];
home = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 43, 56)];
list = [[UIButton alloc] initWithFrame:CGRectMake(63, 0, 40, 56)];
charts = [[UIButton alloc] initWithFrame:CGRectMake(123, 0, 49, 56)];
favorits = [[UIButton alloc] initWithFrame:CGRectMake(192, 0, 66, 56)];
more = [[UIButton alloc] initWithFrame:CGRectMake(278, 0, 42, 56)];
[home setImage:[UIImage imageNamed:#"home.png"] forState:UIControlStateNormal];
[list setImage:[UIImage imageNamed:#"liste.png"] forState:UIControlStateNormal];
[charts setImage:[UIImage imageNamed:#"charts.png"] forState:UIControlStateNormal];
[favorits setImage:[UIImage imageNamed:#"favorites.png"] forState:UIControlStateNormal];
[more setImage:[UIImage imageNamed:#"more.png"] forState:UIControlStateNormal];
home.tag = 1;
list.tag = 2;
charts.tag = 3;
favorits.tag = 4;
more.tag = 5;
[tabView addSubview:home];
[tabView addSubview:list];
[tabView addSubview:charts];
[tabView addSubview:favorits];
[tabView addSubview:more];
}
-(void)switchView{
NSLog(#"hi");
}
When I start the app, it crashes immediately when I press one button.
If you're getting an unrecognized selector error, you might try:
[tabBar.home addTarget:tabBar action:#selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
Note the colon behind action:#selector(switchView:)