How to change label of tab bar item dynamically (IPhone dev) - iphone

I'm using XCode 4.1 and I have an already existing IPhone app.
In the "MainWindow.xib", in the Interface builder, there is a Navigation Controller. Inside it, a Tab Bar Item with a title on it. I need to change this title dynamically. However, there seems to be no ".h" file associated with the MainWindow.xib, so I have no idea how to access this object. Any idea?

You don't need a .h file to access items in MainWindow.xib. You can declare IBOutlets in your app delegate for the items you want to access. One of the objects in MainWindow.xib is the app delegate, so you can assign elementos to its IBOutlets.

The main window or the tab bar bontroller was, somehow, related to another controller (associated with a .h and .m file).
So in this controller, I added this code :
UIViewController *uvButton1 = [self.tabBarController.viewControllers objectAtIndex:0];
uvButton1.tabBarItem.title = NSLocalizedString(#"KEY", nil);
and so on for the 2 others (changing the index "0" for "1" and "2");
the NSLocalizedString function goes to a localized file (Localizable.strings) and chooses the right text based on the key. You can also just use a standard string there.

Related

Tab Bar Items selected in the More... tab get the title "Item"

I have an app delegate, which is also UINavigationControllerDelegate (and I've tried setting it as UITabBarControllerDelegate and UITabBarDelegate before asking, if that would trigger something to work, but...).
In mainwindow.xib, I have a tab bar with 6 items, which become split up into 4 + a more tab, which goes to the standard view with the two remaining tabs (in a list).
This tab bar has a controller which is the root controller over the nav controller, and called rootCt.
Now, the problem is that selected the tabs in the More nav ctlr makes the selected viewcontroller's title nil, showing "Item" instead of the tab title.
I would like to get the title as set in IB, as I've made localized nibs. Ie., simply the title that you click on in the More view.
I've tried:
UITabBarItem *item=[[appd rootCt].tabBar.items objectAtIndex:4];
NSString *str=[item title];
self.title=str;
But this gives nil. Changing index to 3 gives the 3rd title correctly, and 5 results in a crash.
Tried:
NSString *str=[appd rootCt].selectedViewController.title;
This also gives nil, because it's not set yet - that's what I need to set in this code.
I can't use selectedViewController.title, as that's nil and that's what I'm trying to set. Right?
How do I get the selected tab title coming from the More view? Should it be this hard?? Or have I missed something obvious...
First of all, where are you attempting to run the code you've pasted above? We probably need more detail to help you out here.
The correct place to set the title (if you wanted to overwrite what's set in the xib, which it doesn't sound like you want to do!) is in the UIViewController that's being displayed, in its viewDidLoad or viewDidAppear methods. If you use viewWillAppear, it may not be set yet.
But it really sounds to me like your xib isn't set up correctly.
Another question for you is what template did you start with in Xcode? It's significantly harder to set all this stuff up "by hand" than it is to use the standard template.
It must have been a lost outlet reference or similar. Deleting the .xib and redoing the work solved it.

File's Owner is not showing Tab bar controller Outlet?

I have included Tab Bar Controller in my application.
I have declared Outlet in the App Delegates' .h and .m file.
When I am right clicking on File's Owner, It doesn't show me Outlet.
OR
If I double click on the Tab Bar Controller I could not see the Outlet.
How to connect it...
I have Included the XIB file in an empty app.
I am using Xcode 4.
Any body can tell me how to get APPLICATION DELEGATE icon when clicking on xib file...??
Click on the app delegate icon and not on the file's owner.
Did you set the class for that xib or not. you can check that in the custom class group like in this pic. If not than you should set it with a class name of your choice.
Arpit, Instead of declaring Outlet in ProjectNameAppDelegate.h file that is being shown in side list... declare in .h file with the same name as .xib file... Same problem has been faced my me also... Hope this will help you...
You need to get the the type "'Object'" and drag it to the Below the File's Owner Window.
Set it's Class to your present appln delegate..this is the way you can create app delegate if not present by default near file's owner.

How to change the default View Controller that is loaded when app launches?

I have an application, say 'MyApp', which by default loads the view controller 'MyAppViewController' whenever the application launches. Later, I added a new view controller 'NewViewControler' to the project.
I now want the 'NewViewController' to be my default view controller which loads when the app launches.
Please let me know what changes I need to make in my project to achieve this.
Its easy, just:
Open your Storyboard
Click on the View Controller corresponding to the view that you want to be the initial view
Open the Attributes Inspector
Select the "Is Initial View Controller" check box in the View Controller section
Open MainWindow.xib and replace MyAppViewController with NewViewController.
In your app delegate class, replace the property for MyAppViewController with one for NewViewController. Connect NewViewController to its new outlet in Interface Builder.
In application:didFinishLaunchingWithOptions: add NewViewController's view to the window instead of MyAppViewController's view.
Most likely your main NIB file is still set to "MainWindow", check your *-Info.plist file.
If that's the case you can open the MainWindow.xib in Interface Builder. You should see a View Controller item. Bring up the inspector window and change the Class Identity to point to your new class. That should take care of instantiating your class.
As this feels like a "newbie" question (please pardon me if I'm mistaken) I would also highly recommend the following article:
iPhone Programming Fundamentals: Understanding View Controllers
Helped me understand the whole ViewController thing and the IB interaction..
As for me with xcode 4.3.3, all I had to do was simply replace all references of 'MyAppViewController' with 'NewViewController' in the AppDelegate h and m files.
Perhaps all the other steps have been taken out in the newer versions of xcode.
Hope this helps.

Creating a nib file for a Table View Controller

I was trying to follow the Table View Programming Guide for iPhone OS but was having trouble creating a new Table View Controller that loads its data from a nib file.
Specifically, I was trying to follow the steps in this part:
If you prefer to load the table view
managed by a custom table-view
controller from a nib file, you must
do the following:
In Interface Builder, create an empty Cocoa Touch nib file (File >
New).
Drag a UITableViewController object from the Interface Builder
Library into the nib document window.
Save the nib file in your project directory under an appropriate name
and, when prompted, select your
project to have the nib file added to
it.
Select Table View Controller in the nib document window and open the
Identity pane of the inspector. Set
the class to your custom table-view
controller class.
Select File’s Owner in the nib document window and set its class
identity to the custom table-view
controller class.
Customize the table view in Interface Builder.
Select the table-view controller in the nib document window, open the
Attributes pane of the inspector, and
enter (or select) the name of the nib
file in the Nib Name field.
So I created a new UITableViewController subclass in Xcode (called "MyTableViewController"), then I went into IB and followed those steps. I made sure to link up all the Class attributes to the same name as the UITableViewController subclass I made in Xcode like it says in the steps.
But now I get the following warning in IB:
"My Table View Controller" has both its
"View" and "Nib Name" properties set.
This configuration is not supported.
When I run the application and push the table view controller, it appears but it seems like nothing is being loaded from the nib file at all (e.g. I set the alpha to 0 instead of 1).
Any idea as to what I'm doing wrong?
Thanks for the help.
Here's some more information that might help you understand the situation better.
I noticed a few differences between creating a UITableViewController with the template (e.g. by creating a new Navigation-based Application) vs. creating one yourself (e.g. following the steps above). I'm going to refer to each as TemplateNib and CustomNib, respectively, to make it easier to understand the differences.
In TemplateNib, it has the following objects in the document window:
File's Owner
First Responder
Table View
In CustomNib, it has the following objects in the document window:
File's Owner
First Responder
My Custom Table View Controller
Table View
Another difference is in the File's Owner links...
TemplateNib's File's Owner:
Outlets
tableView -> Table View
view -> Table View
Referencing Outlets
dataSource -> Table View
delegate -> Table View
CustomNib File's Owner:
Outlets
view -> (nothing)
CustomNib My Table View Controller:
Outlets
view -> Table View (this is grayed out so you can't delete it)
Referencing Outlets
dataSource -> Table View
delegate -> Table View
Update:
I tried to mimic the .xib file that is created by the template by following these steps:
Created an empty file in Interface Builder.
Set the File's Owner to the class that inherits from UITableViewController.
Added a Table View to the document window.
Set the Table View's dataSource and delegate to File's Owner.
Set the File's Owner view to the Table View.
Added a tableView propery in the Identity pane of type UITableView.
Set the File's Owner tableView property (which I just created) to the Table View.
However, this still seems like it is not loading it from the NIB file. (I also never set the name of the NIB file anywhere though... is there anyplace I need to set it or does it look for one with the same name?).
I then tried overriding initWithNibName to load from the name of the nib file, and now it does seem to load it from the nib file. However, if I look at the .m file of the TemplateNib table view controller, it doesn't need to override this method, why is that? I still think I am doing it the wrong way cause the Programming Guide didn't mention anything about doing it this way.
Update:
I tried comparing the two .xib files using a diff tool, the only significant difference between the two seems to be:
<string key="superclassName">UITableViewController</string>
// and:
<reference key="NSSuperview"/>
I don't see any reference to the Nib file in the original file anywhere, are there any other files I should check?
Update:
It seems like the thing that makes TemplateNib load from the nib file is that in the MainWindow.xib (default name given by the template), the RootViewController is added with the NIB Name property having the value "RootViewController". Additionally, its class is set to "RootViewController".
I tried putting a breakpoint in both initWithNibName:bundle: and initWithStyle: on the RootViewController, however, it never gets to there. I'm kinda wondering how the TableViewController is created when you set it up in the MainWindow.xib like that.
I also tried adding the my custom table view controller to MainWindow.xib, setting the class and nib names in hopes that it will load it from the nib file I specified, but it doesn't even call iniWithNibName.
Create a TableViewController in Xcode.
Create an empty nib file in Interface Builder.
Set the File's Owner Class property to the TableViewController from step 1.
Add a TableView to the empty nib file.
Set the File's Owner view property to the TableView from step 4.
Customize the TableView in IB as you want.
Override the initWithNibName:bundle: method in Xcode for the TableViewController you created and use code similar to the following:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:#"MyNibName" bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
I had this same problem last night and found this post while trying to search for the answer. I ended up solving it.
Basically I had opened up the wrong XIB file (I hoped main_window.xib, not the view controllers xib)
I cut all the controls from my main xib, pasted them into the controllers xib, realigned everything, reconnected all the outlets/actions and the warning went away :)
Hope this helps someone :)
Instead of doing all that, I would use the "New File" iPhone UI template to create a TableViewController with xib file option checked. Then you get a controller and xib file all wired together properly.
Eagle, when you create a new file, select the "UIViewController subclass" icon. There's a checkbox to make it a UITableViewController subclass just above the checkbox to include XIB file.
You've got two places where your UITableViewController shows up in Interface Builder.
(1) It shows up in the nib with the controllers own name.
(2) It shows up as a controller object in the nib of another object, usually the MainWindow.
Your problem is at (2). There are two ways to set the tableview for a UITableViewController in Interface builder. First, you can create a UITableView under the controller in the MainWindow and connect that to the controller's view property. Secondly, you can bring up the inspector in the attributes pane and in the popmenu listed "NibName" select the name of the controllers nib.
You can't use both systems at once because the first loads a view from the MainWindow nib file and second loads a completely unrelated view from the controller's separate nib file.
This is one of those maddening errors that using Interface Builder makes so hard to track down.

How to use UISearchDisplayController from a controller within an UITabBar controller?

I have an UITabBar controller managing several controllers (using SDK 3.0). One of these is a tableView controller and I need to provide a search capability using the UISearchDisplayController. All of my code is based on Apple TableSearch example. However, when clicking on the tab, the tableView controller appears showing its related content, but no searchBar appears. I have checked the xib in IB to make sure that all of the outlets are properly set, but no matter what I try self.searchDisplayController is always nil and the search bar does not appear.
In practice I have replicated MainView.xib from the TableSearch example and set the file's owner class to the correct controller class for the tab. The outlets are sets as in the example MainView.xib. Am i missing any important step or doing something wrong?
Thank you in advance.
I had the same issue and happened to stumble upon this solution ...
If you have your table view controller (eg. UISearchDisplayController) nested in a Tab Bar or Navigation controller using Interface Builder, you need to set the "Nib Name" in the "Attributes Inspector" window.
The Nib name will be the one that holds the table view and has the controller (eg. UISearchDisplayController) as the File's Owner.
I too have this issue :( Is the search bar getting hidden behind the tableview?
#unforgiven,
did you try this...?
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
[self.tableView setTableHeaderView: searchBar];
This manually creates a searchbar and it works. But I'm making some stupid mistake in IB that the SearchBar doesn't show up even though my connections are perfect. :-(
Do update this post if you get the answer...
Ok, I have found how to solve it. In my case, the problem was due to the fact that I was using a controller embedded within the UITabBarController as one of its managed tabs (i.e. as a child).
Removing the controller from the UITabBarController, then adding an UINavigationController to the UITabBarController instead, and finally putting my controller as a child of the UINavigationController solved completely the issue.
I do not understand why this is so (there is no related information in the documentation, as often happens); however, it now works like a charm.
With kind regards.
I had a similar issue
To solve I had to do one additional step to unforgivens answer
In my main nib
1) create a UITabController
2) Then I dragged out a UINavigational Controller into the tab controller
3) Then dragged out a UITableViewController into the NavigationalController as a child
4) Then changed (3) class to be my MyTableWithSearchBarViewController in the inspector - check if the nib name is correct and change this if necessary in the inspector as well
5) I then had to delete the tableView which is automatically created by IB in step (3). Only then would the search bar show correctly...
If in step 3 I dragged out a different controller onto the stage or left the tableView there it would only ever display the table and not the search bar
weird
Tomtrapeze has the right answer if your nib file contains the UITableViewController. But, if you're loading the UITableViewController in code -- such as pushing it on the stack of a UINavigationController -- the solution is a little different.
When initializing the UITableViewController or subclass, you need to use the -initWithNibName:bundle: form of initializer and specify the name of your nib file. Alternately, you could use the standard -initWithStyle: initializer and manually set the nibName property before the view is loaded.
If the nibName property is not set when the view gets loaded, UITableViewController will not use the normal UIViewController nib lookup logic. It will just load a standard UITableView instead.
I recently learned that I could load NIB files with [[MyViewController alloc] initWithNibName:nil bundle:nil]; when the NIB file has the same name as the class (i.e. MyViewController.xib). It turns out that initializing this way causes the 'UISearchBarandUISearchBarDisplayController` to not display. When I initialized the view controller by actually typing out the name of the class, my search bar showed up correctly. I initially thought it had something to do with how I was presenting the view controller but was glad that it wasn't.
PGWSearchViewController *searchVC = [[PGWSearchViewController alloc] initWithNibName:#"PGWSearchViewController" bundle:nil];
searchVC.modalPresentationStyle = UIModalPresentationFullScreen;
searchVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:searchVC animated:YES completion:^{ }];
This is using iOS SDK 5.1 and XCode 4.3.1.
Another possible fix that works for me is to init the UITableViewController with initWithNibName:bundle:
SearchEntryTableViewController* searchEntryTableViewController = [[SearchEntryTableViewController alloc]initWithNibName:#"SearchEntryTableViewController" bundle:nil];
To nest the UITableViewController in a UINavigationController before put it in a UITabBarController doesn't fix the issue for me...