How to create Navigation using ToolBar button - ios

I have created a Toolbar button and given action() to navigate to other ViewController.But the code is not working for Navigation.And not able to assign a image to the button.How can i solve this?
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 320, 44);
[self.view addSubview:toolbar];
[toolbar release];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonItemStyleBordered target:self action:#selector(updateAddress:)];
NSMutableArray *toolbarItems = [NSMutableArray arrayWithObjects:customItem, nil];
[toolbar setItems:toolbarItems animated:NO];
}
-(void)updateAddress{
DisplayMapViewController *updateView=[[DisplayMapViewController alloc]init];
UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:updateView];
[self.navigationController popToViewController:navi animated:YES];
}

First you should set the selector right, you should take out the : from the selector because you don't have parameters for the updateAddress method
should be like this
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonItemStyleBordered target:self action:#selector(updateAddress)];
Then You need to initialize the DisplayMapViewController with its NibName
DisplayMapViewController *updateView=[[DisplayMapViewController alloc]initWithNibName:#"DisplayMapViewController" bundle:nil];
and then just pop to the view you are intended to.
[self.navigationController popToViewController:updateView animated:YES];
if you have already rootViewController then you could just popToRootViewController

Related

UIPickerView Only Responds Within Area of Tab Bar

I'm updating my app to iOS 8 and trying to recreate the UIPickerView inside a UIActionSheet with UIAlertController as described here. Using the answer from Nada Gamal, I was able to recreate the functionality, but the UIPickerView only responds within the tab bar (even though the tab bar is hidden behind the picker view). The tab bar controller is the root view controller.
-(void)loadPickerView{
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.backgroundColor = [UIColor whiteColor];
[pickerView selectRow:pickerViewRow inComponent:0 animated:NO];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
pickerToolbar.tintColor = self.navigationController.navigationBar.tintColor;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
UIAlertController * searchActionSheet=[UIAlertController alertControllerWithTitle:#"" message:#"" preferredStyle:UIAlertControllerStyleActionSheet];
[ searchActionSheet.view setBounds:CGRectMake(8, 208, self.view.frame.size.width, pickerView.frame.size.height+pickerToolbar.frame.size.height)];
[searchActionSheet.view addSubview:pickerToolbar];
[searchActionSheet.view addSubview:pickerView];
[self presentViewController:searchActionSheet animated:YES completion:nil];
}
The presentViewController in the last line seems to be the issue. I've tried many different ways to get the pickerView to respond outside the tab bar area, including:
self
self.tabBarController
self.navigationController (pickerView doesn't show up at all)
[[UIApplication sharedApplication] keyWindow].rootViewController
The other option is to make a separate view controller for this, but I'm concerned that won't work either because UIAlertController is a subclass of UIViewController, but perhaps this is an issue with adding subviews to UIAlertController.

blank Toolbar from setItems

My UINavigationController toolbar is blank and no items show up on it. Here is my code
self.navigationController.toolbarHidden = NO;
UIToolbar* toolbar = self.navigationController.toolbar;
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
[toolbar setItems:items animated:YES];
That's not the proper way to setup the toolbar. UIViewController has a toolbarItems property. The nav controller will use that property to automatically populate its toolbar.
Your code should be:
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *btnTest = [[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
self.toolbarItems = #[ btnTest ];

UIBarButtonItem not firing its action method

In viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:NO];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Source" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Aa" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Rabbit" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Method:
- (void)backButtonTapped {
[self.navigationController popViewControllerAnimated:YES];
}
I have a breakpoint on the call inside the method but it never gets called. Why is this method never getting called?
I had a UITapGestureRecognizer on the whole view that intercepted the tap on the UIBarButton. I solved it thanks to this answer, which basically stopped the UITapGestureRecognizer from beginning unless it was outside of the UIToolBar.
Your code is not complete and I dont know if you didn't implement it or just didn't wrote it here, so just to make sure, this should work:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Try like this. Its working fine.
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:#"back"style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
[self.view addSubview:toolbar];

UIToolbar creation

i have created a toolbar as below shown code and now i dont knw how to add the items to it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 350, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[[UIBarButtonItem alloc] init] autorelease]];
[toolbar setItems:items animated:YES];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
Try this code
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"Item" style:UIBarButtonItemStyleBordered target:self action:#selector(action:)];
NSArray *items = [NSArray arrayWithObjects: customItem, nil];
[toolbar setItems:items animated:NO];
[customItem release];
You can also refer to this link:
How to create this toolbar programmatically
Please do google search and Stack Overflow search before posting.
use
[self setToolbarItems: buttons animated:NO];
as u want to set toolbaritems and uiviewcontroller has toolbar itself.

How to get a UIBarButtonSystemItem on a UIToolBar to take me to another view

Accept my early apologies as totally new to this so my terminology may not be accurate, hope you can understand what it is that i'm asking.
I am having issues getting a UIBarButtonSystemItem to take me back to a different view when pushed. I have defined a UIToolBar with a UIBarButtonSystemItem on it which compiles and works perfectly. However, when the button is pushed the app crashes.
This is the code i have used to define the UIToolBar and UIButton:
//create toolbar using new
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 410, 320, 50);
//Add buttons
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(pressButton1:)];
I have then added flexitem to this and added the button to the array using the following code:
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, nil];
This is where I fall down and I have attempted to get the button to take me to a previous view when pushed by using the following code:
-(void)pressButton1:(id)sender {
BMR_TDEE_CalculatorViewController *controller1 = [[BMR_TDEE_CalculatorViewController alloc] initWithNibName:#"BMR_TDEE_ViewController" bundle:nil];
controller1.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller1 animated:YES];
[controller1 release];
}
Like I said, this just crashes my simulator. It compiles without issue but Im guessing the code is fundamentally wrong as i am new to this. Any help explained dumbed down would be fantastic ;-)
Thanks All
This:
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(pressButton1:)
should be:
UIButton *button=[[UIButton alloc] init];
button.frame=CGRectMake(0, 0, 65, 32);
[button addTarget:self action:#selector(pressButton1:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
For the (void)pressbutton code it should now be:
-(IBAction)pressButton1:(id)sender {
BMR_TDEE_CalculatorViewController *controller = [[BMR_TDEE_CalculatorViewController alloc] initWithNibName:#"BMR_TDEE_CalculatorViewController" bundle:nil];
[self.navigationController presentModalViewController:controller animated:TRUE];
}

Resources