Navigation pushview Controller is not working - iphone

I am implement the TabBar app for ipad & iphone both. But In IPAD the navigation controller is not working for ipad nib in table cell. The viewdid Load method is not call for ipad.
But it work fine for iphone..
self.navigationController is NULL for ipad only in my class to navigate.
Help me!!
my code is as follow:
//Connet.m:
if(i==1)
{
TwitterController *tc;
if ([self isPad]) {
tc = [[TwitterController alloc] initWithNibName:#"TwitterController_ipad" bundle:nil];
}
else
tc = [[TwitterController alloc] initWithNibName:#"TwitterController" bundle:nil];
[self.navigationController pushViewController:tc animated:YES];
NSLog(#"%#",self.navigationController); /Problem ****NULL*****///////
//TWITTER.m
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//Custom initialization
self.navigationController.navigationBarHidden = YES;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
NSString *urlAddress = #"https://twitter.com/UJUDGEIT1";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webv loadRequest:requestObj];
}

Try the following code:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
NSString *nibname=#"";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
nibname=#"TwitterController_ipad";
}
else{
nibname=#"TwitterController";
}
TwitterController *view = [[TwitterController alloc] initWithNibName:nibname bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:view];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Write this code in your app delegate class. The viewDidLoad method gets called.

piyush
how you implement your tab bar.
make each controller of tab bar as navigation controller. it may called your view did load method.
following code may help you. it create 2 controller on tab bar. finally at it to root view controller.
-(void)createTabbar{
NSMutableArray *controllers = [[NSMutableArray alloc] init];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//Controller 1
PasswordVC *pvc = [[PasswordVC alloc] initWithNibName:#"PasswordVC" bundle:nil];
UINavigationController *passwordNav = [[UINavigationController alloc] initWithRootViewController:pvc];
passwordNav.navigationController.navigationBarHidden=YES;
[controllers addObject:passwordNav];
pvc.title = #"Password";
[pvc release];
[passwordNav release];
//Controller 2
SettingVC *SVC = [[SettingVC alloc] initWithNibName:#"SettingVC" bundle:nil];
UINavigationController *settingNav = [[UINavigationController alloc] initWithRootViewController:SVC];
settingNav.navigationController.navigationBarHidden=YES;
[controllers addObject:settingNav];
SVC.title = #"Settings";
[SVC release];
[settingNav release];
}
else { //ipad
}
_tabBarController = [[[UITabBarController alloc] init] autorelease];
_tabBarController.delegate=self;
_tabBarController.viewControllers = controllers;
_tabBarController.customizableViewControllers = controllers;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_window cache:NO];
_window.rootViewController = _tabBarController;
[UIView commitAnimations];
}

Related

get reference of the application window in the succeeding classes IPHONE app

I am adding tab controller on my 4th screen. Up to 4th screen my navigation bar is visible
now on the 4th screen when i am adding Tab Controller to the window, navigation bar is getting disappeared...
code written in the tabbedController class is :
- (void)viewDidLoad
{
self.tabController = [[UITabBarController alloc]init];
FirstTabScreen *firstTab = [[FirstTabScreen alloc]initWithNibName:nil bundle:nil];
SecondTabScreen *secondTab = [[SecondTabScreen alloc]initWithNibName:nil bundle:nil];
firstTab.title=#"First";
firstTab.tabBarItem.image = [UIImage imageNamed:#"small_star.png"];
secondTab.title=#"second";
secondTab.tabBarItem.image = [UIImage imageNamed:#"small_star.png"];
tabController.viewControllers = [NSArray arrayWithObjects:
firstTab,
secondTab,
nil];
// self.tabWindow = [[[[UIApplication sharedApplication]keyWindow ]subviews ] lastObject];
//self.tabWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.tabWindow = [[UIApplication sharedApplication] keyWindow ];
//self.tabWindow = self.appDelegateAccess.window;
self.tabWindow = self.appDelegateAccess.window;
[self.tabWindow addSubview:tabController.view];
[self.tabWindow makeKeyAndVisible];
// [self.view addSubview:tabsContainer.view];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Please let me know where I am going wrong....
i think I am not referencing the root window for adding the tab controller ..
it it is the case please suggest me the way to take root window for adding tab controller
Thanks
why are you adding the tabbarcontroller in the window. why dont you add it in self.view ?
First of all make object of tabBar in YourAppDelegate. And write following code in following methode
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.tabController = [[UITabBarController alloc]init];
FirstTabScreen *firstTab = [[FirstTabScreen alloc]initWithNibName:nil bundle:nil];
SecondTabScreen *secondTab = [[SecondTabScreen alloc]initWithNibName:nil bundle:nil];
firstTab.title=#"First";
firstTab.tabBarItem.image = [UIImage imageNamed:#"small_star.png"];
secondTab.title=#"second";
secondTab.tabBarItem.image = [UIImage imageNamed:#"small_star.png"];
tabController.viewControllers = [NSArray arrayWithObjects:
firstTab,
secondTab,
nil];
/*
Your Other Code
*/
}
Then in your third View controller's .m file import your appDelegate like..
#import "YourAppDelegate.h"
Last Step write following code on selecting a row of your third view controller...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YourAppDelegate * appDel = (YourAppDelegate *)[[UIApplication SharedApplication] delegate];
[appDel.window setRootViewController:appDel.tabController];
}
You need to add navigation controller separately,
Try this,
self.tabController = [[UITabBarController alloc]init];
FirstTabScreen *firstTab = [[FirstTabScreen alloc]initWithNibName:nil bundle:nil];
SecondTabScreen *secondTab = [[SecondTabScreen alloc]initWithNibName:nil bundle:nil];
UINavigationController *navControllerOne = [[UINavigationController alloc] initWithRootViewController: firstTab];
navControllerOne.navigationBar.tintColor = [UIColor blackColor];
navControllerOne.tabBarItem.image=[UIImage imageNamed:#"star.png"];
navControllerOne.tabBarItem.title = #"First";
UINavigationController *navControllerTwo = [[UINavigationController alloc] initWithRootViewController: secondTab];
navControllerTwo.navigationBar.tintColor = [UIColor blackColor];
navControllerTwo.tabBarItem.image=[UIImage imageNamed:#"star.png"];
navControllerTwo.tabBarItem.title = #"Second";
tabController.viewControllers = [NSArray arrayWithObjects:
navControllerOne,
navControllerTwo,
nil];
// self.tabWindow = [[[[UIApplication sharedApplication]keyWindow ]subviews ] lastObject];
//self.tabWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.tabWindow = [[UIApplication sharedApplication] keyWindow ];
//self.tabWindow = self.appDelegateAccess.window;
self.tabWindow = self.appDelegateAccess.window;
[self.tabWindow addSubview:tabController.view];
[self.tabWindow makeKeyAndVisible];
// [self.view addSubview:tabsContainer.view];
great !!!! it fixed ...
I have removed
self.tabWindow = self.appDelegateAccess.window;
[self.tabWindow addSubview:tabController.view];
[self.tabWindow makeKeyAndVisible];
and added only one line
[self.view addSubview:tabController.view];
Finally it worked !!!
Thanks Rohit ..

Tabbar controller load empty xib

I'm trying to build an app that have main view at the beginning with 3 buttons, then when the user press any button of these buttons, the tab bar view will appear with selected tab bar item.
My problem is here, when the tab bar view should appear ... it appears empty !!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
MainMenuViewController *mainMenuViewController = [[[MainMenuViewController alloc] initWithNibName:#"MainMenuViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:mainMenuViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
//Button Action in main menu view
-
(IBAction)button1Action:(id)sender {
TabbarViewController *tempView = [[TabbarViewController alloc] initWithNibName:#"TabbarViewController" bundle:nil];
[self.navigationController pushViewController:tempView animated:YES];
[tempView release];
}
You have to set viewControllers property of your TabbarViewController(of course if it is superclass of UITabBarController). Create your 3 viewControllers in init method of TabbarViewController, add them to array and set it as viewControllers property like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(#"%#",[[self class] superclass]);
UIViewController *yourFirstViewController = [[UIViewController alloc] init];
UIViewController *yourSecondViewController = [[UIViewController alloc] init];
UIViewController *yourThirdViewController = [[UIViewController alloc] init];
yourFirstViewController.title = #"First";
yourSecondViewController.title = #"Second";
yourThirdViewController.title = #"Third";
NSArray *threeViewControllers = [[NSArray alloc]initWithObjects:yourFirstViewController, yourSecondViewController, yourThirdViewController, nil];
self.viewControllers = threeViewControllers;
// Custom initialization
}
return self;
}

UIWebView in UINavigationController not scrolling

I have an application which is bascially a TabBar where the tabs are UINavigationControllers which move back and forth between different ViewControllers containing UIWebViews. My problem is that although I can click on the buttons for the UIWebViews, I can't scroll the content when it is larger than the screen.
Am creating the TabController
tabBarController = [[MainTabBarController alloc] init];
[tabBarController setDelegate:self];
When switching between tabs, I have this code:
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
UIWebView *wv = [[UIWebView alloc] init];
[wv setUserInteractionEnabled:YES];
[wvc setTitle:[nc title]];
[wv setDelegate:self];
[wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]]];
[wvc setWebpage:wv];
[wvc setView:wv];
[nc pushViewController:wvc animated:NO];
NSLog(#"NC.viewcontroller count: %d", [nc.viewControllers count]);
[nc setLastURL:requestURL];
[wvc startLoaderIndicator];
[wv release];
[wvc release];
requestOk = YES;
And when a link within one of the UIWebViews is clicked, I have this:
ItemNavigationController *nc = (ItemNavigationController *)[tabBarController selectedViewController];
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
//wvc.view = wvc.sView;
[wvc setTitle:[nc title]];
[wv setDelegate:self];
[wv loadRequest:request];
[wvc setWebpage:wv];
[wvc setView:wv];
[nc pushViewController:wvc animated:NO];
NSLog(#"NC.viewcontroller count: %d", [nc.viewControllers count]);
[nc setLastURL:request.URL.absoluteString];
[wvc startLoaderIndicator];
[wv release];
[wvc release];
requestOk = YES;
Everything works perfectly, except that the WebViews don't scroll. It looks something like this:
First thing I don't like in this code block...
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
wvc.view = wvc.sView;
Instead create and add the subView to the view in the viewDidLoad block of the view controller. Furthermore, avail yourself of IB and add the WebView via the XIB and simply connect the IBOutlet and delegate.
If you insist upon doing it this way don't overwrite the view,
[wvc.view addSubview:wv];
I wrote this in a simple WebViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://news.google.com"]]];
}
and called it from a AppDelegate like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
CPWebController *webController = [[CPWebController alloc] init];
CPNavController *navController = [[CPNavController alloc] initWithRootViewController:webController];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
It sizes correctly, has a navbar, and scrolls correctly. Please forgive the lack of releases for code brevity.
From what I can see you're setting it with a fixed height here:
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,**480**)];
So the content size is only going to be 480 so there's nowhere to scroll.
Try something like (0,0,320,1000); to test if that is the issue
If so set the height to be the cotent size so it's dynamic
Create WebViewController class and inside .m in viewDidLoad add:
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[wv setDelegate:self];
NSURL*url = [NSURL URLWithString:#"http://www.link.com/twitter_iPhone.php"];
[wv loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:wv];
}

custom tab bar controller not working?

I am trying to build a custom tab bar controller but for some reason the views will not switch... The initial view is loaded properly. Here is my init method:
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
AccountViewController *accountViewController = [[AccountViewController alloc]
initWithNibName:#"AccountViewController" bundle:nil];
MoreViewController *moreViewController = [[MoreViewController alloc]
initWithNibName:#"MoreViewController" bundle:nil];
BarTabViewController *barTabViewController = [[BarTabViewController alloc]
initWithNibName:#"BarTabViewController" bundle:nil];
LocationsViewController *locationsViewController = [[LocationsViewController alloc]
initWithNibName:#"LocationsViewController" bundle:nil];
self.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
[self.view addSubview:locationsViewController.view];
self.selectedController = locationsViewController;
return self;
}
Like I said, this will display the selected controller properly, however when the app launches and I try to switch views with the tab bar, the subview just become grey... I have been looking through several tutorials to try to figure out this issue, but it seems that I am doing it exactly the same. I have also checked the IB file to make sure my tabs are connected properly, they are. The following is the code to switch items:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item == locationsTabBarItem) {
UIViewController *locationsController = [viewControllers objectAtIndex:0];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:locationsController.view];
self.selectedController = locationsController;
}
else if (item == accountsTabBarItem) {
UIViewController *accountsController = [viewControllers objectAtIndex:1];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:accountsController.view];
self.selectedController = accountsController;
}
else if (item == barTabTabBarItem) {
UIViewController *barTabController = [viewControllers objectAtIndex:2];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:barTabController.view];
self.selectedController = barTabController;
}
else {
UIViewController *moreController = [viewControllers objectAtIndex:3];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:moreController.view];
self.selectedController = moreController;
}
}
try With this one
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.selectedIndex = 0;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
self.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:delegate.tabBarController animated:YES];

UITabBarController + UISplitviewController alternatives?

As this configuration is not supported I was wondering what alternatives people have used.
I have a universal app which currently uses a 4 tab UITabBarController on both the iPhone and iPad.
As I'd now like to use the splitviewcontroller I am faced with a design decision.
I guess I could just go with a toolbar at the top and go from there but was hoping there were more interesting techniques.
You could replicate a UITabBar within a modelViewController that pops up when a button on the bottom of the screen is taped and swap Views. :)
I created a UITabBarController subclass which properly propagates the rotation messages to all UISplitViewControllers it contains. This maintains the correct internal state of the UISplitViewControllers. However, one of the SplitViewController delegate methods is not called if the SplitViewController is not visible, so I account for this in the detail view controller viewWillAppear method. I've confirmed this works in iOS5.0 - iOS6.1
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
OSMasterViewController *masterViewController = [[[OSMasterViewController alloc] initWithNibName:#"OSMasterViewController_iPhone" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
self.window.rootViewController = self.navigationController;
} else {
OSMasterViewController *masterViewController = [[[OSMasterViewController alloc] initWithNibName:#"OSMasterViewController_iPad" bundle:nil] autorelease];
UINavigationController *masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
OSDetailViewController *detailViewController = [[[OSDetailViewController alloc] initWithNibName:#"OSDetailViewController_iPad" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
masterViewController.detailViewController = detailViewController;
UISplitViewController *splitViewController = [[[OSSplitViewController alloc] init] autorelease];
splitViewController.viewControllers = #[masterNavigationController, detailNavigationController];
splitViewController.delegate = detailViewController;
OSTestViewController *secondaryController = [[[OSTestViewController alloc] init] autorelease];
OSTabBarController *tabBarController = [[[OSTabBarController alloc] init] autorelease];
tabBarController.viewControllers = #[self.splitViewController, secondaryController];
self.window.rootViewController = tabBarController;
}
[self.window makeKeyAndVisible];
return YES;
}
OSTabBarController.m
#import "OSTabBarController.h"
#implementation OSTabBarController
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
for(UIViewController *targetController in self.viewControllers){
if(targetController != self.selectedViewController && [targetController isKindOfClass:[UISplitViewController class]]){
[targetController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
for(UIViewController *targetController in self.viewControllers){
if(targetController != self.selectedViewController && [targetController isKindOfClass:[UISplitViewController class]]){
[targetController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
}
}
#end
DetailViewController
#implementation OSDetailViewController
-(void)viewWillAppear:(BOOL)animated{
//the splitViewController:willHideViewController:withBarButtonItem:forPopoverController: may not have been called
if(!UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
self.navigationItem.leftBarButtonItem = nil;
}
}
#pragma mark - UISplitViewControllerDelegate Methods
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
}
#end