ACTION for UIBUTTON in iOS - ios

I have one custom class with subclass of UIView, inside this I created one button dynamically.
Now I want to set Action for button method. I set normal like UIviewController.
But its not working.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *but=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:but];
// Initialization code
}
return self;
}
-(void)aMethod
{
NextEyeViewController *nexteye=[[NextEyeViewController alloc]initWithNibName:#"NextEyeViewController" bundle:nil];
[self presentViewController:nexteye animated:YES completion:nil];
}
-(void)aMethod is my method name for button

If you want to set an action to your button created programmatically you need to do like this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 160.0, 40.0);
[view addSubview:button];

Create UIButton:
UIButton *yourButton = [[UIButton alloc] initWithFrame:frameButton];
// Here code to set button properties: color, label...
[yourButton addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:yourButton];

Your method has to receive the sender of the action:
-(void)aMethod:(id)sender {
// your code
}
Update:
You also have to call your method suitably with #selector(aMethod:) instead of #selector(aMethod).

Related

How I can add action to title of NavigationBar?

I want to give/add action to the title of navigation title.
(Example "Explore")
I want to perform some activity using this approach.
I tried following code, but it's not working.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Explore" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 100);
//[view addSubview:button];
[self.navigationItem.titleView addSubview:button];
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(#"Clicked on Navigation Title");
}
ViewController name is came from presentViewController type not from push.navigationController stack.
How I can achieve this using objective-C code?
Replace your code with the following:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Explore" forState:UIControlStateNormal];
self.navigationItem.titleView = button;
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(#"Clicked on Navigation Title");
}
titleView is nil by default.
https://developer.apple.com/documentation/uikit/uinavigationitem/1624935-titleview
You should set your title view with your button. Not add it as a subview.
self.navigationItem.titleView = button

I want to hide button Objective-C ios?

When I define the button on .h, it works but when I code the button does not work.
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"nl.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(230,302,32,32);
[button setImage:buttonImage forState:UIControlStateNormal];
//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(clickActionItem) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)btnClicked
{
[self.button Sethidden:YES]:
self.button.hidden=YES;
_button.hidden=YES;
}
But not work
Issue
The problem is that you are creating UIButton on viewDidLoad Method and you don't keep the instance stored so you cannot access it. Another issue is that you are giving another method to selector of the button.
Solution
Change your target at selector and add the UIButton as parameter just like the code below
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
And the method btnClicked take the instance of the button which is passed at button
-(void)btnClicked:(UIButton *)button
{
[button sethidden:YES];
}
This should do the trick.
#implementation YourView{
UIButton *button;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"nl.png"];
//create the button and assign the image
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(230,302,32,32);
[button setImage:buttonImage forState:UIControlStateNormal];
//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)btnClicked
{
[button setHidden:YES];
}
Your code did not give sufficient information for to see what you intend to do. Can you also provide the header file?

Setting selectors for global UIButton

I want to return a global UIButton from a global class.
+(UIBarButtonItem *) globalButton {
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0.0, 0.0, 40, 40);
[myButton addTarget:self action:#selector(???)
forControlEvents:UIControlEventTouchUpInside];
return myButton;
}
So if I call [Global globalButton] from my viewController, I can get this button. The problem I'm having is how to set the selector, if I'm calling if from my view controller?
You should change your code as follow, assuming you would be calling it from UIViewController
+(UIButton *) globalButton:(UIViewController*)delegate {
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0.0, 0.0, 40, 40);
if([delegate respondsToSelector:#selector(gloBalButtonTapped:)])
[myButton addTarget:delegate action:#selector(gloBalButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
return myButton;
}
//Below method should be present in your view controller than only it will be added to myButton click event.
-(IBAction)gloBalButtonTapped:(id)sender{
}
You should return UIButton only if you are creating UIButton, if you need to return UIBarButtonItem than you should create UIBarButtonItem or Xcode will give warning, just a suggestion though.
Just dont add target in Global Class wherever you want to use it just use it like this.
UIBarButtonItem *btn = [Global globalButton];
btn addTarget:self action:#selector(yourSelector)
forControlEvents:UIControlEventTouchUpInside];
Or you can make globalButton a property in GlobalClass and write this method in setGlobalButton and then you can use it as a property in any viewController.
Or if you want to control it in Global only then you can mention your selector mentioned in this class only. And in that method you can control the behaviour.
Try this in your controller:
UIBarButtonItem* button = [UIBarButtonItem globalButton];
[button addTarget:self action:#selector(yourDidTapAction:) forControlEvents:UIControlEventTouchUpInside];
or this in globalButton:
+(UIBarButtonItem*)globalButtonWithTarget:(id)target andAction:(SEL)action
{
// ...
[myButton addTarget:target action:action
forControlEvents:UIControlEventTouchUpInside];
// ...
}
I found the easiest way to do this is below:
+(UIBarButtonItem *) buttonWithSelector:(SEL)selector andSender:(id)sender {
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(-40.0, 10.0, 40, 40);
[myButton addTarget:sender action:selector forControlEvents:UIControlEventTouchUpInside];
return myButton;
}

Update UIButton title after receiving reply from SKProductsRequest for showing in-app purchases price

I'm trying to create two buttons whose label is initially set as a default value. The two buttons are managed by a popOverViewController, in which the viewDidLoad method initialise all the controls. I also have another method called addButtons that gets executed by the AppDelegate after receiving a valid response from Apple's servers containing products descriptions and prices.
My problem is that I can't get the label updated with that method.
My viewDidLoad method in the popOverViewController is:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Add a label to the popover's view controller.
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(button1Week:)
forControlEvents:UIControlEventTouchDown];
NSString *priceWeekly = [NSString stringWithFormat:#"weekly subscription"];
[button setTitle:priceWeekly forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 200.0, 50.0);
[button2 addTarget:self
action:#selector(button1Month:)
forControlEvents:UIControlEventTouchDown];
[button2 setTitle:#"monthly subscription" forState:UIControlStateNormal];
button2.frame = CGRectMake(0.0, 55.0, 200.0, 50.0);
[self.view addSubview:button];
[self.view addSubview:button2];
}
and my update label method, of which I'm sure it gets called at the right time, is:
-(void)addButtons {
[self.button setTitle:#"updated label" forState:UIControlStateNormal];
[self.button2 setTitle:#"updated label 2" forState:UIControlStateNormal];
}
can you please check my code and suggest the right way to update the labels? Basically I want to initialize the controls with a default label because of the waiting time between controls creation and server response.
UPDATE:
I have updated the code as follows, but still everything that's inside the addButtons method gets executed as I have an NSLog to check, but the button title still remains as created in viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Add a label to the popover's view controller.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ilManifestoAppDelegate *delegate = (ilManifestoAppDelegate*)[[UIApplication sharedApplication] delegate];
[button addTarget:self
action:#selector(button1Week:)
forControlEvents:UIControlEventTouchDown];
// [button setTitle:priceWeekly forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 200.0, 50.0);
[button2 addTarget:self
action:#selector(button1Month:)
forControlEvents:UIControlEventTouchDown];
// [button2 setTitle:#"monthly subscription" forState:UIControlStateNormal];
button2.frame = CGRectMake(0.0, 55.0, 200.0, 50.0);
// [self addButtons];
NSString *weekPrice = [NSString stringWithFormat:#"settimanale a %#",delegate.priceWeek];
[button setTitle:weekPrice forState:UIControlStateNormal];
[button2 setTitle:#"updated label 2" forState:UIControlStateNormal];
NSLog(#"week price %#", weekPrice);
[self.view addSubview:button];
[self.view addSubview:button2];
}
-(void)addButtons {
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setTitle:#"modificato" forState:UIControlStateNormal];
[self.view addSubview:button];
NSLog(#"addButtons executed");
}
In viewDidLoad you are using button and button2. In addButtons you are using self.button and self.button2. Did you ensure that these are the same buttons?
Also place a breakpoint in addButtons to see if it really is called, if you haven't done this yet.
Have you also created the outlet to the buttons in the xib, if you haven't placed them only with code.

IBAction & Buttons programmatically

I'm creating buttons programmatically and I then want to add functionality whereby when they are tapped on/pressed, they stay highlighted unless tapped on again. What I'm doing now is creating the buttons and then trying to add an IBAction. However, the issue is I have my button creation in a method and then I'm not sure how to reference the button in my IBAction. Here's my code:
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"test_butn_un.png"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"test_butn_pressed.png"] forState:UIControlStateHighlighted];
[testButn addTarget:self action:#selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentview addSubview:testButn
-(IBAction)staypressed:(id)sender{
//Not sure what to do here, since this method doesn't recognize testButn, How do I reference testButn
The sender is testButn. You should change stayPressed's argument type from (id) to (UIButton *)
Unless an action method is connected to several different classes of objects, it's best to replace the id with whatever object class you're using. This can be useful if you're hooking things up in IB, since it won't let you hook it to the wrong kind of object.
The fact that it's not working isn't because it doesn't recognize your button. Your approach is wrong. I think you need to have an action connected to touch down, and maybe set the selected state to YES. In your button definition, you need to set an imageForState: for the selected state. The way you're doing it now, that method isn't called until touch up.
Something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"New_PICT0019.jpg"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"New_PICT0002.jpg"] forState:UIControlStateSelected];
[testButn addTarget:self action:#selector(stayPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:testButn];
}
-(void)stayPressed:(UIButton *) sender {
if (sender.selected == YES) {
sender.selected = NO;
}else{
sender.selected = YES;
}
}
You need to cast sender to UIButton.
- (IBAction)staypressed:(id)sender
{
UIButton *theButton = (UIButton*)sender;
//do something to theButton
}
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"test_butn_un.png"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"test_butn_pressed.png"] forState:UIControlStateHighlighted];
[testButn addTarget:self action:#selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
testButn.tag = 1;
[self.contentview addSubview:testButn
-(IBAction)staypressed:(id)sender
{
if ([sender tag]==1)
{
somecodes...
}
}

Resources