Function with variable blind to a button xcode - objective-c

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.

Related

Creating custom buttons for UIBarButton, selector throwing exception

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

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;

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;

Why am I getting "Message sent to deallocated instance" in Objective-C?

I have several buttons on my app that are being created dynamically. They are all pointed at the button click event when pressed. When the button pressed method is called, the sender's tag (int value) is parsed into the controller's house ID. It works with one of the buttons — the first one created, to be specific — but the others throw the following error:
-[CFNumber intValue]: message sent to deallocated instance 0xc4bb0ff0
I am not releasing these buttons anywhere in my code. I haven't set them to autorelease or anything like that. I'm just wondering why they are doing this on the click.
The button click event:
- (IBAction) ButtonClick: (id) sender
{
HouseholdViewController *controller = [[HouseholdViewController alloc] initWithNibName:#"HouseholdViewController" bundle:nil];
controller.delegate = self;
controller.HouseID = [(NSInteger)[(UIButton *)sender tag] intValue]; //this line throws an error
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
Where I am creating the buttons:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(MyLongInScreenCoords, MyLatInScreenCoords, 50, 50);
UIImage *buttonImageNormal = [UIImage imageNamed:#"blue_pin.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:50 topCapHeight:50];
[button setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
[self.view addSubview:button];
button.tag = [NSNumber numberWithInt:[[words objectAtIndex: i] intValue]];
ButtonPoints[CurrentHouseCount][0] = button;
ButtonPoints[CurrentHouseCount][1] = [NSValue valueWithCGPoint:CGPointMake(MyActualLat, MyActualLong)];
[button addTarget:self action:#selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
CurrentHouseCount++;
tag is an integer property, not an object. just set it
button.tag = [[words objectAtIndex: i] intValue];
and use:
controller.HouseID = [(UIButton *)sender tag];

Help with iphone button pressed

I am trying to see which button was clicked on so I can preform the correct logic.
This is the code for the buttons:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(270, 423, 60, 60)];
[button addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[[UIImage imageNamed:#"refreshicon.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button.tag = 1;
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 423, 60, 60)];
[button2 addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setBackgroundImage:[[UIImage imageNamed:#"login.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button2.tag = 2;
[self.navigationController.view addSubview:button];
[self.navigationController.view addSubview:button2];
And this is how I call the buttonPressedAction:
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
if(button.tag == 1)
{
NSLog(#"1");
}else
{
NSLog(#"2");
}
}
But when I use NSLog to see what the sender value is, it crashes.
Any advice for what's happening and how to correct it?
Now corrected :o) THANKS!
As other have pointed out, UIButton does not have a value property. I guess you are trying to detect which button was clicked. Here are two ways to do this:
Use the tag property one each button. I.e. button1.tag = 1, button2.tag = 2. You can then test which button was clicked with if(sender.tag == 1) etc. You could introduce constants for the numbers to make the code more readable.
If you keep a reference to the button, you can check if the reference is equal. Example: if(sender == self.button1)
Probably the best approach would be to give buttons that do different things different actions, but failing that, you could distinguish them by tag.
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
// do something
}
If you were saving the pointers to the button objects you are creating, you could compare the pointers. Or you can just set the tag property for the button, and use that to switch behaviour.