How to Activate Tab IOS - ios

How to set selected tab in objective c,
i am adding tab programmatically,
Here is my code for that:
#import "TabControllerViewController.h"
#import "DetailsView.h"
#interface TabControllerViewController (){
NSMutableArray *kirtanDetail;
CGPoint startPosition;
}
#end
#implementation TabControllerViewController
- (void)viewDidLoad {
[super viewDidLoad];
dbm=[[DBManager alloc]init];
[dbm openDB];
kirtanDetail=[dbm getAllPad:[NSString stringWithFormat:#"%#",_kirtanID]];
// Do any additional setup after loading the view.
[self addTabView:[kirtanDetail count]];
[self.tabBarController setDelegate:self];
self.tabBarController.selectedIndex = 2;
}
-(void)viewDidAppear:(BOOL)animated{
// [[NSNotificationCenter defaultCenter] postNotificationName:#"activeTab" object:#"2"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)addTabView:(NSInteger *) tab {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
for(int i=0;i<tab;i++){
UIViewController *view1 = [sb instantiateViewControllerWithIdentifier: #"DV"];
[view1 viewDidLoad];
[tabViewControllers addObject:view1];
//can't set this until after its added to the tab bar
view1.tabBarItem =[[UITabBarItem alloc] initWithTitle:[NSString stringWithFormat:#"PAD %d",i+1] image:[UIImage imageNamed:#"view1"]tag:i];
UITextView *textView=[[view1.view subviews]objectAtIndex:0 ];
NSArray *data=[kirtanDetail objectAtIndex:i];
if([[data objectAtIndex:0] isEqualToString:[NSString stringWithFormat:#"%#",_kirtanID]]){
NSLog(#"Active TAB=%d",i);
}
textView.text=[NSString stringWithFormat:#"%#",[dbm getKirtanDetails:[NSString stringWithFormat:#"%#",[data objectAtIndex:0]]]];
}
[self setViewControllers:tabViewControllers];
}
-(void)setActiveTab:(NSNotification *)notification{
[self.tabBarController setSelectedIndex:[notification object]];
}
#end

[self.tabBarController setSelectedIndex:1];
Add this after viewDidLoad, in viewWillAppear
EDIT:
Is this controller TabControllerViewController subclass of UITabBarController or UIVIewController? If it's subclass of UITabBarController then replace [self.tabBarController setSelectedIndex:1] with [self setSelectedindex:1]

Related

Scroll tab bar programmatically with dynamic tabs programmatically

If we have more than 5 tabs... put the 5th tab as "More" tab.. then on clicking the "More" tab rest of the options will be shown in a table view...
I have created the same Dynamic tabs programmatically.
But instead of showing in more tab I need it to show it scrollable.
Here is my tab class.
I have, LoginView,testvc1,testvc2,testvc3,testvc4,testvc5,testvc6,testvc7 viewcontroller.
Loginview(contain a button)-> on click of button tab bar is open.
TabBarVC.h
#import <UIKit/UIKit.h>
#interface TabBarVC : UITabBarController
{
UITabBarController *tabController;
}
#property(nonatomic,retain)NSString *tabOrders;
#end
TabBarVC.m
#import "TabBarVC.h"
#import "testvc1.h"
#import "testvc2.h"
#import "testvc3.h"
#import "testvc4.h"
#import "testvc5.h"
#import "testvc6.h"
#import "testvc7.h"
#interface TabBarVC ()
{
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TabBarVC *tabBarController;
#end
#implementation TabBarVC
- (void)viewDidLoad {
self.tabOrders = #"HOME|home,LOVE|love|,MATCH|match|";
self.tabOrders=[self.tabOrders stringByAppendingString:#",OpenFB|www.facebook.com,OpenWeb|www.google.com,web|www.google.com,YAHOO|www.yahoo.com,GOOGLE|www.google.com"];
//====================================================================================
//========================= View controller used to be assign ========================
//====================================================================================
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
NSMutableArray *resttabViewControllers = [[NSMutableArray alloc] init];
NSMutableArray *tabOrderArray=[[self.tabOrders componentsSeparatedByString:#","] mutableCopy];
for (int i=0; i<[tabOrderArray count]; i++)
{
NSArray *arr_tab=[[tabOrderArray objectAtIndex:i] componentsSeparatedByString:#"|"];
NSString *tabName=[arr_tab objectAtIndex:0]; //get array name from pipe serate
NSString *tabAction=[arr_tab objectAtIndex:1];
if ([tabAction isEqualToString:#"home"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
testvc1 *testvc1 = [storyboard instantiateViewControllerWithIdentifier:#"testvc1"];
testvc1.title = #"testvc1";
testvc1.tabBarItem.image=[UIImage imageNamed:#"homeicon"];
UINavigationController *testvc1NavVC = [[UINavigationController alloc] initWithRootViewController: testvc1];
[tabViewControllers addObject:testvc1NavVC];
}
else if ([tabAction isEqualToString:#"love"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
testvc2 *testvc2 = [storyboard instantiateViewControllerWithIdentifier:#"testvc2"];
testvc2.title =#"Love";
testvc2.tabBarItem.image=[UIImage imageNamed:#"loveicon"];
UINavigationController *testvc2NavVC = [[UINavigationController alloc] initWithRootViewController: testvc2];
[tabViewControllers addObject:testvc2NavVC];
}
else if ([tabAction isEqualToString:#"match"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
testvc3 *testvc3 = [storyboard instantiateViewControllerWithIdentifier:#"testvc3"];
testvc3.title = #"MATCH";
testvc3.tabBarItem.image=[UIImage imageNamed:#"matchicon"];
UINavigationController *testvc3NavVC = [[UINavigationController alloc] initWithRootViewController: testvc3];
[tabViewControllers addObject:testvc3NavVC];
}
else
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
testvc4 *testvc4 = [storyboard instantiateViewControllerWithIdentifier:#"testvc4"];
testvc4.title = tabName; //TabTitle
testvc4.tabBarItem.image=[UIImage imageNamed:#"icon_rest"];
UINavigationController *testvc4NavVC = [[UINavigationController alloc] initWithRootViewController:testvc4];
[resttabViewControllers addObject:testvc4NavVC];
}
}
for (int i=0; i<[resttabViewControllers count]; i++) {
[tabViewControllers addObject:[resttabViewControllers objectAtIndex:i]];
}
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:15/255.0 green:68/255.0 blue:140/255.0 alpha:1.0]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:15/255.0 green:68/255.0 blue:140/255.0 alpha:1.0];
self.navigationController.navigationBar.alpha = 0.7f;
self.navigationController.navigationBar.translucent = YES;
UINavigationController* morenav = self.tabBarController.moreNavigationController;
morenav.navigationBar.tintColor = [UIColor whiteColor];;
morenav.navigationBar.barTintColor=[UIColor colorWithRed:15/255.0 green:68/255.0 blue:140/255.0 alpha:1.0];
[self.navigationController.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor whiteColor]}];
[self.navigationController.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor whiteColor]}];
[self.tabBarController.tabBar setBarTintColor:[UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0]];
[self setViewControllers:tabViewControllers];
self.customizableViewControllers = nil;
[self setSelectedIndex:0];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
to open tab bar i called
-(IBAction)openTab:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tbc = [storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
[self presentViewController:tbc animated:NO completion:nil];
}
My Storyboard have below uiviewcontrollers with no segue connection with each other.
1) UITabBarController (with class name tabbar and with identifier UITabBarController)
2) UIViewcontrollers
testview1,testview2,testview3,testview4,testview5,testview6,testview7
I need it to be scrollable like this image with dynamic tabs

UIWebView Not Showing Up

I just added a new UIWebView and it doesn't seem to be showing up. I'm pretty new here, so this could be a very simple error.
I created the view using the storyboard and dragged it into my GuideViewController.h
This is my GuideViewController.h
#import <UIKit/UIKit.h>
#interface GuideViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *newsWebView;
#end
This is my GuideViewController.m
#import "GuideViewController.h"
#interface GuideViewController ()
#end
#implementation GuideViewController
#synthesize newsWebView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *newsURL = [NSURL URLWithString:#"http://www.yahoo.com"];
NSURLRequest *newsRequest = [NSURLRequest requestWithURL:newsURL];
[newsWebView loadRequest:newsRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This is what I'm using to add the view to my app delegate. I already have a sidebar menu added.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
VidViewController *vidViewController = [[VidViewController alloc] init];
GuideViewController *newsWebView = [[GuideViewController alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:vidViewController];
[self.window setRootViewController:navController1];
SelectVideo1 *selectVideo1 = [[SelectVideo1 alloc] initWith:#"Test"];
//[self.navController pushViewController:selectVideo1 animated:YES];
SelectVideo1 *selectVideo2 = [[SelectVideo1 alloc] initWith:#"Latest News"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *tabStoryBoard = [UIStoryboard storyboardWithName:#"TabStoryboard" bundle:nil];
UIStoryboard *navStoryBoard = [UIStoryboard storyboardWithName:#"NavStoryboard" bundle:nil];
UINavigationController *navController = [navStoryBoard instantiateViewControllerWithIdentifier:#"Nav Controller"];
UITabBarController *tabController = [tabStoryBoard instantiateViewControllerWithIdentifier:#"Tab Controller"];
ViewController *redVC, *greenVC;
redVC = [[ViewController alloc] init];
greenVC = [[ViewController alloc] init];
RootController *menuController = [[RootController alloc]
initWithViewControllers:#[navController, selectVideo1, selectVideo2, tabController, newsWebView]
andMenuTitles:#[#"Test", #"Videos", #"Latest News", #"Walkthroughs", #"Official News"]];
self.window.rootViewController = menuController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#end
The view shows up in the menu bar, but when I click it, nothing happens. I believe the problem lies in my appdelegate and how I call the view?
If you're using a nib file to define your GuideViewController, use
GuideViewController *newsWebView = [[GuideViewController alloc] initWithNibName:#"Insert_Nib_Name_Here" bundle:nil];
If you define your GuideViewController in a storyboard, use
UIStoryboard *navStoryBoard = [UIStoryboard storyboardWithName:#"NavStoryboard" bundle:nil];
GuideViewController *newsWebView = [navStoryBoard instantiateViewControllerWithIdentifier:#"Insert_Guide_Controller_Name_Here"];
If that doesn't work, make sure your IBOutlet UIWebView is connected (control+drag from the UIWebView object in your nib file)
You can also just add the UIWebView programmatically.
In GuideViewController.h, you probably want to conform to the UIWebViewDelegate protocol. Implement the methods to control the UIWebView's functionality
#interface GuideViewController : UIViewController <UIWebViewDelegate>
In GuideViewController.m...
- (GuideViewController*)init
{
self = [super init];
if (self) {
//Adjust the frame accordingly if you don't want it to take up the whole view
self.newsWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
self.newsWebView.delegate = self;
[self.view addSubview:self.newsWebView];
}
return self;
}

Add UIScrollView with Paging to Existing UIViewController

I'm wanting to add a UIScrollView with paging to go through different views from my existing view controller which is the root view of my app. I also have tab bar and navigation bar controllers along with it. Can I add a scroll view to this view controller to accomplish what I'm wanting, and if so, can someone point me in the right direction on how to go about it?
Here is my view controller.
#import "KFBViewController.h"
#import "ListViewController.h"
#import "ActionAlertsViewController.h"
#import "MarketUpdatesViewController.h"
#import "AgStoriesViewController.h"
#import "KFBNewsViewController.h"
#import "MemberBenefits.h"
#import "SocialNetworks.h"
#import "WebViewController.h"
#import "YouTubeView.h"
#import "KFBFlickrViewController.h"
#import "RSFM.h"
#import "UAPush.h"
#import "TUSafariActivity.h"
#interface KFBViewController ()
{
}
#end
#implementation KFBViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.title = NSLocalizedString(#"Home", #"Home");
self.tabBarItem.image = [UIImage imageNamed:#"home"];
self.navigationController.delegate = self;
}
return self;
}
- (void) showMenu
{
NSURL *urlToShare = [NSURL URLWithString:#"https://itunes.apple.com/us/app/kentucky-farm-bureau/id580530986?mt=8"];
NSArray *activityItems = #[urlToShare];
// TUSafariActivity *activity = [[TUSafariActivity alloc] init];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:TRUE completion:nil];
}
- (IBAction)gotoSecondView
{
YouTubeView *youTubeView = [[YouTubeView alloc] initWithNibName:#"YouTubeView" bundle:nil];
youTubeView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:youTubeView animated:YES completion:nil];
}
- (IBAction)gotoPublicAffairs
{
ListViewController *publicAffairs = [[ListViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[publicAffairs setWebViewController:wvc];
publicAffairs.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:publicAffairs animated:YES];
}
- (IBAction)gotoActionAlerts
{
ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[actionAlerts setWebViewController:wvc];
actionAlerts.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:actionAlerts animated:YES];
}
- (IBAction)gotoMarketUpdates
{
MarketUpdatesViewController *marketUpdates = [[MarketUpdatesViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[marketUpdates setWebViewController:wvc];
marketUpdates.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:marketUpdates animated:YES];
}
- (IBAction)gotoAgStories
{
AgStoriesViewController *agStories = [[AgStoriesViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[agStories setWebViewController:wvc];
agStories.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:agStories animated:YES];
}
- (IBAction)gotoKFBNews
{
KFBNewsViewController *kfbNews = [[KFBNewsViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[kfbNews setWebViewController:wvc];
kfbNews.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:kfbNews animated:YES];
}
- (IBAction)gotoMemberBenefits
{
MemberBenefits *memberBenefits = [[MemberBenefits alloc] initWithNibName:nil bundle:nil];
memberBenefits.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:memberBenefits animated:YES];
}
-(IBAction)gotoPhotos:(id)sender
{
KFBFlickrViewController *photosView = [[KFBFlickrViewController alloc] initWithNibName:#"KFBFlickrViewController" bundle:nil];
[self.navigationController pushViewController:photosView animated:YES];
}
- (IBAction)gotoSocialNetworks
{
SocialNetworks *socialNetworks = [[SocialNetworks alloc] initWithNibName:nil bundle:nil];
socialNetworks.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:socialNetworks animated:YES];
}
- (IBAction)gotoFarmMarkets
{
RSFM *rsfm = [[RSFM alloc] initWithNibName:nil bundle:nil];
rsfm.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:rsfm animated:YES];
}
- (IBAction)settingsButtonPressed:(id)sender
{
[UAPush openApnsSettings:self animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"Home";
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:#"\u2699" style:UIBarButtonItemStyleBordered target:self action:#selector(settingsButtonPressed:)];
[settingsButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:24], UITextAttributeFont,nil] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = settingsButton;
UIBarButtonItem *systemAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(showMenu)];
self.navigationItem.rightBarButtonItem = systemAction;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
#end
This works really well for me:
Declare a property for your UIScrollView, and set your ViewController as a UIScrollViewDelegate
#interface ViewController : UIViewController<UIScrollViewDelegate>
#property (strong, nonatomic) UIScrollView *theScrollView;
#end
**Note that I'm setting up my UIScrollView with code, but it can easily be done with a XIB.
In viewDidLoad: of your ViewController
self.theScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 300)];
self.theScrollView.delegate = self;
self.theScrollView.pagingEnabled = YES;
self.theScrollView.showsHorizontalScrollIndicator = NO;
self.theScrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:self.theScrollView];
NSArray *viewArray = [NSArray arrayWithObjects://your UIViews];
for(int i=0; i<viewArray.count; i++)
{
CGRect frame;
frame.origin.x = self.theScrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.theScrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
[subview addSubview:[viewArray objectAtIndex:i]];
[self.theScrollView addSubview:subview];
}
self.theScrollView.contentSize = CGSizeMake(self.theScrollView.frame.size.width * viewArray.count, self.theScrollView.frame.size.height);
Most paging scrollViews also have a UIPageControl associated with it. To do that, override this UIScrollViewDelegate method:
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = self.theScrollView.frame.size.width;
int page = floor((self.theScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.thePageControl.currentPage = page;
}
I would perhaps try using UIPageViewController, rather than a regular paging scrollview.
Apple's PhotoScroller sample code provides a very good example of how to use UIPageViewController.
I also have some code on github that modifies PhotoScroller to load the UIPageViewController inside a UIViewController subclass. (In Apple's sample code, the UIPageViewController is the root view controller.)
I have never used a UIPageViewController with different UIViewController subclasses in each page (the PhotoScroller sample code uses PhotoViewControllers in all pages), but I can't see why it couldn't be done with a few modifications to the code.
This link helps for you. Here page controller and scrollview both are used -
PageControl Example in iPhone

How to pass tab index value from UITabBarController to UIViewController

I've created tab bar items in the first controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
StageViewController *stageViewRegular = [[StageViewController alloc] init];
[stageViewRegular setTitle:#"Regular"];
stageViewRegular.tabBarItem.tag = 1;
StageViewController *stageViewAdvanced = [[StageViewController alloc] init];
[stageViewAdvanced setTitle:#"Advanced"];
stageViewAdvanced.tabBarItem.tag = 2;
NSArray* controllersArray = [NSArray arrayWithObjects:stageViewRegular, stageViewAdvanced, nil];
tabBarController.viewControllers = controllersArray;
tabBarController.selectedIndex = 0;
[self.view addSubview:tabBarController.view];
and I want to pass tabBarItem.tag to the controllers as mentioned above
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
chooseLevel = viewController.tabBarItem.tag;
}
but in the second controller
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(#"%d", chooseLevel);
}
the chooseLevel always record old value. If I press the first tab and then press the second tab, the value in chooseLevel is 1 rather than 2.
Does anyone know how to solve it?
Do the following steps :
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
viewController1.tabBarItem.tag = 0;
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
viewController2.tabBarItem.tag = 1;
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
FirstViewController.m
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(#"%d",self.tabBarItem.tag);
}
SecondViewController.m
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(#"%d",self.tabBarItem.tag);
}
OutPut :
2013-06-27 18:13:03.913 sampletabs[3981:c07] 0
2013-06-27 18:13:05.282 sampletabs[3981:c07] 1
Thanks, Do let me know it works for you ?
Why don't you use selectedIndex property of the UITabBarController. I think it has the value you are trying to use.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html
You can use selected index property like this:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
chooseLevel = tabBarController.selected index;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
int selectedtag=self.tabBarController.selectedIndex;
NSLog(#"%d", selectedtag);
}
Do this :
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
FirstViewController *stageViewRegular = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[stageViewRegular setTitle:#"First"];
stageViewRegular.tabBarItem.tag = 1;
SecondViewController *stageViewAdvanced = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[stageViewAdvanced setTitle:#"Second"];
stageViewAdvanced.tabBarItem.tag = 2;
NSArray* controllersArray = [NSArray arrayWithObjects:stageViewRegular, stageViewAdvanced, nil];
tabBarController.viewControllers = controllersArray;
tabBarController.selectedIndex = 0;
[self.view addSubview:tabBarController.view];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"%d",viewController.tabBarItem.tag);
}
FirstViewController
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"viewWillAppear %d",self.tabBarItem.tag);
}
SecondViewController
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"viewWillAppear %d",self.tabBarItem.tag);
}
If you need Global value of selected tab Index, then create readWrite property in AppDelegate and set the value in either RootViewController or First/SecondViewController.you will get global value of ChooseLevel.
Thanks,

iOS - Second View is not displaying over tab view

I have a tabBarView and a GWDNativeViewController. GWDNativeViewController is a main menu. I am trying to get that view to display first and overtop the tabBarView. I have this same code set within an IBAction and it works. When I put it into the Loaddidfinishwithoptions it doesn't work.
#implementation GWDNativeAppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
#synthesize secondView = _secondView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[GWDNativeFirstViewController alloc] initWithNibName:#"GWDNativeFirstViewController" bundle:nil];
UIViewController *viewController2 = [[GWDNativeSecondViewController alloc] initWithNibName:#"GWDNativeSecondViewController" bundle:nil];
UIViewController *viewController3 = [[GWDNativeThirdViewController alloc] initWithNibName:#"GWDNativeThirdViewController" bundle:nil];
UIViewController *viewController4 = [[GWDNativeFourthViewController alloc] initWithNibName:#"GWDNativeFourthViewController" bundle:nil];
UIViewController *viewController5 = [[GWDNativeFifthViewController alloc] initWithNibName:#"GWDNativeFifthViewController" bundle:nil];
//tabBarController Stuff
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, viewController5, nil];
self.window.rootViewController = self.tabBarController;
//Specify W`enter code here`hich tab to display (Number need to be set based on button selected)
//self.tabBarController.selectedIndex = 2;
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
[self.window makeKeyAndVisible];
return YES;
}
Here is the the code in the GWDNativeFirstViewController.m
The code in the IBAction works.. Code in the viewDidLoad doesn't.?
-(IBAction)pressedButton {
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:secondView animated:YES];
//[secondView release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"First", #"First");
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
- (void)viewDidLoad
{
[contentWebView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://m.web.org/?iapp=1#tab1"]]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
}
I'm not sure why it doesn't work in viewDidLoad, but it can work in applicationDidFinishLaunchingWithOptions: if you put your [self.window makeKeyAndVisible] line before you define secondView.
[self.window makeKeyAndVisible];
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
If you want it to appear immediately, change the animated option to NO, and delete the line defining the modalTransitionStyle.
The tabBarController's view hasn't loaded here yet. You could put that code into the viewDidLoad in one of your 5 view controllers.

Resources