add an action on a button without ibaction - objective-c

I'm working on a mapkit view, i added a button on an annotation like this:
UIButton *bt = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[bt setFrame:CGRectMake(0, 0, 30, 30)];
annView.rightCalloutAccessoryView = bt;
i want to go to another view (for example expleViewController) when touching that button, i'm accustomed to do it using an IBAction method and link it with the button using Interface Builder, but this button is created manually, so how can I link it?

[bt addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
- (void)buttonPressed:(UIButton *)b
{
// do whatever you want ;)
}

Related

TVOS: UIButton action not working in subview

in the TVOS Application storyboard mainview I added a subview.
Within the subview there are 3 button created in the very same way (the code here belongs to the subview)
UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
b.frame = CGRectMake(left, top, 400, size);
[b setTitle:#"Click Me" forState:UIControlStateNormal];
[b addTarget:self action:#selector(Action_Up) forControlEvents:UIControlEventPrimaryActionTriggered];
[self.view addSubview:b];
...
-(void)Action_Up {
[self ShowData];
}
in the simulator and on the real device I can select the button and press it, but the action is never fired.
Could you help ?
Try this.. it worked for me
button.addTarget(self, action: "someMethodName:", forControlEvents: .PrimaryActionTriggered)

iOS7 - Navigation Controller - Custom Back Button

I need to use custom Back button image for my new iOS 7 app. When I use Storyboard and add image by using Attributes inspector, this is what XCode renders for me:
Does anyone know why the Navigation controller behaves this way? I'm regularly putting #2x PNG image into Bar Button Item(under Navigation Item) in my Table View.
#interface MEDevicesViewController : UITableViewController
In my application, I use ECSlidingViewController and I have Table View within Navigation Controller. This is on my navigation controller.
Any help appreciated...
UIButton * button= [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 51, 31)];
[button setImage:[UIImage imageNamed:#"back_btn.png"]forState:UIControlStateNormal];
[button addTarget:self
action:#selector(backbtnpressed:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
-(IBAction)backbtnpressed:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

cocoa:How to obtain the View above all button?

I want obtain the View above all button,Button to set the background.The following code error
When the program is open,Settings button background.
NSButton *button = [(AppWindow *)self.window button];
if (button.tag==5) {
[button setImage:[NSImage imageNamed:#"menuBtnDown.png"]];
}

Selector is not called

I can't understand why the selector is not called.
//EDITED
[self.scrollView setContentSize:CGSizeMake(320, 600)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self
action:#selector(validateTextFields:)
forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"Продължи" forState:UIControlStateNormal];
btn.frame = CGRectMake(55, 580, 210, 50);
[self.scrollView addSubview:btn];
-(IBAction)validateTextFields:sender
{
NSLog(#"Called");
}
When I touch the button "Called" is not logged in console. If I change UIControlEventTouchUpInside to UIControlEventTouchDown validateTextFields method is executed.
You are adding a button on UIScrollView, this might be creating problem. Please check this question: iPhone: adding button to scrollview makes button inaccessible to interaction. It may be helpful.
The type for action should be
- (IBAction)validateTextFileds:(id)sender
{
// do your stuff
}
and your #selector parameter in addTarget should look like this: #selector(validateTextFields:)
try this:
[addContact addTarget:self action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
}

How to change the image of the button on click of the button in objective c

I have placed a pause button in UI, where when the user clicks the button, the image of button from pause image it has to change to the start image. I have placed both the start and the pause images in the bundle, I am able to do this,but when i click on start again , it should show pause image button,How to do this, the following is the code i am using,
-(IBAction)btnClked:(id)sender
{
[pauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
You need a variable to keep track of that state change. A BOOL is fine.
But using your code, you can "abuse" the tag property of UIButton to tell if it's playing (tag == 1) or paused (tag == 0)
-(IBAction)btnClked:(id)sender {
UIButton *btn=(UIButton *)sender;
if (btn.tag == 1) {
btn.tag = 0;
[btn setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
} else {
btn.tag = 1;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
}
You can use UIButton's selected state to do the same. For example, while initiating the button you have to define 2 images. One for normal state and other for selected state. When touch event of the button fired, you have to make it selected. If it is already selected you need to deselect it. This is much better, than maintaining additional variables to check selected state of the button. Also you could improve the performance with this approach.
Code example
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *startButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[startButton setBackgroundImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[startButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startButton];
}
- (IBAction)modeButtonClick:(id)sender {
UIButton *startButton = (UIButton *)sender;
[startButton setSelected:![startButton isSelected]];
}
If you are creating the button from story board or XIBs set the backgroud image for default state config and selected state config.