Custom UIButton to drill up from table view? - ios

Ive put in a custom "Back" UIBarButtonItem, which is from an Image i made in photoshop. Ive managed to make it appear just fine, my problem is making it work.
My button does nothing, im assuming because i havent assigned any actions to it. I just dont know the correct syntax to do so?
Heres my code
UIImage *buttonImage = [UIImage imageNamed:#"BackButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
// Im assuming this is where i need to put the action...
//[backButton addTarget:self action:#selector(.......) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:backButton];
Basically, i want my new custom backbutton to function just like the default one in a regular UITableView
regards,
Andyy

You can do this in IB or you can do it in code with a call to addTarget:action:forControlEvents: for UIControlEventTouchUpInside to invoke a method (action) when the button is tapped. For example:
[backButton addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
This is documented in UIControl class reference, UIButton is a subclass of UIControl.
You'll need a method which will be called when the event occurs, which I called backButtonPressed: in my code above. This method takes one parameter, the object sending the event, something like this:
-(void)backButtonPressed:(id)sender
{
// NOTE: assumes self is your UIViewController
[self.navigationController popViewControllerAnimated:YES];
}

Related

how to call target method for a button implemented in base class

In my project i have a Baseclass : UIViewController.I have created a UIButton programmatically and a target method for that button in base class.
I have created a view controller which inherits from base class.The problem is the button is reflected on the screen but nothing happens when i click on it. Please guide how can i create an Action for the button inherited from base class
//method in base class that addss button
-(void)addRHSRoundButton{
UIImage *imgNotSelected = [UIImage imageNamed:#"MENU_NOT_SELECTED.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:imgNotSelected forState:UIControlStateNormal];
button.frame = CGRectMake(978, 340, imgNotSelected.size.width/2, imgNotSelected.size.height/2);
[button addTarget:self action:#selector(roundButtonClicked) forControlEvents:UIControlEventTouchUpInside];
button.tag= 101;
[self.view addSubview:button];
}
Your method doesn't match you create target with method roundButtonClicked but you expect method RHSRoundButtonClicked to be run.
It's good if you pass button as a parameter. Amend your method like that:
-(void)RHSRoundButtonClicked:(id)sender
{
NSLog(#"btnClicked"); button.hidden = YES;
}
And change the line where you create button target to:
[button addTarget:self action:#selector(RHSRoundButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//Extended
If you want to call super class method you can call super instead of self:
[button addTarget:super action:#selector(RHSRoundButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
This should help.

Intercept back navigation in iOS7

I want to stop somebody going back on my Navigation Controller if they haven't saved their changes. Most posts on SO discuss overriding the back button (with a variety of techniques to do this). However, iOS7 allows you to now swipe to go back in a navigationViewController...
I did see the UINavigationControllerDelegate which looks like the right type of delegate I'd want to implement but I see no way to cancel a navigation action. Any ideas how to do this?
Unfortunately nothing changed in iOS7, you still need to fake your back button if you want to put some check into it.
By the way it easier now since you don't need to fake the arrow button.
Edit:
to do that:
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 44.0f, 30.0f)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popVC) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Adding action and target to a custom Navigation BarButtonItem?

I am trying to customize the Navigation Bar by adding a custom button to the rightBarButtonItem. Creating one is pretty straight forward.
UIImage *myImage = [UIImage imageNamed:#"menu-icon.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:CGRectMake(0, 0, myImage.size.width, myImage.size.height)];
[myButton setBackgroundImage:myImage forState:UIControlStateNormal];
UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[navItem setLeftBarButtonItem:myButtonItem animated:YES];
Looks good, except that now I can't set the target or action properties. The following code does nothing:
myButtonItem.target = myTarget;
myButtonItem.action = #selector(myButtonAction);
I found that the reason it's not working is because I initialized the UIBarButtonItem with -initWithCustomView. Apples documentation says:
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
I'm a little confused by what exactly apple means by that. Am I supposed to create a custom control or something? I haven't really done that yet and I'm not sure where to start.
Does anyone have any suggestions on how to do this or an alternative way to set up a custom Navigation Bar Button?
ANSWERED----------------------------------------------------------
Just had to add this method to the UIButton:
[myButton addTarget:myTarget action:#selector(myButtonAction) forControlEvents:UIControlEventTouchUpInside];
It's pretty straight forward, you need to add target for uibutton not for uibarbuttonitem.
To explain more what you want for uibarbuttonitem is respond touch up inside action method, as documentation says that you need to do that for uibutton as you are initializing your uibarbuttonitem with custom view in your case uibutton.
Add an action and target to a UIButton like this:
[myButton addTarget:self
action:#selector(handleMyButtonTap)
forControlEvents:UIControlEventTouchUpInside];
Add an action and target to a UIBarButtonItem like this:
[myBarButtonItem setAction:#selector(handleBarButtonTap)];
[myBarButtonItem setTarget:aTarget];
With the help of answers by hgwhittle and Mustafa I implemented and it works perfect
here is the complete snip may help someone
//Create ImageButton
UIImage *shareBtnIcon = [UIImage imageNamed:#"dummy_share_icon_22"];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setFrame:CGRectMake(0, 0, shareBtnIcon.size.width, shareBtnIcon.size.height)];
[shareButton setBackgroundImage:shareBtnIcon forState:UIControlStateNormal];
[shareButton addTarget:self action:#selector(shareButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//Create BarButton using ImageButton
UIBarButtonItem *shareBarBtn = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
//Add BarButton to NavigationBar
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:shareBarBtn, nil];
//method for share butto click
-(void) shareButtonClicked:(UIButton*)sender
{
NSLog(#"you clicked on share button %ld");
}

How to pass a variable to a method that is called when UIButton is triggered?

I have a programmatically created UIButton with a UIScrollView. When this button is pressed, it triggers an action, but besides just triggering the action, I want the button to send a (int) variable to the method as well.
Here is my button:
UIButton *btn15 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn15 setTitle:#"Cool title" forState:UIControlStateNormal];
[btn15 setFrame:CGRectMake(165, 770, 145, 145)];
[btn15 addTarget:self action:#selector(selectFav) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn15];
...So this is my 15th button within this scroll view. I want to write the method 'selectFav' to handle a button press. I would like each button to send to the method it's number, and then have the method print the number out to NSLog. Since this example is button 15, I want to have the the number 15 somehow passed to selectFav.
I'm assuming this is an easy thing to do, but can't seem to make it work. Thanks for any suggestions!
You can use UIView's tag property into the button, and then take the value out of the button once it's pressed.
something like:
- (void)yourAction:(UIButton *)sender{
NSInteger value=[sender tag];
}
This is easy to do in BlocksKit. BlocksKit allows you to attach blocks as event handlers.
UIButton *btn15= ...;
[btn15 addEventHandler:^(id sender) {
[self selectFav:15];
} forControlEvents:UIControlEventTouchUpInside];
- (void)mySelectFav:(NSUInteger)buttonId {
...
}
This is how I would do it. Set a tag property of the button and then get to the property in button action.
-(void)myMethod
{
UIButton *btn15 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn15 setTitle:#"Cool title" forState:UIControlStateNormal];
[btn15 setFrame:CGRectMake(165, 770, 145, 145)];
[btn15 addTarget:self action:#selector(selectFav:) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn15];
btn15.tag = 15;
}
-(void)selectFav:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", button.tag); //this will print 15. Store it in a int variable
}
You can use the tag of the UIButton being passed as mentioned by #J2theC, or you can also add a property to UIButton: Subclass UIButton to add a property
As you are passing the UIButton to the selector, you can identify which UIButton is being passed from the tag or the property.
Also do not forget to update your addTarget method implementation to include : in the selector parameter being passed:
[btn15 addTarget:self action:#selector(selectFav:) forControlEvents:UIControlEventTouchUpInside];

Custom UIBarButtonItem back button

I need to customize UIBarButtonItem back button so it is displayed with a simple arrow icon (like in Facebook, Twitter etc). Do I need to create a custom UIBarButtonItem and manually add it to the nav bar or is there a way to do this through appearance API?
Thanks
You have to make your own and add it
You can do it like this (this is for a custom button using an image)
UIImage* image3 = [UIImage imageNamed:#"favorites.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton2 = [[UIButton alloc] initWithFrame:frameimg];
[someButton2 setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton2 addTarget:self action:#selector(loadFavorites)
forControlEvents:UIControlEventTouchUpInside];
[someButton2 setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *favoritesButton =[[UIBarButtonItem alloc] initWithCustomView:someButton2];
self.navigationItem.rightBarButtonItems =[NSArray arrayWithObjects:favoritesButton, nil];
Okay so it turns out that you can't actually use backBarButtonItem with a UIBarButtonItem created with a custom view. The best I can offer is you can create a template with an image and a empty background image, although it feels a bit like spacer.gif. I threw together a quick example to show what I meant about the view controller ordering too.
http://cl.ly/36091p0l0120

Resources