Creating custom buttons for UIBarButton, selector throwing exception - objective-c

I am creating a custom button with its own image, and assigning it to the rightbarbutton of a nabvigationcontroller I have. The problem is that when the user clicks on the button I get an exceptoin about my controller not recognizing that selctor??
UIImage* image1 = [UIImage imageNamed:#"someImage.png"];
CGRect imgRect = CGRectMake(0, 0, image1.size.width/1.8, image1.size.height/1.8);
UIButton *myButton = [[UIButton alloc] initWithFrame:imgRect];
[myButton setBackgroundImage:image1 forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(nextScreenButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton =[[UIBarButtonItem alloc] initWithCustomView:myButton];
[self.navigationItem setRightBarButtonItem:myButton];
self.navigationController.navigationBarHidden = false;

May be this is the problem nextScreenButtonAction change it to
[myButton addTarget:self action:#selector(nextScreenButtonAction:) forControlEvents:UIControlEventTouchUpInside]
In your method definition you be passing and argument like
-(IBAction) nextScreenButtonAction:(id)sender
{
}
so in the addTarget you need to specify a colon(:) to indicate parameter

In response to below code,
[myButton addTarget:self action:#selector(nextScreenButtonAction) forControlEvents:UIControlEventTouchUpInside];
Make sure that you have create method in the same class like,
- (void) nextScreenButtonAction
{
// write your logic here...
}

Related

Handle popViewControllerAnimated like event in another class?

I have to create a same button in every ViewController. So i decided to write a class having this functionality once. I have created a class name MyUtils and added a back button to UINavigationItem and it displays correctly but don't know how to add an event to it in that class.
It displays accurately but no event is perform. I mean popViewControllerAnimated:YES
I call myUtil like
MyUtil *utils = [[MyUtil alloc] init];
[utils NavAndBackBtn:self.navigationItem];
MyUtil.m
-(void)NavAndBackBtn:(UINavigationItem *)nav
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"icon_back.png"] forState:UIControlStateNormal];
//[btn setTarget:self action:#selector(popView) forControlEventTouchUPInside];
[button setFrame:CGRectMake(0,0,36,20);
UIBarButtonItem *barbtn = [[UIBarButton alloc] initWithCustomView:btn];
nav.leftBarButtonItem = barbtn;// this gives error
}
-(void)popView
{
// [self.navigationController popViewControllerAnimated:YES]; //not working no method in self
}
But don't know how to add an event to popViewControllerAnimated:YES in MyUtil class. because i don't have the reference of self there.
popView method
there must be a way to get the context of the class . i mean self in this class. and add event on its behalf. And there is no method of UINavigationItem which will popViewController.
In Simple Words
I simply want to popViewController event on that button which i created in MyUtils . But it must be implemented in MyUtils class. How can i popViewControllerAnimated in another class.
-(void)showBackBarButton:(UIViewController*)controller{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTitle:#"Back" forState:UIControlStateNormal];
[btn addTarget:controller.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(0,0,36,30)];
UIBarButtonItem *barbtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
controller.navigationItem.leftBarButtonItem=barbtn;
}
On ViewDiDLoad
[utils showBackBarButton:self];

UIButton didn't call to target method

I have the following code:
UIImage *registerImg = [UIImage imageNamed:#"register_button_normal"];
CGFloat registerOffsetX = (view.frame.size.width - registerImg.size.width) / 2;
UIButton *registerBtnTwo = [[UIButton alloc] init];
registerBtnTwo.frame = CGRectMake(registerOffsetX, 111, registerImg.size.width, registerImg.size.height);
[registerBtnTwo setBackgroundImage:registerImg forState:UIControlStateNormal];
[registerBtnTwo addTarget:self action:#selector(submitRegister) forControlEvents:UIControlEventTouchUpInside];
[registerPanel addSubview:registerBtnTwo];
[registerBtnTwo release];
I have - (void)submitRegister; in class headers as instance method;
The problem is event isn't fired because i have NSLog in my submitRegister implementation
What i've tried:
1. UIButton *registerBtnTwo = [UIButton buttonWithType:UIButtonTypeCustom];
2. UIButton *registerBtnTwo = [[UIButton alloc] initWithFrame:frame];
EDIT 1: Who i add the current View Controller to my navigationController ?
if ( row == kLoginRegisterIndex ) {
login = [[LoginRegisterVC alloc] init];
[self.navigationController pushViewController:login animated:YES];
}
EDIT 2:
UIView *registerPanel = [[UIView alloc] initWithFrame:frame];
registerPanel.backgroundColor = [UIColor colorWithPatternImage:image];
Any ideas?
Try to change the
[registerBtnTwo addTarget:self action:#selector(submitRegister) forControlEvents:UIControlEventTouchUpInside];
to
[registerBtnTwo addTarget:self action:#selector(submitRegister:) forControlEvents:UIControlEventTouchUpInside];
The method signature is submitRegister: (not submitRegister).
You don't need to declare the method in the header.
If this doesn't help check the frame of the host view. When the button is outside of the frame it can be shown but it won't accept touches.
I don't understand what you are asking in your edit.
The problem was that parent view was UIImageView which by default have userInteractionEnabled = NO;

Why is my UIButton not displaying on my view?

I have a iOS5 project using storyboard and with ARC on.
I have a view in storyboard with the class thisViewController and in there I got a smaller subview which I dragged in and gave it the class thisView.
thisView has a custom drawRect function, that works and draws what I want nicely.
But I also want to add buttons dynamically, so I'll add them in the initWithFrame method of thisView like so:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"This is called <3");
UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 50, 50);
btn.backgroundColor = [UIColor redColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
[self bringSubviewToFront:btn]; // last thing I tried, didn't work, thought z-index was to low or something
}
return self;
}
The function is being called since the NSLog displays, but the button is nowhere to be found
--EDIT--
Extra information:
ThisView.h
#interface RadarView : UIView <CLLocationManagerDelegate>{
CLLocation *currentLocation;
}
Dont add the SubView in the init method.
Add the SubView in the - (void)layoutSubviews.
- (void)layoutSubviews {
[super layoutSubviews];
if (![self viewWithTag:12345]) {
UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 50, 50);
btn.tag = 12345;
btn.backgroundColor = [UIColor redColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
}
I found what the problem was thanks to some comments and the chat
There was an init in the radarviewcontroller, which triggered initwithframe, which made the NSLog display, and the initwithcoder wasn't used so I didn't see anything, that was the problem and got stuff mixed up. Thanks for commenting and helpin!

Function with variable blind to a button xcode

Hi I have a problem i can not send parameter for function
-(IBAction)ukazObrat:(NSInteger *)cis {
stvrtyViewController *screen = [[stvrtyViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
screen.obratLabel.font = [UIFont systemFontOfSize: 12];
screen.obratLabel.numberOfLines = 0;
screen.obratLabel.text = [array objectAtIndex:cis];
[screen release];
}
I set this function to a button klick but I m unable to compile it when I add parameter to it.
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button.titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[button.titleLabel setTextAlignment:UITextAlignmentLeft];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button setTitle:obratLable forState:UIControlStateNormal];
[button addTarget:self
action:#selector([self ukazObrat:minus]
forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(0.0, 0.0, 250.0, 60.0);
It was working until I add there the variable.
please help
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventTouchDown];
Then define buttonHandler as:
- (void)buttonHandler {
[self ukazObrat:minus];
}
Manage minus however is necessary.
You can't set variable into selector definition. Definition can contain only function name. And number of colons will show compiler how much parameters this function required. Selector for button can have one argument - pointer to event-sender button. If you want to send some data from one controller to another you should create some shared storage for that data.
I use for that purpose singleton class with mutable dictionary inside it.

How to associate a method with this button?

I have a navigation bar button that displays both image and text. This is the code:
UIImage *saveImage = [UIImage imageNamed:#"star.png"];
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:saveImage forState:UIControlStateNormal];
[saveButton setTitle:#"Save" forState:UIControlStateNormal];
saveButton.frame = (CGRect) {
.size.width = 100,
.size.height = 30,
};
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:saveButton];
[self.navigationItem setRightBarButtonItem:barButton animated:YES];
I tried this
[barButton setAction:#selector(saveArray)];
but it doesn't work.
Remember that you must specify target as well. You can set action/target to the UIButton object itself:
[saveButton addTarget:target action:#selector(saveArray) forControlEvents:UIControlEventTouchUpInside];
I've tried to set target/action to the UIBarButtonItem directly but it seems not to work in case of UIButton for custom view for some reason.
Did you remember to set the target? If the desired method belongs to the same class, you could do:
[barButton setTarget:self];
That said, action and target are both properties (new to Obj-C 2.0), consider using the dot notation:
barButton.action = #selector(saveArray:)
barButton.target = self;