I try create tab bar controller programatically, it works but I can not set title to ta bar items. I can not see title when I running my application. My code is here. Please help me, what is the problem?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
myTabBarController = [[UITabBarController alloc] init];
view2Controller = [[testView alloc] init];
view3Controller = [[testView2 alloc] init];
view4Controller = [[testView3 alloc] init];
view5Controller = [[testView4 alloc] init];
view6Controller = [[testView5 alloc] init];
myTabBarController.viewControllers = [NSArray arrayWithObjects: view2Controller, view3Controller,view4Controller,view5Controller,view6Controller,nil];
UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
[tabItem setTitle:#"theTitle"];
[self.view addSubview:myTabBarController.view];
myTabBarController.selectedIndex=0;
}
Set the title of ur viewcontroller in side viewcontroller's viewwillappear/disappear and it will be displayed in ur tabbaritem.
You can set the title of UIViewController, which reflects when it is pushed in UINavigationController or a UITabBarController. But you should set the title before putting it inside any of them.
init is generally a good place to set the title.
- (id)init {
// ... other code including check for self
self.title = #"My Title";
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// self.title = #"My Title";
}
self.title = #"My Title";
return self;
}
Use above code in every subviews of tabbar controllers.
This may be help.
use this code
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title=#"FREE BOOKS";
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
Here is the tutorial from the scratch which should help you to achieve the exact functionality you want to achieve. Download the source code and run it directly and if title are correct the you can follow the tutorial to get the proper understanding of the flow of UITabBarController.
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.tabBar.items?[0].title = "Title"
}
}
OR
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBar.items?[0].title = "Title"
}
Related
So I tried to make some changes to a pod someone wrote only they did it in objective c So instead of rewriting the entire thing to swift I figured I would just make the changes in objective c since it would take less time then completely rewriting it.
However I can't seem to get one of my customisations working.
I needed to override the initWithNibName to add an additional parameter to the initialiser. Here is what it looks like now
- (id)initWithNibName:(NSString *)nibNameOrNil dayNib:(NSString *)nibDayNibOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
_allowClearDate = NO;
_allowSelectionOfSelectedDate = NO;
_clearAsToday = NO;
_daysInFuture = NO;
_daysInHistory = NO;
_historyFutureBasedOnInternal = NO;
_autoCloseCancelDelay = 1.0;
_dateTimeZone = [NSTimeZone defaultTimeZone];
_slideAnimationDuration = .5;
_selectedDates = [[NSMutableArray alloc] init];
_selectedDateViews = [[NSMutableArray alloc] init];
_allowMultiDaySelection = NO;
if(nibDayNibOrNil == nil) {
[self setDayNibName:#"MHDateDay"];
} else {
[self setDayNibName:nibDayNibOrNil];
}
}
return self;
}
However when I install the pod and try to initialise the view controller in a swift project it will give me the following suggestion
var dp = MHDatePickerViewController(nibName: <String>, bundle: <Bundle>)
so for some reason it doesn't seem to see the customisation I did to the initialiser
Does anyone happen to know where I went wrong? why it isn't showing me the correct options?
I implemented a TabBar Controller, it is initalised like this
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
firstView = [[FirstView alloc]init];
secondView = [[SecondView alloc]init];
firstItem = [[UITabBarItem alloc]initWithTitle:#"First" image:nil tag:0];
secondItem = [[UITabBarItem alloc]initWithTitle:#"Second" image:nil tag:1];
firstView.tabBarItem = firstItem;
secondView.tabBarItem = secondItem;
NSArray *viewArray = [[NSArray alloc]initWithObjects:firstView, secondView, nil];
[self setViewControllers:viewArray];
}
return self;
}
In the AppDidFinishedLoading I just did this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
mainbarController = [[MainBarController alloc]initWithNibName:#"MainBarController" bundle:nil];
[self.window setRootViewController:mainbarController];
[self.window makeKeyAndVisible];
return YES;
}
My problem is, I want to place the tabBar somewhere else, not at the bottom, as it is when I run the project. But I can´t find a xib File to my code. The mainwindow.xib is just an empty window. I know it´s very basic but I´m confused about this.
thanks anyway
I try this but it not works I can not see tab bar item..How can I solve this problem? Thanks for your help.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
myTabBarController = [[UITabBarController alloc] init];
tab1 = [[ZiyaretFormTab1 alloc] initWithNibName:#"ZiyaretFormTab1" bundle:nil];
tab2 = [[ZiyaretFormTab2 alloc] initWithNibName:#"ZiyaretFormTab2" bundle:nil];
tab3 = [[ZiyaretFormTab3 alloc] initWithNibName:#"ZiyaretFormTab3" bundle:nil];
tab4 = [[ZiyaretFormTab4 alloc] initWithNibName:#"ZiyaretFormTab4" bundle:nil];
tab5 = [[ZiyaretFormTab5 alloc] initWithNibName:#"ZiyaretFormTab5" bundle:nil];
myTabBarController.viewControllers = [NSArray arrayWithObjects: tab1, tab2,tab3,tab4,tab5,nil];
UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
[tabItem setTitle:#"theTitle"];
[self.view addSubview:myTabBarController.view];
myTabBarController.selectedIndex=0;
}
In each of your view controllers that go into the tabs (ZiyaretFormTab1 to ZiyaretFormTab5), insert this code into initWithNib or viewDidLoad functions.
UITabBarItem * tabtitle = [[UITabBarItem alloc] initWithTitle: #"title"
image: nil //or your icon
tag: 0];
[self setTabBarItem: tabtitle];
Tab bar titles are controlled by their respective view controllers. The easiest way to set the title is from within the view controller for that tab, instead of trying to set it in the tab directly.
Each view controller has a title property which is used to set the title in tab bars and navigation bars. It also has a tabBarItem property which has it's own title property that you can set if you want to just set the tab and not affect navigation bars.
So in the viewDidLoad of your viewController, you could write:
self.tabBarItem.title = #"theTitle";
Put the tab settings to AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Put your name and icon information uniforms visit here:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Kulağını tersten tutmana gerek yok";
self.tabBarItem.image = [UIImage imageNamed:#"ringtab.png"];
}
return self;
}
Have you tried to set
[tab1 setTitle:#"Your Title"]
after allocating and initialising it.
UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:0];
[item setTitle:#"fgfdgd"];
I want to package some UIViews suchs as UIButton, UIImageView in a single UIView.
When I try to display that view, it is not showing up on my RootViewController:
Here is the code of my UIView subclass :
#import "Hover.h"
#implementation Hover
- (id)init{
self = [super init];
if (self) {
// Initialization code
UIImage *hover = [UIImage imageNamed:#"Hover.png"];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = hover;
imageView.frame = CGRectMake(0, 0, hover.size.width, hover.size.height);
imageView.alpha = 0.75;
[self addSubview:imageView];
[imageView release];
}
return self;
}
And here is the RootViewController class:
- (void)viewDidLoad
{
Hover *hover = [[Hover alloc] init];
[self.navigationController.view addSubview:hover];
[hover release];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
The "hover view" is not displayed! However, when I add a single UIImageView to my RootViewController, it works!
In order for your view to display, you should override the -(id)initWithFrame:(CGRect)frame;, instead of writing a custom initializer.
Try this way :
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self) {
// do init here...
}
return self;
}
Also, in the -(void)viewDidLoad; method, first send [super viewDidLoad];, and then alloc/init the view.
- (void)viewDidLoad
{
[super viewDidLoad];
Hover *hover = [[Hover alloc] init];
[self.navigationController.visibleViewController.view addSubview:hover];
[hover release];
// Do any additional setup after loading the view, typically from a nib.
}
Just a quick guess at a glance: If you're using a navigationcontroller, you should initWithRootViewController. You can't add a view to a navigationcontroller directly, Use push instead.
HTH
Marcus
I am using a UINavigationController in the appdelegate as a basis for all views and I am using the TTNavgitor for antother flow starting with a TTTableViewController.
When I use the TTNavigator for some reason I have 2 problems:
After I choose one of the rows there is no back button in the viewcontroller loaded
The transition is not animated when selecting a row in the table
I am using the basis of the Three20 examples:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.title = #"Settings";
self.navigationItem.backBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:nil action:nil] autorelease];
self.tableViewStyle = UITableViewStyleGrouped;
TTNavigator* navigator = [TTNavigator navigator];
navigator.supportsShakeToReload = YES;
navigator.persistenceMode = TTNavigatorPersistenceModeNone;
navigator.window = self.view.window;
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]];
[map from:#"tt://login" toViewController:[LoginController class]];
}
return self;
}
Does some has an idea why do I have these problems? and how can I solve this fast without rewriting all of the code?