How to add button to navigation controller - objective-c

Here is my code..I am unable to add a button in my navigation controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2, *viewController3,*viewController4;
viewController1 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
viewController2 = [[DisplayAllImagesController alloc] initWithNibName:#"DisplayAllImagesController" bundle:nil];
viewController3 = [[EmptyView alloc] initWithNibName:#"EmptyView" bundle:nil];
viewController4 = [[ListView alloc] initWithNibName:#"ListView" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController2, viewController1, viewController3, viewController4, nil];
self.tabBarController.title = #"Title";
self.navController = [[UINavigationController alloc]
initWithRootViewController:self.tabBarController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
So,please let me know how i can add button to navigation controller...please...

just add this bellow code in your ViewController's viewDidLoad: method...
UIBarButtonItem *btnReload = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(btnReloadPressed:)];
self.navigationController.topViewController.navigationItem.rightBarButtonItem = btnReload;
btnReload.enabled=TRUE;
btnReload.style=UIBarButtonSystemItemRefresh;

Try this:
UIButton *customButton = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 50,30)];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.rightBarButtonItem = closeButton;

if you want custom button we can use this
UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton1 setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
myButton1.showsTouchWhenHighlighted = YES;
myButton1.frame = CGRectMake(0.0, 3.0, 50,30);
[myButton1 addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1];
self.navigationItem. rightBarButtonItem = rightButton;

//create the button and assign the image
UIImage *buttonImage = [UIImage imageNamed:#"button.png"];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setFrame:CGRectMake(280,7,buttonImage.size.width,buttonImage.size.height)];
[rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
rightButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[rightButton setTitle:#"Right" forState:UIControlStateNormal];
// add image view
[self.navigationController.navigationBar addSubview:rightButton];
[self.navigationController.navigationBar bringSubviewToFront:rightButton];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarDone = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = customBarDone;
[rightButton addTarget:self action:#selector(yourSelector:) forControlEvents:UIControlEventTouchUpInside];

UIButton* btn =[UIButton alloc] init];
....
self.navController.view addsubview:btn];

Related

Cannot push back to view controller embed in Navigation Controller

I have created ViewController A (a.k.a vcA) and vcB embed in Navigation View Controller ,mapping child view controller in storyboard as classes.m
When I try to construct this redirect of view controller A->B->A , or to create vcA from vcB , by using :
A :
PairViewController * sliderVC = [self.storyboard instantiateViewControllerWithIdentifier:#"PairViewController"];
sliderVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:sliderVC animated:NO completion:nil];
sliderVC.view.backgroundColor = [UIColor clearColor];
B : ViewController *destinationController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[destinationController setBle:_ble];
[destinationController setLeDevice:selectedDevice];
destinationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:destinationController animated:NO completion: nil] ;
The new A is created without any navigation bar appeared.
When I use
[self.navigationController pushViewController:destinationController animated:nil];
The xCode said for the exception ;
Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'
Would you please tell me the way to create vcA from vcB , with my custom navigation bar keeping intact , not missing ?
In A I try to construct the navigation bar in this method but navigation bar turns nil if creating vcA from vcB
-(void)viewWillLayoutSubviews{
navigationBar= [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
}
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO];
navigationBar = self.navigationController.navigationBar;
[navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:#"TitilliumText22L-Medium" size:22.0], NSFontAttributeName,
nil] ];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"FPV Control"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
[button setImage:[UIImage imageNamed:#"menu_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem *buttonItemA = [[UIBarButtonItem alloc] initWithCustomView:button];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(buttonClicked:)];
[leftBarButtonItem setCustomView:button];
navigationItem.leftBarButtonItem = leftBarButtonItem;
UIButton *buttonA = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 20)];
[buttonA setImage:[UIImage imageNamed:#"rc_logo.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItemB = [[UIBarButtonItem alloc] initWithCustomView:buttonA];
navigationItem.rightBarButtonItem = buttonItemB;
[navigationBar pushNavigationItem:navigationItem animated:NO];
NSLog(#" navigaytion item : %#" , navigationItem);
NSLog(#" navigaytion bar : %#" , navigationBar);
So present your ViewController with NavigationController as below.
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:myViewController];
//now present this navigation controller modally
[self presentViewController:navigationController
animated:NO
completion:nil];

how do you add a right button to a uinavigationcontrol

How can I add a button to a UINavigationControl that I added to a ViewController? (The project isn't UINavigationControler based.)
Here's the code I've added to my viewDidLoad method:
navigation = [[UINavigationController alloc] init];
navigation.view.frame = CGRectMake(0, 0, 1024, 748);
navigation.navigationBar.tintColor = [UIColor blackColor];
navigation.navigationBar.alpha = 1.0f;
navigation.navigationBar.translucent = NO;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
[navigation.navigationItem setRightBarButtonItem:cancelButton animated:YES];
[cancelButton release];
[self.view addSubview:navigation.view];
[self pushPresentationList];
Can someone help me fix lines 7-9 please?
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Button"
style:UIBarButtonSystemItemDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Awesome"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[navigationBar pushNavigationItem:item animated:NO];
Do you need an UINavigationController? Why not just use a simple UINavigationBar. Here is some sample code:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:
CGRectMake(0,0,self.view.frame.size.width, 49.0f)];
[self.view addSubview:navBar];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
UINavigationItem *rightBarButtonItem = [[UINavigationItem alloc] init];
[rightBarButtonItem setRightBarButtonItem:cancelButton];
[navBar pushNavigationItem:rightBarButtonItem animated:NO];

UIBarButtonItem target-action does not invoke

- (void)viewDidLoad
{
[super viewDidLoad];
//Load the image
UIImage *buttonImage = [UIImage imageNamed:#"1.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//sets the frame of the button to the size of the image
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
//creates a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
customBarItem.target = self;
customBarItem.action = #selector(click:);
self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil];
}
- (void)click:(id)sender {
NSLog(#"action");
}
I think my console will print "action" when I push down the barItem.
However "action" is not printed.
Did I miss anything?
Thanks for advance!
Try this
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"Click ME!" style:UIBarButtonItemStyleBordered target:self action:#selector(click)];
.
.
.
-(void)click{
NSLog("hello");
}
I believe the problem is that you are using a UIButton for the [[UIBarButtonItem alloc] initWithCustomView when you should simply pass a UIView, the action is probably going to the UIButton and not the UIBarButtonItem.
Instead do this:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1.png"];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:myImageView];
I think you're trying to do something more like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[button addTarget:self action:#selector(click) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

UIBarbuttonItem disappears

I have the following function which displays the top bar on my app. It consists of an image and a UIBarButton. I call it from viewWillAppear. When I push the current controller and move to the next controller and then move Back to the main controller, the right bar button disappears. Please help.
-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
[rightButton release];
}
[titleImage release];
}
-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
// [rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
//[rightButton release];
}
// [titleImage release];
}

two buttons on UINavigator

I would like to add 2 buttons to the UINavigator controller
I tried to create a button first and then add it to a UIToolbar but just can't get it to work.
Here's an image of what I want :)
link
Any help would be appreciated
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addButton.frame = CGRectMake(40, 40, 44, 44);
[addButton setTitle:#"YES" forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)];
NSArray* buttons = [NSArray arrayWithObjects:self.editButtonItem, addButton, nil];
[toolbar setItems:buttons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
}
- (IBAction)buttonClicked:(id)sender
{
NSLog(#"Hi!");
}
You can just do:
self.navigationItem.rightBarButtonItem = toolbar;