Problem tabbing to field when adding and removing view - objective-c

I have an scope bar containing a NSSearchField. The bar can be shown and hidden using a menu item. I generate this bar by creating a new NSViewController (and loading a new view from the XIB). When the bar is shown, I do a addSubview: to the window's contentView; when the bar is hidden, I do removeFromSuperview to the view within the view controller.
If when I launch the app and the bar is already opened, hitting tab toggles between the main view within the window (a table view) and the search field in the scope bar. If I launch the app and the bar isn't already shown, once I do show the bar I can tab from the table view to the search field, but not the other way.
Once I remove the scope bar for the first time, then show it again, I can no longer tab between the search field and the table view, no matter which view is currently selected.
Is there something I need to be doing besides addSubview: and removeFromSuperview? I can't wrap my head around why this won't work, and especially why I get different behaviors if the bar is shown on launch or not.

You need to set the nextKeyView of both views if you want to control what happens when you hit the tab key.
[yourTableView setNextKeyView:yourSearchField];
[yourSearchField setNextKeyView:yourTableView];
However, you need to be careful because you can break the automatically constructed key-view loop. This article has more detail on how to handle this situation.

I was able to get the desired behavior by setting setAutorecalculatesKeyViewLoop: to true on the views' window.

Related

View Controllers, Popup buttons, AppDelegates, Status Menus, and headaches

This is kinda long but I want to give as many details as possible.
I have created a status bar app. One of the things I've done is made it to where there is a "Settings" menu item that if you click on, an NSWindow pops up with 5 different buttons -- these buttons are tied to their own custom NSViewController so that if you click them, the View Controller changes, but the window remains the same, just like any other StatusBar app.
On one of the view controllers is a popup button with several user selectable options. If I add a button to the specific VC (view controller) and link that to an ibaction that pulls the selected index of the popup button, it works just fine (i.e. I select item 2, it logs item 2).
Now here's where things get. . . odd. I've created a submenu that would in theory allow the user to quickly change the selected item of the popup linked to my custom view controller. Think of this submenu as being labeled 1-4 and clicking one of those items would change the index of the popup button, however every time I try this and follow it up with a NSLog to ensure the button has changed, it ALWAYS reads zero, in other words, it NEVER changes.
Here's what I tried.
Just like with every other view controller I've ever used, I create a new object of my view controller (alloc, init), and reference the popup button object that i've created as a property. This compiles just fine, but returns only zero no matter what is selected.
I've tried calling the method that I know works (the button that is on the view controller containing the popup), but again, this returns only zero no matter what is selected.
Any advice would be greatly appreciated.
tl;dr: can't change a popupbutton on a view controller from the app delegate or nsstatusmenu item, it just always returns zero.
EDIT: I have found a work around since I can communicate to my app delegate just fine, I created an int property and set it to zero. From there, whenever my popupbutton on my VC changes, I change the value of the app delegate int property and just ensure that whenever the nib launches, it set's the selected item to the app delegate property. I feel there has to be a more straight forward way to just get the value of the popup and change it from the app delegate.
EDIT 2: Some code as requested. I realized I needed to point the the view controller by using initwithnibname from the app delegate and then load the view. Doing so allows me to pull the first object in the popup button, any subsequent changes are not recognized unless I do so via my test button on the view controller itself. Also, I'm adding values to my popup in my awakefromnib in the view controller.
searches_VC = [[Searches_ViewController alloc] initWithNibName:#"Searches_ViewController" bundle:nil];
[searches_VC loadView];
NSLog(#"Selected Title: %#",[[searches_VC profilePopupOutlet] titleOfSelectedItem]);

How I can keep the value of a barButton among the views handled by a tab bar?

I have two weeks programing in objective-c. Basically, I have a three views handled by a tab bar. Each view has rounded rect buttons that calls another view with different information. Also, at the top of each view, there is a bar button item. The title of all those bar button items depends of the value of a switch: manual or automatic. If it is automatic, the button's title must be "Trigger". If it is manueal, the button's title must be "Star" and, if you make a click on it must change to "Stop". My problem is, the button's title is changing but if I'm in the fist view and I press "start", it change to "stop", but in the second view it's still "start".
So, could you help me with this, please?
Sorry, I don't understand your problem (it would be easier if you would provide code or pictures). Thus I try to interpret the title of your question:
You have a tab bar controller that controls a few view controllers that have each its own view. These views are independent of each other. If each view has e.g. its own barBottom item but they should all have the same title when you switch from one tab view to another, their titles must be changed by program. Since only the tab bar controller knows when it changes its views, it has to assign the new title to all the barBottom items of its views.
It can do this using its delegate that corresponds to the UITabBarControllerDelegate protocol. This delegate (and the tab bar controller may be its own delegate) can e.g. implement the method tabBarController:didSelectViewController: which is called when one switches from one view to another. In this method, the tab bar controller can set the new title by calling a method in each of its view controllers (which can be accessed via the property viewControllers).

disable rearrange the tabs iphone

I have a six UITabBarItem in a UITabBarController.
I want to disable the NavigationBar that auto-generates the tab number 5, which allows you to reorder the tabs as you want.
Is there a way to disable this function?
EXTRA
I use customtabbar but in the tab5 appears this navigationbar.
the tab6 for me must be "hidden"
there is the property customizableViewControllers
from the docs:
"Changing the value of the viewControllers property (either directly or using the setViewControllers:animated: method) also changes the value of this property. When first assigned to the tab bar controller, all view controllers are customizable by default."
so call myTabBarController.customizableViewControllers = nil to remove customization
It looks like you are using a UITabBarController.
You can only have up to 5 tab bar items without bringing up the additional item to customize the bar items. If you don't want the extra tab to allow for customization, your 6th item will be unreachable anyway, so just remove the last tab to make it 5 items.
From the UITabBarController Documentation:
The tab bar has limited space for displaying your custom items. If you
add six or more custom view controllers to a tab bar controller, the
tab bar controller displays only the first four items plus the
standard More item on the tab bar. Tapping the More item brings up a
standard interface for selecting the remaining items.
If you do not like this limitation, use a UITabBar directly. There is more work involved because you won't be using a UITabBarController which manages the view controllers of your tabs for you.

Objective C: How to reload tableview immediately after switching tabs?

I have 2 tabbars in my tabbar controller. I am currently in the second tabbar, after I click on a 'done' button, the tabbar controller needs to switch to the first tab and auto refresh the tableview in it.
I am able to execute on the first part
//Switch to the first tab's view
self.tabBarController.selectedViewController
= [self.tabBarController.viewControllers objectAtIndex:0];
However I need some advise on how to refresh the first view's tableview immediately after the switch is made. Any advise is appreciated.
EDIT: Updated requirement
Note: I do not want the view in the tabbar to reload everytime someone clicks on the tab. The reload should only happen in tab 1's tableview when
1) User is in tab 2
2) User clicks on 'Done button' in tab 2
3) App switches to tab 1 + Reloads the tableview in tab 1
Rationale is that the user will update some information in tab 2 which can only be viewed in tab 1. I want the user to be able to view the updated info immediately after switching to tab 1.
You could just do [tableView reloadData] in the first view's viewDidAppear: method?
EDIT: Editing to add additional data.
General caveat here, Apple tends to frown upon having custom UI Patterns like this. I'm not sure what your app is trying to do here, but you should mostly stick with switching tabs only if the user explicitly clicks on that tab. Have you looked at modal view controllers if you want to present a screen and get some data back, instead of having it in a tab bar?
If not, I guess you could have a solution where the first tab is a delegate of the second tab. When the button is clicked, you could call [delegate doneBtnClicked:] so the control shifts to the first tab. In that function, you could use the setSelectedIndex: to your current tab and call reloadData: on the table view.
You can send NSNotification after
pressing done button on second tab
and handle this notification on first tab controller.
So, create a DONE Button as a UIBarButtonItem, and in it's IBAction, do the following.
For reloading the tableView for the view in the first tab, put that tableView as that class' property , and reference the class like #class class1 , and create an object as a property class1 *object1 for View1 (Tab1) in the class for View2 (Tab2), and access the array1 using object1 like
[class1.object1.array1 reloadData];
And for bringing the view to tab one, use
self.tabBarController.selectedIndex = 0;
Thats pretty much it !
Alternatively you can also use a singleton class for this purpose.

Adding and Removing UIBarButtonItems to UINavigationBar from Multiple Views

I currently have a Main Window set up as a UINavigationController (also is the root view controller), and I have two views. The first view is the login screen, and the second screen is a table view screen. What I would like to have happen is to show different UIBarButtonItems based on what screen is showing up. For instance, when the logon screen is being displayed, I want a left button on the navigation bar to be displayed (more specifically, it'd be a Settings button before logging in). Once the user logs in, I want the left button to say "Logout" and the right to be a reload button. I've tried programmatically adding the buttons, but they won't show up. Any suggestions?
Also, I've gotten most of this done in IB, but I feel like it would probably be easier to add these buttons programmatically.
All subclasses of UIViewController have a property called navigationItem. While it is readonly, you can alter its properties. So in LoginViewController, you would do self.navigationItem.leftBarButtonItem = /* Code to create the bar button */ and likewise within the TableViewController. Let me know if you need more detail.