Does anyone know if it is possible for a single tab bar item to have multiple relationships?
I would like to be able to direct to two different view controllers from a single UITabbar icon, depending on the type of user that logs in.
For example, if the user logs in as user type "A", I want the Tab bar icon to direct to a profile view controller. If the user logs in as user type "B", I want the same icon to direct to a settings view controller.
I've tried to connect the additional view controller to the tab bar and it just creates an additional icon/tab on the tab bar.
You will need to do it from code, so look at the setViewControllers method.
Supposing you have 4 Tabs corresponding to vc1 vc2 vc A or B and vc4...
You could determine which VC you want assigned, then instantiate the full "set" of controllers with:
// set "vcA" as the 3rd tab
[self.tabBarController setViewControllers:#[vc1, vc2, vcA, vc4] animated:NO];
// or, set "vcB" as the 3rd tab
[self.tabBarController setViewControllers:#[vc1, vc2, vcB, vc4] animated:NO];
Or... to save on "manually" instantiating the controllers...
You could assign all 5 controllers in your storyboard, then:
// get the array of viewControllers
NSMutableArray *a = self.tabBarController.viewControllers;
// a now contains [vc1, vc2, vcA, vcB, vc4]
// remove "vcA"
[a removeObjectAtIndex:2];
// or, remove "vcB"
[a removeObjectAtIndex:3];
// set the controllers array
[self.tabBarController setViewControllers:a animated:NO];
You could also place a container view in the the view controller for that tab, add two views to the container view, and then depending on what type of user it is show the proper view during viewDidLoad.
Will add code when I have time.
This would be one way of doing it:
A. keep track of what sort of user logs in held a variable, passed on from your previous viewController, or held centrally in a data object:
bool userCanAccessProfile = false;
B. depending on the above bool, update your layout and logic code accordingly:
//layout your tab bar
UITabBar * tabBar = [UITabBar new];
tabBar.frame = CGRectMake(0, h-50, w, 50);
tabBar.delegate = self;
[self.view addSubview:tabBar];
//create the item(s)
UITabBarItem * item = [UITabBarItem new];
item.title = (userCanAccessProfile) ? #"Profile" : #"Settings";
item.image = (userCanAccessProfile) ? [UIImage imageNamed:#"profile.png"] : [UIImage imageNamed:#"settings.png"];
[tabBar setItems:#[item]];
The lines above that look like this, means this:
something = (isThisTrue) ? (true) setThisValue : (false) setAnotherValue;
You're asking if userCanAccessProfile is true, and if it is, you're setting a different text and image accordingly.
C. When the User clicks on the item, you would again query the bool to find out what to do:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
//when the item is clicked
if (userCanAccessProfile){
//open profile
} else {
//open settings
}
}
Be sure to set the delegate in the .m file:
tabBar.delegate = self;
and add the delegate in the .h file:
#interface yourVC : UIViewController <UITabBarDelegate>
Related
I have a UINavigationController which points to a UITableViewController (a list of items) where there is a segue from a cell to another UITableViewController (a screen to edit an item).
On first run of the application, I'd like to skip the first list and immediately go to the second screen, to edit a new item.
The problem is I need to pass the first UITableViewController, as I need to be able to go back to that one (or is there a way to set the controller the back button is pointing to?).
Things I've tried and failed:
Set a boolean shouldPresentNewItem on the UINavigationController and in the viewDidLoad if it is set to true, present the first UITableViewController, also setting a boolean so I can go to the edit screen.
Using self.navigationController!.popToViewController(arr[index] as UIViewController, animated: true) in the UINavigationControllers viewDidLoad. This gave an error as self.navigationController was nil. (I don't get why this happens)
How can this be done?
In navigation controller set some boolean indicating that you're going to show edit screen and in viewDidLoad just push edit view controller without animation:
- (void) viewDidLoad {
[super viewDidLoad];
if (self.presentEditScreen) {
self.presentEditScreen = NO;
EditViewController *e = [[DetailViewController alloc] init];
[self pushViewController:e animated:NO];
}
}
simplest way will be. just push from second view to first view
firstViewController *objFirstViewController = [[firstViewController alloc]initWithNibName:#"firstViewController" bundle:nil];
[self.navigationController pushViewController:objFirstViewController animated:No];
Hi in VC1 I have an NSMutableArray displaying results. I want to keep that array alive even when users clicks to a different tab (so they don't have to search again) until the user searches again.
I have a strong pointer to it, but it seems to unload when I leave the view.
Not much code to show (_resultsArray is set from a previous controller using delegates, so it loads with the results already)
- (void)viewDidLoad
{
[super viewDidLoad];
_resultsTableView.dataSource=self;
_resultsTableView.delegate=self;
[self.navigationController.navigationBar setHidden:YES];
}
//then standard tableview delegate methods...
This code is to try to figure out how to segue tab bar to share info.
(in prepareforsegue)
Currently in Search VC. Now I have results I want to give to resultsIndexVC. The code below attempts this.
This is placed in current (search VC) prepare for segue.
ResultsIndexViewController* vc = [[ResultsIndexViewController alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (ResultsIndexViewController *)[[tbc customizableViewControllers] objectAtIndex:1];
vc.SearchDelegate=self;//crash here (normally works if using regular segue)
vc.resultsArray = _temporaryResultsArray;
vc.originalQuery=_queryArray;
Thanks
Issue is that I was pushing a VC. Instead I used the tabbar (which doesnt release the object when switching tabs)
- (void)createBarButtons
{
UIBarButtonItem *myCheckButton = [[UIBarButtonItem alloc] initWithTitle:#"Check Records" style:UIBarButtonItemStylePlain target:self action:#selector(checkRecordsAction)];
UIBarButtonItem *mySaveButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveAction)];
[mySaveButton setTintColor:[UIColor colorWithRed:34.0/255.0 green:97.0/255.0 blue:221.0/255.0 alpha:1]];
NSArray *myButtonArray = [[NSArray alloc]initWithObjects:mySaveButton, myCheckButton,nil];
self.navigationItem.rightBarButtonItems = myButtonArray;
}
I dont know if this question falls under too localized category. But help me out here.So like you see i have created two bar button items. Save is just saving the data onto CoreData,works just fine. But the check records should launch a new UITableviewcontroller.
- (void)checkRecordsAction
{
NSLog(#"the new stack action");
ITMSyncRecordsTVC *syncRecords = [[ITMSyncRecordsTVC alloc]init];
// [self presentViewController:syncRecords animated:YES completion:^{
// self.navigationController.view.superview.bounds = CGRectMake(0, 0, 250, 250);}];
[self.navigationController pushViewController:syncRecords animated:YES];
}
ITMSyncRecordsTVC is a TableViewController with a "Back" button on it.So when i click the check records it launches a tableview controller but no values in it and it does not show the "Back" bar button i put on it. Until now i have been using segues and storyboards just fine. But how do i launch a new view controller without them i dont know. My first leap into ios is ios6. I am missing something I dont know. So let me know how to call/launch a new TableViewController. In android we had intents that did the trick. Please let me know if you need more information. Thanks...
EDIT: So i edited my checkRecordsAction code.
EDIT :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ITMOrdersVC *ordersVC = [[ITMOrdersVC alloc]init];
NSLog(#"at line 188 %d",indexPath.row);
if(indexPath.row < self.salesOrdersArray.count)
{
ordersVC.salesOrder = [self.salesOrdersArray objectAtIndex:indexPath.row];
NSLog(#"the sales purchase order number is %#",ordersVC.salesOrder.purchaseOrderNumber);
NSLog(#"done - 140");
[self.navigationController pushViewController:ordersVC animated:YES];
}
}
So on selecting a row on ITMSyncRecordsTVC table view controller it does the above. I get a new ITMOrdersVC screen with "Back" bar button at the left and 2 dynamically generated bar buttons.I get the 2 dynamically generated bar buttons but not the back. I thought once i click the row it will "go back" to previous screen to which i am passing the salesOrder object. My next step was to check if i get the salesorder object from the TVC then load it. So basically 2 screens only. First screen (save,check sync records). Second screen click a.) back(go to first screen..do nothing) or b.)click a row in second screen and populate first screen without the bar button.If it is not clear please ask me.
To launch a new table view controller using storyboards, you want to:
Have your main scene embedded in a navigation controller.
You want to have a push segue from the main controller to the second one. So control-drag from the view controller icon (in the bar below the main scene) to the next scene:
It should then look like (note the appearance of the navigation bar in the destination table view):
Then select the segue and give it a "storyboard identifier":
And now, your code to transition to that scene would look like:
The checkRecordsAction:
- (void)checkRecordsAction
{
NSLog(#"the new stack action");
[self performSegueWithIdentifier:#"PushSegue" sender:self];
}
Update:
By the way, in the interest of full disclosure, there's an alternative to the push segue. If you give that next scene, itself, a "storyboard id", you can use the following code (obviously replacing "insertNextScenesStoryboardIdHere" with the identifier you give your next scene:
- (void)checkRecordsAction
{
NSLog(#"the new stack action");
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"insertNextScenesStoryboardIdHere"];
[self.navigationController pushViewController:controller animated:YES];
}
I personally don't like this as much, as your storyboard now has a scene floating out there without any segue, your code now has dictated the nature of the transition between view controllers vs having everything in the storyboard, etc., but it is another approach.
I'm trying to create a transition between two scenes, this is a dumbed down version of what I have in my production code :
Both are ViewController, the left one has a TableView inside it and when clicked it should transition to the right hand scene, passing along data from whatever cell was clicked.
Currently, with a modal segue I can tap the cell and it transitions correctly, however, I can't figure out how to place a back button onto the nav bar.
I'm transitioning from the cell to the 2nd view controller like so :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"toSecond" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toSecond"])
{
NSLog(#"Preparing segue for toSecond, setting some data on target scene");
NSIndexPath *path = [self.theTableView indexPathForSelectedRow];
MyData * myData = [myDataArray objectAtIndex:path.row];
// Obtain handles on the current and destination controllers
FirstController * startingViewController;
SecondController * destinationController;
startingViewController = (FirstController * ) segue.sourceViewController;
destinationController = (SecondController * ) segue.destinationViewController;
destinationController.someData = myData;
}
}
On the SecondController, I've tried amending the viewDidLoad method to programatically include a back button item as suggested in this previous SO question:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[self.navigationItem setBackBarButtonItem:back];
}
So my question is, how can I get a back button onto that nav bar? Something like this :
Thanks
One (easy) way, using the storyboard, is to embed a navigation controller into your view that has the table. Make sure the correct view, the one with the table, is highlighted and then go to Editor > Embed In > Navigation Controller.
The back button will automatically be there when you click a row of your table. Like Dan said, make sure it is a push segue between the 2 views in your picture.
You will not be able to add a back looking button to a modally presented ViewController easily
If you just want to add a normal button to the left side of the bar, do the following
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonSystemItemDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Title"];
item.leftBarButtonItem = rightButton;
item.hidesBackButton = YES;
[self.yourNavigationBar pushNavigationItem:item animated:NO];
If you insist of presenting your view modally and still want a back button style you could use
Three20
In place of where you have the view with the table, us a UINavigationController don't delete the view with the table, just in place of where you segue to it, or if it was the root controller, use the nav con. Make the view with the table the root view controller of the navigation controller, and then simply use a push segue instead of a modal segue, and you should automatically get the back button
The best way is to just use a UINavigationController as the parent of your table view controller, and use a push segue instead of a modal segue.
You can create two instances of UINavigationItem and tell the UINavigationBar about them by setting the bar's items property. Instance 0 represents the table view controller.
You can create a UIButton with type 101 (the undocumented back button type), and wrap it in a UIBarButtonItem using initWithCustomView:.
My app is based on a tab bar architecture. In order to have an easy internationalisation mechanic, I do not write any string in XIB files. viewdidload permits to change programmaticaly the strings in the views.
When my app launches, I can see the 4 tabs. But in fact only the first one loads its view controller. The others wait for user click to be loaded.
Tabs title can be changed using [self setTitle:#"Mouhahaha"]; in viewDidLoad of loaded view controller.
If I want to keep my internationalisation mechanic available, I do not set in my XIB the name of tabbar items. But, as at start all tab' view controllers are not loaded, I have blank titles for some tabs. The right title is set only when the user click on the tab.
I am looking for a way to set this title programaticaly for each tabbaritem. Do you have hints ?
Thanks a lot.
kheraud
my preferred method of doing this programmatically together with the storyboard is to make a subclass of UITabBarController, have my tab bar controller scene in my storyboard use the new subclass (with 3 UIViewController relationships from the tab bar controller to the desired view controller in the case below), and then override viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSArray* titleKeys = [NSArray arrayWithObjects:#"top places",
#"localizablekey1",
#"localizablekey2",
#"localizablekey3",
nil];
[super viewWillAppear:animated];
int count = 0; for (UIViewController* viewController in self.viewControllers)
viewController.tabBarItem.title = NSLocalizedString([titleKeys objectAtIndex:count++], nil);
}
All you need to do is make an instance of UITabBarController, then alloc any views you want in it, then set the UITabBarController views. Make sure that your TabBarController is the one that is visible. Also make sure that any viewControllers you want in your tab bar are being imported with #import.
UITabBarController *c = [self tabBarController];
SecondViewController *s = [[SecondViewController alloc] init];
[s setTitle:#"Whatever"];
c.viewControllers = [NSArray arrayWithObjects:self, s, nil];
[s release];
Put this in the viewDidLoad method of the first controller allocated.
Alternatively you could just change the way your ApplicationDelegate sets up the TabBarController, but i won't go into that.
Hope this helps