Dismiss ModalView Programmatically by done button - ios

I have to click on done button to dismiss modalview programmatically. I think UIButton is better than UIBarButtonItem to add UIControlEventsTouchupInside.
But with UIButton I'm confused what buttontype should use.
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(dismissViewaction:)] autorelease];

You will most likely want to use UIButtonTypeRoundedRect or UIButtonTypeCustom
With the custom type, you can add images for display.
You can try "stealing" these images from the UIBarButtonItem (image property defined in UIBarItem) and making the custom button look like the UIBarButtonSystemItemDone button
UIBarButtonItem * buttonForImage = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil] autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:buttonForImage.image forState:UIControlStateNormal];
Things to look out for. When setting an image for a UIButton, it does not scale to the size of the button according to contentMode property. If you want the image to follow the rules of the contentMode property, use setBackgroundImage: forState: instead.

I think the type of button doesn't matter. You have to assign an action or a selector to the button.
Something like this:
UIBarButtonItem *bttItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(yourBttAction:)] autorelease];
the action:
- (IBAction) yourBttAction:(id)sender
{
NSLog(#"Done Button clicked");
//do something
}
If the button is on the modalViewController, I usualy, for dismiss it, I use:
[self dismissModalViewControllerAnimated:(BOOL)];
or
//if you have a navigationController
[self.navigationController dismissModalViewControllerAnimated:(BOOL)];
But if you want to use a delegate to dismiss it, take a look at this tutorial

Related

how to make the add and edit button like in the alarm application

I need to make two button (add and delete) in the top of the view in my application just like the alarm clock.
how can i put like these buttons can anyone help me?
many thanks,
Initiate UIBarButtons in ViewDidLoad method
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addButtonTapped:)];
UIBarButtonItem *editButton =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(editButtonTapped:)];
Place them in navigationBar as NavigationItem
self.navigationItem.rightBarButtonItem = addButton;
self.navigationItem.leftBarButtonItem = editButton;
If you want to use two buttons on same side of NavigationBar use
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:editButton,addButton, nil]];
These are called UIBarButtonItems and you need to instantiate them and add to your navigationItem as follows in your viewDidLoad method of your view controller.
self.navgiationItem.leftBarButtonItem = yourButtonInstance;
P.S. if you please ask your question with more detail like supporting it with screen shots, etc would be more helpful for answer.
This SO question and answer is exactly what you need.
If you are looking to add one in Navigation bar
-(void)setNavigationBarRightButton
{
UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[rightButton setImage:[UIImage imageNamed:#"setting.png"] forState:UIControlStateNormal];
[rightButton setShowsTouchWhenHighlighted:TRUE];
[rightButton addTarget:self action:#selector(onClickrighttButton:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
// self.navigationItem.hidesBackButton = TRUE;
self.navigationItem.rightBarButtonItem = barBackItem;
}
- (void)onClickrighttButton:(id)sender
{
NSLog(#"right button");
}
and in viewdidload
[self setNavigationBarRightButton];
You can add any image you like

Info button as UIBarButtonItem doesn't respond to taps

I'm trying to programmatically add and info button to my app's navigation. However, it doesn't ever fire the action that I'm registering to it.
UIBarButtonItem* infoButton =[[UIBarButtonItem alloc] initWithCustomView:[UIButton buttonWithType:UIButtonTypeInfoLight]];
[infoButton setTarget:self];
[infoButton setAction:#selector(infoButtonTapped)];
The button shows up in my nav bar, but clicking on it doesn't pass into infoButtonTapped
Add the target/action to the UIButton. The bar button item behaves differently when it's set up from a UIButton.
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(infoButtonTapped) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:button];

How to prevent animation of back button when switching views?

I cannot figure out how to disable the back-button animation that occurs in the navigation bar when switching from a tableview to a standard view (when a cell is selected). There is no obvious line of code that enabled animation to begin with. Here it is in gif-form:
The navigation buttons in the Facebook app do not animate, so it is possible.
It may be relevant to mention that I am using the ViewDeck library to create the Facebook-like tableView menu, i.e. swipe to the right to expose a table.
EDIT: solution is based on Hesham Abd-Elmegid's answer but modified to use a custom image...
UIImage *settingsImage = [UIImage imageNamed:#"back_button#2x.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(280.0, 10.0, 29.0, 29.0);
[backButton setBackgroundImage:settingsImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 50, 30);
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBarItem;
If you set a custom UIBarButtonItem as a left navigation item (instead of standard back button item), it will fade instead of slide in, just like in Facebook's app. Just create a simple method that will replace back button functionality by calling popViewControllerAnimated: on the navigation controller in which your detail view controller is contained.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(goBack)] autorelease];
}
- (void)goBack
{
[self.navigationController popViewControllerAnimated:YES];
}
Note: UIBarButtonItem can also be set up with an image using initWithImage:style:target:action: method.
You could replace the back button with a custom UIButton. That way it won't animate on transition.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Back" forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 50, 30);
[backButton addTarget:self action:#selector(onBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
You will have to find a PNG for the arrow shape of the back button though.

How to make leftBarButtonItem looking like backBarButtonItem?

The default solvings are not appropriate:
to change the previous ViewController title - I need to make my own function controlling the button touches up
to make a leftBarButtonItem and hide backBarButtonItem - leftBarButtonItem doesn't look like a default backBarButtonItem.
Here's an example to create a custom leftBarButtonItem:
UIImage *buttonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *aButton = [UIbutton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
[aButton addTarget:self action:#selector(aSelector) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
self.navigationItem.leftButtonItem = backButton;
Hope it helps...
Edit:
Try this...
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"yourimage.jpg"] style:UIBarButtonItemStyleBordered target:self action:#selector(aSelector)];
self.navigationItem.leftBarButtonItem = backButton;
I don't remember what type of button is the backbutton, try to change the default style to other. Good luck!
Just create a custom barbuttonitem and customize it with a picture similar to the back button item.
You can get its text from the view controllers in the navigation stack.
Then handle the taps to implement the desired effect
You can assign your custom views as the bar buttons to your navigation controller, like this:
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomizedControl];
[yourCustomizedControl release];
self.navigationItem.leftBarButtonItem = customItem;
[customItem release];
Check this Apple documentation for a more detailed explanation, if you want.
Here is a hack to add a back-like button:
UINavigationItem* backNavigationItem = [[UINavigationItem alloc] initWithTitle:#"Back"];
[_navigationBar pushNavigationItem:backNavigationItem animated:NO];
[backNavigationItem release];
You can add more items (such like title, right button) this way:
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"Title"];
navigationItem.rightBarButtonItem = // Some UIBarButtonItem
[_navigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];

UIButton not showing in iOS

Following is a code to display a button to the toolbar.
UIButton myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *myImage = [UIImage imageNamed:#"img1.png"];
[myButton setBackgroundImage:myImage forState:UIControlStateNormal];
UIBarButtonItem *myToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
Nothing gets displayed on the toolbar. Please tell me what is wrong.
[myToolbar setItems:[NSArray arrayWithObject:myToolbarButton]]; // might help
OR if it's a navigation bar
self.navigationItem.rightBarButtonItem = myToolbarButton;
Also you should set the frame for the custom button, something like this:
UIButton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake:0,0,200,50];
Then if you want to add a selector/callback:
[myButton addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = myButtonItem; // and change for the right button.
Where self is a UIViewController.
You forgot to add this button to the toolbar!
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myToolbarButton,nil]; // you can more buttons here
[aToolbar setItems:toolbarItems animated:NO]; // change to YES if you want to have nice animation
[toolbarItems release];
If you want to add a button to navigation bar, use this:
[self.navigationItem setRightBarButtonItem:myToolbarButton]; // or setLeft...
Also, you should remember to set the frame of myButton:
[myButton setFrame:CGRectMake(0, 0, 30, 32)];

Resources