I would like to implement a method to add a done bottom to the right of the title navigation bar as so as some one starts entering text in a UITextView but for some reason nothing happens. The method gets called but does not add the button:
-(void)textViewDidBeginEditing:(UITextView *)textView{
UIBarButtonItem *doneButton;
[doneButton setTarget:self];
[doneButton setStyle:UIBarButtonItemStyleDone];
[doneButton setAction:#selector(keyboardDone:)];
[self.navigationItem setRightBarButtonItem:doneButton animated:YES];
}
Am I doing something really basic wrong here?
You need to alloc and init a UIBarButton item before it's ready for use. All you did was declare that "doneButton" was a UIBarButtonItem but you never properly set it up for use.
Related
I am trying to find a way to stop some of the processes within a detail controller (and let the user know that this is happening) when the back button in the navigation bar is pressed. However, I can't find a way to implement these changes when the button is pressed.
Is there a way to do this?
If you want to raise a user alert/notification before going back, you are really going to have to create your own back bar button item and assign it to self.navigationItem.leftBarButtonItem.
You need to hide the default button using:
self.navigationItem.hidesBackButton = YES;
Then add a target to your new button which does your process cleanup and raises an alert. In the alert handler, pop the controller once the user has acknowledged the alert.
The fastest and easiest way is to use custom back button like below;
-(void)viewDidLoad{
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(actionBack)];
[self.navigationItem setBackBarButtonItem:barButtonItem];
}
-(void)actionBack{
//PopViewController
}
Like any other button.
Put this in the .m file.
- (IBAction)saveButton:(id)sender {
//actions
}
Then control drag from the UINavigationBarButton to the IBAction
I have included numerous ViewControllers and as per the requirements, all the ViewControllers must contain the two same custom BarButton items(settings,help) in the right and so i created a separate class extending NSObject and included two UIButtons in it.
In all the ViewControllers, i imported this class and set barButtons like this:
NavigationBarButtonItems *nav=[[NavigationBarButtonItems alloc]init];
self.navigationItem.rightBarButtonItems = [nav BarButtonItems];
where, NavigationBarButtonItems is the class containing two custom Buttons in -(NSArray *)BarButtonItems; method.
Everything worked perfect. But Here is the problem, i created a viewController for one of the barButtons, say, 'settings' and i cant figure out how to present this ViewController(settings) when settings barButton is clicked as settings barButton is included in almost all the viewControllers.
Help me out. Thanks!
In the custom class Create the button like this.
-(void)createSettingbutton:(id)FromClass {
//Create Add category Button
UIButton *addSettingButton=[UIButton buttonWithType:UIButtonTypeCustom];
addSettingButton.frame=CGRectMake(0, 0, 30, 30);
[addSettingButton setBackgroundImage:[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"" ofType:#"png"]] forState:UIControlStateNormal];
**[addSettingButton addTarget:FromClass action:#selector(ButtonAction) forControlEvents:UIControlEventTouchUpInside];**
UIBarButtonItem *_addSettingButton=[[UIBarButtonItem alloc]initWithCustomView:addSettingButton];
[self.navigationItem setRightBarButtonItem:_addSettingButton];
[_addSettingButton release];
_addSettingButton=nil;
}
Now add the following code in the viewcontroller where you want to add the setting button.
[self createSettingbutton:self];//Second self is the parameter which is passed to 'FromClass' Variable.
Now in the same viewcontroller add the action for setting button
-(void)ButtonAction
{
[self.view addSubview:mSettingView.view];
//or
[self.navigationController pushViewController:mSettingView animated:YES]
}
Hi I have created a button programmatically. I will add this button to the navigation bar. Now I want to add a Touch Up Inside action listener to it. How do I do it? Thanks.
A UIButton is a subclass of UIControl.
All you need to do after creating an button is to set the target and action of the button. i.e.
// Create your button:
UIButton *button = // However you create your button
// Set the target, action and event for the button
[button addTarget:// the object that implements the action method, or nil if you want it to propagate up the responder chain.
action:// A selector for the method
forControlEvents:UIControlEventTouchUpInside];
Since you've added them to a navigation bar, it's slightly different, but basically the same. You will add the listener/handler at the same time you create the button(s). Here I've added << and >> to a navigation bar using the following:
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#">>" style:UIBarButtonItemStylePlain target:self action:#selector(navNextButtonPressed)];
UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:#"<<" style:UIBarButtonItemStylePlain target:self action:#selector(navPrevButtonPressed)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:nextButton, prevButton, nil];
and then create your handlers as normal:
#pragma mark - button handling
-(void)navNextButtonPressed
{
NSLog(#"Next pressed");
}
-(void)navPrevButtonPressed
{
NSLog(#"Prev pressed");
}
I have an edit rightbarbuttonitem in my view in navigation bar. I set it up with the help of storyboard/IB, not programmatically. Now, all i want is to assign an action when the "done" barbuttonitem is pressed (not edit).
Is there a way to achieve it? I tried manually through -(IBAction), but it's not working. Also, i want to perform the action on selected items in UITableView. So if you give me an idea, it would be great.
That button calls the method
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
You can implement it and it will get called everytime your edit/done button gets tapped. All you have to do is check the button's title property to see when it's showing done and when it's showing edit
If you declared your button as an IBOutlet then all you'd need to do is use the synthesised variable on your .m as so:
_yourBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(runMethod)];
self.navigationItem.rightBarButtonItem = _yourBarButton;
Then you'd have to declare your run method:
-(void)runMethod
{
//do stuff
}
I'm using this:
https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar
Anyone know how to get a modal view raised from say..the camera button (if the camera button were to be used for something else other than the camera)?
In BaseViewController.m the centre button is added in
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
Simply add the observer:
[button addTarget:self action:#selector(click:) forControlEvents:UIControlEventTouchUpInside];
And then obviously implement:
-(void)click:(id)sender{
[self presentModalViewController:[[UIViewController alloc] init] animated:YES];
}