Buttons that aren't a subview of UIView don't work - objective-c

If I change it to be a subview of timerView it works fine but nothing else works.
timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 33)];
[self.view addSubview:timerView];
UIImageView *changerBackground = [[UIImageView alloc] init];
[changerBackground setImage:[UIImage imageNamed:#"speedbackground.png"]];
changerBackground.frame = CGRectMake(-12 , -16, 105, 33);
[timerView addSubview:changerBackground];
UIButton *normal = [UIButton buttonWithType:UIButtonTypeCustom];
normal.frame = CGRectMake(40, 8, 22, 22);
[normal setBackgroundImage:[UIImage imageNamed:#"normalwhite.png"] forState:UIControlStateNormal];
[normal setBackgroundImage:[UIImage imageNamed:#"normalred.png"] forState:UIControlStateHighlighted];
[normal addTarget:self action:#selector(quickTest:) forControlEvents:UIControlEventTouchUpInside];
[changerBackground addSubview:normal];

[changerBackground setUserInteractionEnabled:YES];
will solve your issue. As the UIImageView disables the user interaction by default.

Related

Go back to previous ViewController in xcode 5 storyboard

I am trying to create a back button on my iPhone app and I would like to go back to my landing view .. I am using this code but it did not work
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
[back setFrame:CGRectMake(0, 0, 24, 24)];
[back setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.hidesBackButton= YES;
navEng.leftBarButtonItem = btnBack;
-(void)goback{ [self.navigationController popToRootViewControllerAnimated:YES]; }
try this .... just changed last line code
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom]; [back setFrame:CGRectMake(0, 0, 24, 24)]; [back setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.hidesBackButton= YES;
self.navigationItem.leftBarButtonItem = btnBack;
-(void)goback
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
navEng is declared as IBoutlet UINavigationItem in header file and the back button appears and the action handled correctly but the navigation to the previous ViewController did not worked
#property (nonatomic, strong) IBOutlet UINavigationItem *navEng;
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
[back setFrame:CGRectMake(0, 0, 24, 24)];
[back setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.hidesBackButton= YES;
navEng.leftBarButtonItem = btnBack;
-(void)goback{
NSLog(#"back");
[self.navigationController popViewControllerAnimated:YES];
}

Objective-c : action:#selector(xxxxxx) not called

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;
}

iPhone UIButton action is not working

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.");
}

Image in UIButton is darker tharn original

I'm trying to add a UIButton in a cell which already has a UIImageView in the background.
I put this code in configureCell
UIButton *mail = [[UIButton alloc] initWithFrame:CGRectMake(10, 270, 40, 40)];
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
mail.backgroundColor = [UIColor whiteColor];
[mail addTarget:self action:#selector(sendMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:mail];
It works but the image in the UIButton is seems darker
I tried these but it didn't changed anything
mail.alpha = 1;
[cell.contentView bringSubviewToFront:mail];
Does anyone have an explanation for this, and a solution as well.
I had the same problem once and I solved it by setting the button's image for the UIControlStateHighlighted
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateHighlighted];
Hope it works for you
Try this
UIButton *mail=[UIButton buttonWithType:UIButtonTypeCustom];
mail.frame= CGRectMake(10, 270, 40, 40);
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
mail.backgroundColor = [UIColor clearColor];
[mail addTarget:self action:#selector(sendMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:mail];

Adding CustomView on a NavigationBar

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.