UITabbarController change items dynamically - uiviewcontroller

I want to edit UITabbarItems during the view life time.
The background is that I have an App with login view and according to whether the user is admin or not, the admin TabbarItem should be visible or not.
I'm using this code to initially create the UITabbarController in the AppDelegate:
// AppDelegate.m
settings = [[settingsViewController alloc] init];
settings.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Einstellungen" image:[UIImage imageNamed:#"settings.png"] tag:3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:readState, friends, settings, nil];
When I try to manipulate the items later from another UIViewController, nothing happens and the UITabbar remains like it was before.
I actually tried two ways I could imagine:
[[self tabBarController] setToolbarItems:[[[self tabBarController] toolbarItems] arrayByAddingObject:admin] animated:NO];
[[self tabBarController] setViewControllers:[[[self tabBarController] viewControllers] arrayByAddingObject:admin] animated:NO];
How can I reach my goal? Thanks in advance, with kind regards, Julian

I figured out a workaround for my problem. I don't really like to import the AppDelegate but it seems as the tabbarController property is not being automatically set for the UIViewControllers of a UITabbarController.
// generate an instance of the needed UIViewController
adminViewController *admin = [[adminViewController alloc] init];
admin.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Admin" image:[UIImage imageNamed:#"admin.png"] tag:5];
// get the AppDelegate instance
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// get the »viewControllers« array of the AppDelegates UITabbarController as mutable array
NSMutableArray *viewControllersArray = [NSMutableArray arrayWithArray:[[appDelegate tabBarController] viewControllers]];
// insert the UITabbarItem of the needed UIViewController
[viewControllersArray insertObject:admin atIndex:2];
// Finally tell the app delegates UITabbarController to set the needed viewControllers
[[appDelegate tabBarController] setViewControllers:viewControllersArray animated:NO];

Related

How to pass some variable to parent view controller?

I have a UIViewController its my main view controller. When the app starting that main viewcontroller is loading. Then there is a bar button. When I click on that, another view controller loding like this.
LoginViewController *viewController = [[LoginViewController alloc]
initWithNibName:#"LoginViewController" bundle:nil];
viewController.mainViewController = self ;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
navController.navigationBar.tintColor = [UIColor blackColor];
[self presentModalViewController:navController animated:YES];
In that new view controller I am doing some tasks and when click the close button, the results should be loaded in on the parent view controller.How can I do this. In parent view controller ViewDidAppear also not calling when I just close the top view controller. How can I do this? Please help me.
Thanks
First of all the line of code
[self presentModalViewController:navController animated:YES];
is deprecated and its been a long time, I don't know why are you still using it.
// Use below code instead
[self presentViewController:viewController animated:YES completion:nil];
Also you need to use protocols and delegates to pass any object to parent viewcontroller while you are dismissing the other viewcontroller.
If you can show up some code we might be able to figure out that why your viewDidAppear is not calling.

viewcontroller(xib)-random call in ios7

Does the following code call the viewcontrollers randomly?
Here controller is my NSArray,where all my viewcontroller names are stored.
[self.navigationController pushViewController:arc4random_uniform(controllers.count) animated:YES];
Please help
arc4random_uniform returns integer value. If you want to implement this, then you might need to use this -
[self.navigationController pushViewController:[controllers objectAtIndex:arc4random_uniform(controllers.count)] animated:YES];
Update -
NSString *className = [controllers objectAtIndex:arc4random_uniform(controllers.count)];
UIViewController *viewController = (UIViewController*)[[NSClassFromString(#"NameofClass") alloc] init];
[self.navigationController pushViewController:viewController animated:YES];

Is there any way to load Direct Map View ,hiding default North America loading?

I have made an GPS Location based Mapkit application for a store where it has four locations at the table view and when the user selects any one of them then it navigates to MapView and all the stuff gets loaded but my problem is that at the loading time it is showing North America Map after loading completes i am getting the required map view.I need either activity indicator or blank grey map screen to be display at the loading time.
i am calling the mapview programatically where my coding is
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 372.0f)];
self.view = contentView;
[contentView release];
routeMapView = [[MKMapView alloc] initWithFrame:contentView.frame];
routeMapView.delegate = self;
routeMapView.showsUserLocation = YES;
[contentView addSubview:routeMapView];
[routeMapView release];
routeOverlayView = [[UICRouteOverlayMapView alloc] initWithMapView:routeMapView];
UIBarButtonItem *space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *currentLocationButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"reticle.png"] style:UIBarButtonItemStylePlain target:self action:#selector(moveToCurrentLocation:)] autorelease];
UIBarButtonItem *routesButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"list.png"] style:UIBarButtonItemStylePlain target:self action:#selector(showRouteListView:)] autorelease];
self.toolbarItems = [NSArray arrayWithObjects:currentLocationButton, space, /*mapPinButton,*/ routesButton, nil];
[self.navigationController setToolbarHidden:NO animated:NO];
Atlast i found the solution for my question,
first defining a local region co-ordinate like this
MKCoordinateRegion region={{0.0,0.0},{0.0,0.0}};
then defining animation for changes made over it,
[routeMapView setRegion:region animated:YES];
then
activityIndicator =[[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]autorelease];
activityIndicator.frame=CGRectMake(0.0, 0.0, 40.0, 40.0);
activityIndicator.center=self.view.center;
[self.view addSubview:activityIndicator];
if([UIApplication sharedApplication].networkActivityIndicatorVisible=YES)
{
[activityIndicator startAnimating];
}
then declaring [activityIndicator stopAnimating]; at the delegate function where all the end of the operation takes place.

presentModalViewController from TableView

I have created a custom cell with buttons.
the buttons should present a modalViewController, one for the email and another for a webView.
I have also created a delegate to get a callback from the class.
so in the cell class:
_delegateAction = [[HistoryVC alloc] init];
_delegateAction.delegate = self;
[_delegateAction openData:_data Type:_title.text]; //start process
The class that the table is on is a UIviewController that have a table view. the (with the custome cells that are another class inherit from UiTableViewCell.
if you need meed information just tell me.
in the UIviewController I am trying to present a modal view as follow:
-(void) openData: (NSString *)data Type:(NSString*)type
{
QRWebView * webView = [[QRWebView alloc] init];
webView.url = data;
[self presentModalViewController: webView animated:YES];
}
The modalview doesn't show. I know that I'm doing something wrong, but what is it?
Thanks in advance.
You have created a new view controller with _delegateAction = [[HistoryVC alloc] init]; but you haven't made it part of the controller hierarchy by presenting it in any way.
When you call [self presentModalViewController: webView animated:YES];, your self is the new controller and, since it's not in the hierarchy, it's not capable of presenting another controller.
I have managed to fix it by implementing the PresentModalView.
What I did is to create a showModal and DismissModal in the AppDelegate class.
Those method add to the Window the view I wanted by adding it as:
[Window addsubview TheView];
and for the Dismiss is removeFromSuperview.
I implemented an animation to fake the modalView animation.
to get to those methods I have used the shareApplication.
and it's works.

programmatically creating a splitviewcontroller with two uiviewcontrollers that use navigation controllers

I would like to create a UISplitViewController programmatically and have as its master and detail controller (the left and right pane), two UIViewController subclasses which I use to handle various things like processing when cells are selected and so forth.
My problem is that I'm not certain how to create navigation controllers within these custom master and detail controllers and hook them up to the splitview controller. Currently, I add my controllers like this:
mSplitViewController.viewControllers = [NSArray
arrayWithObjects:mSplitMasterController,mSplitDetailController, nil];
and I within each custom controller I have a container UIView created on which I can assemble my subviews. I'm not sure how I add the navigation controllers to these. Do I need to basically do something like this within my custom controllers' loadView?
[self.view addSubview:mNavigationController.view];
after constructing the controllers (and their views) that the navigation controller will manage?
You don't need UIView containers. If wanted to use containers you would need UIViewControllers. Regardless just add the navigation controller to the array directly. Later you can access them from the same array to push and pop on the other split view.
RootViewController* rootViewController = [[RootViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[rootViewController release];
splitVC = [[UISplitViewController alloc] init];
[window addSubview:splitVC.view];
TempViewController* tempViewController = [[TempViewController alloc] initWithNibName:#"TempViewController" bundle:nil];
detailNavController = [[UINavigationController alloc] initWithRootViewController:tempViewController];
[tempViewController release];
splitVC.viewControllers = [NSArray arrayWithObjects:navController, detailNavController, nil];