UINavigationController in UIPopoverController: setting the title - iphone

I'm trying to display a UINavigationController inside a UIPopoverController, but for some reason I can't set the title.
SearchViewController *searchView = [[[SearchViewController alloc] init] autorelease];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:searchView];
[popover presentPopoverFromBarButtonItem:_searchBarButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
I tried changing it in the - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Change the title
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = #"Print / Email";
[label sizeToFit];
self.navigationItem.titleView = label;
}
return self;
}
I also tried setting it in the - (void)viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
self.contentSizeForViewInPopover = CGSizeMake(320.0, 250.0f);
self.navigationItem.title = #"Print / Email";
self.title = #"Print / Email";
}
But in no cases the title is working, am I still doing something wrong?

EDIT Add your SearchViewController controller in UINavigationController ad that in UIPopoverController like this:
SearchViewController *searchView = [[[SearchViewController alloc] init] autorelease];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:searchView];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController];
[popover presentPopoverFromBarButtonItem:_searchBarButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Now in SearchViewController's viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Print / Email";
}
Refer setting-the-title-of-a-uipopovercontroller

Try with this way to show popover controller
SearchViewController *searchView = [[[SearchViewController alloc] init] autorelease];
UINavigationController *navCont=[[UINavigationController alloc] initWithRootViewController:searchView];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navCont];
[popover presentPopoverFromBarButtonItem:_searchBarButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

You can try this:
SearchViewController *searchView = [[[SearchViewController alloc] init] autorelease];
UINavigationController *_nav = [[[UINavigationController alloc] initWithRoot.....:searchView] autorelease];
then put _nav into the popover.
each UIViewController has its own tabbar and navigation but you should init them.then you can use them rightly.

Related

Creating TabBar programmatically only for one ViewController not in AppDelegate

I want to create a tabBar in my app but only in a detailView not in the AppDelegate. I am searching how to do this in google but everything what I have figured out is in AppDelegate.m
I am trying to do something like this. This shows me a Tab bar with two buttons(without names) but I want to go from one FirstViewController with tabBar, to one of the two items SecondViewController or ThirdViewController with a navigationBar with one backItemButton for get back to the FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupTabBar];
}
- (void) setupTabBar {
tabBars = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
YPProfileViewController *profile = [[YPProfileViewController alloc] init];
YPProfileViewController *profile2 = [[YPProfileViewController alloc] init];
[localViewControllersArray addObject:profile];
[localViewControllersArray addObject:profile2];
tabBars.tabBarItem = profile2;
tabBars.viewControllers = localViewControllersArray;
tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);
[self.view addSubview:tabBars.view];
}
at least this works for me.
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = #"FIRST";
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = #"SECOND";
vc2.view.backgroundColor = [UIColor redColor];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = #[vc1,vc2];
tabBar.selectedIndex = 1;
tabBar.view.frame = CGRectMake(50, 50, 220, 320);
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];

How to add UITabBarController programmatically (no xib file or storyboard)

I want to add a UITabBarController to my application. But I have to do it with code only. No xib files or storyboards. How to do this entirely through code?
EDIT:
_tbc = [[UITabBarController alloc] init];
aboutUsView = [[AboutUsView alloc] init];
helpView = [[HelpView alloc] init];
optionsView = [[OptionsView alloc] init];
self.navCon = [[UINavigationController alloc] initWithRootViewController:optionsView];
[self setnavigationCon:self.navCon];
[optionsView setdataLayer:self];
if ([navCon.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:#"Navigation Bar_reduced.png"];
[self.navCon.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
[optionsView addSelfView:window];
}
_tbc.viewControllers = [NSArray arrayWithObjects:navCon, aboutUsView, helpView, nil];
[window addSubview:_tbc.view];
Try this
AppDelegate.h
#interface AppDelegate : UIResponder <UITabBarControllerDelegate>
#property (strong, nonatomic) UITabBarController *tabBarController;
AppDeleGate.m
UINavigationController *nc1;
nc1 = [[UINavigationController alloc] init];
[nc1.navigationBar setTintColor:[UIColor blackColor]];
UIViewController *viewController1 = [[[FirstScreen alloc] initWithNibName:#"FirstScreen_ipad" bundle:nil] autorelease];
nc1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
UINavigationController *nc2;
nc2 = [[UINavigationController alloc] init];
[nc2.navigationBar setTintColor:[UIColor blackColor]];
UIViewController *viewController2 = [[[FullList alloc] initWithNibName:#"FullList_ipad" bundle:nil] autorelease];;
nc2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
UIViewController *viewController3 = [[[FavouriteView alloc] initWithNibName:#"FavouriteView_ipad" bundle:nil] autorelease];
UINavigationController *nc3;
nc3 = [[UINavigationController alloc] init];
[nc3.navigationBar setTintColor:[UIColor blackColor]];
nc3.viewControllers = [NSArray arrayWithObjects:viewController3, nil];
UIViewController *viewController4 = [[[UpcomingFights alloc] initWithNibName:#"UpcomingFights_ipad" bundle:nil] autorelease];
UINavigationController *nc4;
nc4 = [[UINavigationController alloc] init];
[nc4.navigationBar setTintColor:[UIColor blackColor]];
nc4.viewControllers = [NSArray arrayWithObjects:viewController4, nil];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nc1, nc2,nc3,nc4 ,nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
ICON FOR TABBAR
In your ViewController.m file do as follow:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self.title = NSLocalizedString(#"YOUR View NAME", #"YOUR VIEW NAME");
self.tabBarItem.image = [UIImage imageNamed:#"YOUR IMAGE NAME"];
return self;
}
Add this code in your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController * fvc=[[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController * svc=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
ThirdViewController * tvc=[[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil];
FourthViewController * fvc2=[[FourthViewController alloc]initWithNibName:#"FourthViewController" bundle:nil];
tabbar=[[UITabBarController alloc]init];
tabbar.viewControllers=[NSArray arrayWithObjects:fvc,svc,tvc,fvc2,nil];
[self.window addSubview:tabbar.view];
}
Here is the code from user1673099 in Swift (nice username btw... ;0) -
func initialize_tabs() {
var ncArr : [UINavigationController] = [UINavigationController]();
for _ in 0...3 {
let nc : UINavigationController = UINavigationController();
let vc = UIViewController();
nc.viewControllers = [vc];
let v : UIView = UIView(frame: UIScreen.mainScreen().bounds);
let redC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
let greenC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
let blueC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
v.backgroundColor = UIColor(red: redC, green: greenC, blue: blueC, alpha: 1);
let l : UILabel = UILabel(frame: UIScreen.mainScreen().bounds);
l.text = "Test Label";
l.textAlignment = .Center;
v.addSubview(l);
vc.view = v;
ncArr.append(nc);
}
self.tabBarController = UITabBarController();
self.tabBarController.viewControllers = ncArr;
return;
}

NavigationController not showing on UIViewController presenting modally

Have a mainviewcontroller and on it have UIToolbar which has a UIBarButtonItem Info which shows UIViewcontroller modally.
Now when pressing Infobutton it is showing UIViewController modally which has UITextView but not showing UINavigationController with Done button.
I am not able to figure it out what i am missing in my code.
This is how i am showing UITextView and NavigationController in UIViewController modally.
#import "ModalViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation ModalViewController
#synthesize textView;
#synthesize navBar;
#synthesize navigationController;
#synthesize delegate;
-(void)dealloc
{
[textView release];
[navBar release];
[navigationController release];
[super dealloc];
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.title = #"Info";
UINavigationController *navigationController = [[UINavigationController alloc]init];
//initWithRootViewController:viewController];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(Done:)] autorelease];
self.textView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)]autorelease];
self.textView.textColor = [UIColor whiteColor];
self.textView.font = [UIFont fontWithName:#"Georgia-BoldItalic" size:14];
//self.textView.delegate = self;
self.textView.backgroundColor = [UIColor brownColor];
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor whiteColor] CGColor];
self.textView.layer.cornerRadius = 1;
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.
self.textView.editable = NO;
//[self.view addSubview:navigationController.view];
[self.view addSubview: self.textView];
//[navigationController release];
}
And this is how UIViewController presented modally
//Create a final modal view controller
UIButton *modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[modalViewButton addTarget:self action:#selector(modalViewAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.rightBarButtonItem = modalBarButtonItem;
- (void) modalViewAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
//self.viewController = [[ModalViewController alloc] init];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//ModalViewController *myModalViewController = [[ModalViewController alloc] init];
//UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myModalViewController];
//navigationController.navigationBar.tintColor = [UIColor brownColor];
_viewController = [[ModalViewController alloc] init];
//[navigationController pushViewController:_viewController animated:YES];
[self presentModalViewController:self.viewController animated:YES];
//[self.view addSubview:navigationController.view];
//[navigationController release];
[myModalViewController release];
}
I will appreciate if you can figure out what i m doing wrong or missing in my code.
Thanks a lot.
This seems like a needlessly convoluted way of displaying the new controller. In my apps, I do it like this:
//this function displays (modally) view controller nested inside a navcontroller
- (void) showModalController
{
YourViewController * ecreator = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
UINavigationController * navcontrol = [[UINavigationController alloc] initWithRootViewController: ecreator];
[self presentModalViewController: navcontrol animated:YES];
[navcontrol release];
[ecreator release];
}
Now you want to do the graphical setup (navbar color etc) in the initWithNib and/or viewDidLoad functions of the YourViewController.
#synthesize navigationController;
so navigationController is your class member variable.
in the function
- (void) viewDidLoad
you declare a local variable
UINavigationController *navigationController
Please notice that the second navigationController is different from your member variable navigationController .
So, inside viewDidLoad you need to create object of your member variable navigationController. Not the local variable navigationController.
Do not RE-declare navigationController in viewDidLoad. Instead, create the object using member variable like:
navigationController = [[UINavigationController alloc]init];
Try this, it worked great for me. I had the exact same problem
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#“segue_name"])
{
UINavigationController *nav = [segue destinationViewController];
ExampleViewController *exampleVC = (ExampleViewController *) nav.topViewController;
//Setup any properties here and segue
}
}

Changing the color of a UINavigationBar

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Send to Twitter"
style:UIBarButtonItemStyleDone
target:self
action:#selector(save)];
}
return self;
}
Another piece of code
- (void)loadView
{
[super loadView];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.view.backgroundColor = [UIColor whiteColor];
self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.delegate = self;
textView.font = [UIFont systemFontOfSize:15];
textView.contentInset = UIEdgeInsetsMake(5,5,0,0);
textView.backgroundColor = [UIColor whiteColor];
textView.autoresizesSubviews = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
}
I'm trying to change the navigation bar color to black, however no matter what i do here, the color still stays default (Blue). How do you change the color of the bar on top of the view??
Try something like:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
This should be done on viewWillAppear: as during init method self.navigationController will be obtained as nil and hence will not produce any effect.
Hope this helps.
If you use a programatic approach (seems like you don't) you could write this code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// window initialization
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *viewController = [[YourViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] \
initWithRootViewController:viewController];
[viewController release];
[[navigationController navigationBar] setTintColor:[UIColor blackColor]];
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
It works for me. Hope it helps! ;)

how to modify title and badge icon in UITabBarController

I have the following code:
tabBarViewController = [[TabBarViewController alloc] init];
mvc = [[MapViewController alloc] init];
tvc = [[TableViewController alloc] init];
tabBarViewController.viewControllers = [NSArray arrayWithObjects: tvc, mvc, nil];
However, when opening up in the simulator, I don't see any title or badge for these two UITabBar. How can I assign title and badge to these two ?
You must set each child view controller's tabBarItem property:
MapViewController* mvc = [[MapViewController alloc] init];
UIImage* mapIcon = [UIImage imageNamed:#"mapIcon.png"];
mvc.tabBarItem = [[[UITabBarItem alloc] initWithTitle:#"Map" image:mapIcon tag:0] autorelease];
mvc.tabBarItem.badgeValue = #"abc";
TableViewController* tvc = [[TableViewController alloc] init];
tvc.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0] autorelease];
mvc.tabBarItem.badgeValue = #"100";
TabBarViewController has a property to its UITabBar.
The UITabBar has the list of UITabBarItem that have the badge
[tabBarViewController.tabBar.items objectAtIndex:0].badgeValue = #"1";
why not you use this code in tvc, mvc
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.tabBarItem = [ [ UITabBarItem alloc ]
initWithTitle:#"YOUR TITLE"
image:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"IMAGE" ofType:#"png"]]
tag:1 ];
}
return self;
}