uibarbuttonitem not working - ios

- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:#"Refresh" style:UIBarButtonItemStylePlain target:self action:#selector(refreshButtonClicked:)];
[self.navigationItem setRightBarButtonItem:refreshButton animated:YES];
}
- (void)refreshButtonClicked:(id)sender {
NSLog(#"Refreshed.");
}
I added a navigation controller programmatically in AppDelegate and set this view as the root.
When I was trying to add a navigationItem programmatically as shown above, the refreshButton showed in the navigation bar but refreshButtonClicked: was not invoked.
What's the problem? Any suggestions and comments are welcome.

Make a call to the super class in your viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:#"Refresh" style:UIBarButtonItemStylePlain target:self action:#selector(refreshButtonClicked:)];
[self.navigationItem setRightBarButtonItem:refreshButton animated:YES];
}
It will ensure that the root class setup is done

Problem solved. Weird. After I clean the builds, delete the derived data and restart Xcode, it goes runs properly.

Related

Unable to set back button title ios

I am new to iOS,
I want to set empty text to all back button on UINavigationBar globally because i have multiple UINavigationBar so thought to try using category.
Here is my code:
UINavigationItem+BackButton.h
#interface UINavigationItem (BackButton)
- (void)removeBackText;
#end
UINavigationItem+BackButton.m
#implementation UINavigationItem (BackButton)
- (void)removeBackText
{
self.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Bc" style:UIBarButtonItemStylePlain target:nil action:nil];
}
#end
In one of my ViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationItem removeBackText];
}
I have tried this but its not working.
Can any help help where i am doing mistake?
Thank you in advance
Change in your removeBackText method
- (void)removeBackText
{
self.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
}
Here initWithTitle:#"" not initWithTitle:#"Bc"
And also you need to change your viewWillAppear method to add back barButton on navigation bar like below.
self.navigationItem.backBarButtonItem = myBackButton;

ios share extension post button title

I want to change the title of post buttons in SLComposeServiceViewController.
I managed to get the UIButton:
NSArray* subviews =[self.navigationController.navigationBar subviews];
UIButton* postButton =[subviews lastObject];
and i tried to set title like this:
[postButton setTitle:#"Save" forState:UIControlStateNormal];
but the title not changed.
Can anyone help me with this?
I saw Evernote's share extension on my iPad and it looks like this:
UPDATE
My Solution:
I found solution for my question,I removed the original navigation bar and create custom nav bar.
I have two nav bar:
1. with "cancel"\"save" buttons
2. with "back" button
and I change them when navigate to other viewcontroller
(in my case I needed to upload file and user need select location from list)
NOTE: if you not implement configurationItems you need only the first nav bar. (just call to set custom nav bar from viewDidAppear
So my code is here:
#property (strong, nonatomic) UINavigationBar *customNavBar;
-(void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.customNavBar = [[UINavigationBar alloc] initWithFrame:self.navigationController.navigationBar.bounds];
[self.navigationController.navigationBar removeFromSuperview];
[self.navigationController.view addSubview:self.customNavBar];
[self setCancelSaveNavigationItem];
}
setCancelSaveNavigationItem--> called from viewDidAppear of shareViewController
-(void)setCancelSaveNavigationItem
{
UINavigationItem *newItem = [[UINavigationItem alloc] init];
UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Cancel",nil) style:UIBarButtonItemStylePlain target:self action:#selector(cancelButtonTapped:)];
UIBarButtonItem *saveBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Done",nil) style:UIBarButtonItemStyleDone target:self action:#selector(saveButtonTapped:)];
newItem.leftBarButtonItem = cancelBarButtonItem;
newItem.rightBarButtonItem = saveBarButtonItem;
[self.customNavBar setItems:#[newItem]];
[self.navigationItem setBackBarButtonItem:cancelBarButtonItem];
[self.navigationItem setRightBarButtonItem:saveBarButtonItem];
if(self.item.value == nil){
saveBarButtonItem.enabled = NO;
}
}
setBackNavigationItem--> called in configurationItems -->in self.item.tapHandler function
-(void)setBackNavigationItem
{
UINavigationItem *newItem = [[UINavigationItem alloc] init];
UIBarButtonItem *selectBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Select",nil) style:UIBarButtonItemStylePlain target:self action:#selector(selectButtonTapped:)];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:#"❮ %#", NSLocalizedString(#"Back",nil)] style:UIBarButtonItemStylePlain target:self action:#selector(backButtonTapped:)];
newItem.leftBarButtonItem = backBarButtonItem;
newItem.rightBarButtonItem = selectBarButtonItem;
[self.customNavBar setItems:#[newItem]];
[self.navigationItem setBackBarButtonItem:backBarButtonItem];
[self.navigationItem setRightBarButtonItem:selectBarButtonItem];
}
Handle buttons tapped:
- (void)backButtonTapped:(id)sender {
if([self.navigationController.viewControllers count] ==2){
[self setCancelSaveNavigationItem];
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)cancelButtonTapped:(id)sender {
[self cancel];
}
- (void)selectButtonTapped:(id)sender {
...
[self setCancelSaveNavigationItem];
[self popConfigurationViewController];
}
- (void)saveButtonTapped:(id)sender {
...
[self cancel];
}
And it's work for me!!!
The result:
Simply in viewDidAppear
self.navigationController?.navigationBar.topItem?.rightBarButtonItem?.title = "Save"
Your code:
NSArray* subviews =[self.navigationController.navigationBar subviews];
UIButton* postButton =[subviews lastObject];
...is a really bad idea. It only works because the post button is in subviews and is the last item in the array. But the content of subviews is undocumented and might change at any time. Also, since there's no public API for this button, it's entirely possible that there's framework code to prevent or override changes to the button text-- so even if we assume you have the right UI element, you still might not be able to change it.
Evernote's UI is almost certainly a full custom design that only resembles SLComposeServiceViewController. Share extensions are not required to use SLComposeServiceViewController, that's just there for convenience. If it doesn't meet your needs, design your own.
Update: out of curiosity I unzipped the Evernote IPA and had a look at EvernoteShare.appex with nm. There's no reference to SLComposeServiceViewController, which confirms that Evernote is not using that class in their extension.

My Custom UINavigationController won't show right button

I have a "LoginViewController" which presents a new Controller which is a subclass of UINavigationcontroller when clicking a button:
MPNavigationViewController *controller = [[MPNavigationViewController alloc] initWithRootViewController:[[MPQuestionFirstViewController alloc] init]];
[self presentViewController: controller animated:YES completion:nil];
"MPNavigationViewController" subclass UINavigationController and uses "REMenu" to have a sliding-from-top menu ("Link") and on viewDidLoad I try to add a right button to open it:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *toggleMenuButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(toggleMenu:)];
self.navigationItem.rightBarButtonItem = toggleMenuButton;
[self initMenu];
}
It doesn't show any button on the navigation bar. Why could it be?
If I try to add the button from one of the "viewControllers" that will handle sections on the menu. It shows the button, but it doesn't paint it at all.
Thanks.
You are using subclass of UINavigationcontroller which is not actually view controller.
There is only one solution, You need to create your custom button and add it to UINavigationbar as a subview..
Use this hope it will help.
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(goToDoneButtonAction)];
self.navigationItem.rightBarButtonItem = doneButton;

Thread 1: Signal SIGBART

So I have two views that I am trying to connect with a navigation.
I have embedded them in a navigation controller and created a push segue between them in the storyboard:
It the viewDidLoad of the first controller I add a button to the navigation together with a method it should call when clicked:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(buttonClicked:)];
self.navigationItem.rightBarButtonItem = myButton;
}
- (void)buttonClicked
{
NSLog(#"hello");
}
It compiles fine and I can run it in the simulator but when I click on the button in the navigation bar, instead of logging "hello", I get:
Any ideas how to solve it? I have run out of ideas. I am using the latest XCode.
Try to remove the : in your target action:
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(buttonClicked)];
if you want to use the colon define your method like this:
- (void)buttonClicked:(id)sender
{
NSLog(#"hello");
}

Add a UISegmentedControl as the titleView in a UINavigationController

I'm trying to add a UISegmentedControl programmatically as the titleView in a UINavigationController. But it did not show up. Upon further investigation, I found out that the titleView property is ignored if the leftBarButtonItem is not set to nil according to the Apple docs. So I set it to nil but still the segmented control does not show up!
Below is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Personnal", #"Department", #"Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = statFilter;
}
What should I do? What am I missing here?
Thanks.
EDIT :
Thanks for all the responses. However, none of them worked for me so I took a deeper look. It must be the way I have laid out my application. I probably should have mentioned that earlier. Apologies.
What I'm creating is a Storyboard app. The main controller is a UITabBarController. When a certain tab is clicked, I want to show a UITableViewController. Since you cannot display a Navigation bar in a UITableViewController, I found out that I must add a UINavigationController and implement a UITableViewController inside it. This is how my app basically look like,
That Dashboard View Controller is connected to a class inheriting from UINavigationController
class. Maybe this is where the issue resides?
Because I just created a small test app, put a UIViewController, embedded in a UINavigationController and wrote the code I have posted in my original question above inside the UIViewController's ViewDidLoad and the segmented control shows up just fine. Now I don't know how to do the same in my main app because there's no other view controllers or anything connected to the navigation controller.
Sorry if I made things even worse. Please comment if anything is unclear.
I guess you have to make the UISegmentedControll a UIBarButtonItem:
UPDATE:
This Code makes the SegmentedControll centered:
- (void)viewDidLoad{
self.title = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Personnal", #"Department", #"Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
NSMutableArray *buttonArray = [[NSMutableArray alloc] init];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[buttonArray addObject:flexibleSpace];
[buttonArray addObject:[[UIBarButtonItem alloc] initWithCustomView:statFilter]];
[buttonArray addObject:flexibleSpace];
self.navigationItem.leftBarButtonItems = buttonArray;
[buttonArray release];
}
I hope this helps you out!
Use this i hope this will help you
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * navview = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 160,80)];
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Personnal", #"Department", #"Company", nil]];
[navview addSubview:statFilter];
self.navigationItem.titleView = navview;
}

Resources