UIWebView not scrolling in a UITabBarController - iphone

I have setup 4 view controllers each containing a UIWebView and set these up as the Tabs for a UITabBarController.
The content loads fine, and the buttons are clickable, however the content of the UIWebViews refuses to scroll. UIWebView has an inbuilt UIScrollView to handle it's own scrolling, so it should just scroll right?
My initialisation code in the main file is:
tabBarController = [[MainTabBarController alloc] init];
[tabBarController.view setBounds:CGRectMake(0, 0, dev_portrait.size.width, 44)];
[tabBarController setDelegate:self];
UIImage* anImage = [UIImage imageNamed:#"53-house.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:#"Home" image:anImage tag:0];
WebViewController *vc1 = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
[vc1 setup];
[vc1 setTabBarItem:theItem];
/// ... same for the other 3 WebViewControllers
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, vc3, vc4, aNavigationController, nil];
tabBarController.viewControllers = controllers;
//self.window.rootViewController = tabBarController;
self.window.rootViewController = tabBarController;
When a tab is touched, it fires a function in the view controller which loads the URL:
- (void)handleURL:(NSString *)url {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSLog(#"Handling URL: %#", request);
[webpage loadRequest:request];
lastURL = url;
[self startLoaderIndicator];
}

Related

How to add multiple Controllers that are same instance to my tabBarController?

I want to add multiple Controllers that are same instance to my tabBarController.but I can't.the added controllers of array treated as a one controller. How I should do ??and What is the best way??this is my code.
RootViewController *rootViewController = [[[RootViewController alloc]init]autorelease];
tabBarController = [[[UITabBarController alloc]init]autorelease];
[tabBarController setDelegate:self];
[tabBarController setViewControllers:[NSArray arrayWithObjects:rootViewController,rootViewController,rootViewController,rootViewController,rootViewController,nil] animated:NO];
You could try this :
RootViewController *rootViewController = [[[RootViewController alloc]init]autorelease];
RootViewController *rootViewController2 = [rootViewController retain];
RootViewController *rootViewController3 = [rootViewController retain];
tabBarController = [[[UITabBarController alloc]init]autorelease];
[tabBarController setDelegate:self];
[tabBarController setViewControllers:[NSArray arrayWithObjects:rootViewController, rootViewController2, rootViewController3, nil] animated:NO];

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];
}

UIInterfaceOrientation problem

My program is not supporting the UIInterfaceOrientation. Program will not support the UIInterfaceOrientation after I add the UITabBarItem.Please give a solution. Also I added the navigationController.
Here is my code.
-(void) applicationDidFinishLaunching:(UIApplication *)application {
//I create my navigation Controller
//UINavigationController *navigationController;
//I create my TabBar controlelr
tabBarController = [[UITabBarController alloc] init];
// Icreate the array that will contain all the View controlelr
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
// I create the view controller attached to the first item in the TabBar
sivajitvViewController *firstViewController;
firstViewController = [[sivajitvViewController alloc]init];
navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
//[navigationController.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
firstViewController.navigationItem.title=#"Gallery";
//viewController.tabBarItem.image = [UIImage imageNamed:#"natural.jpg"];
navigationController.tabBarItem.image = [UIImage imageNamed:#"Gallery.png"];
navigationController.tabBarItem.title = #"Gallery";
//navigationController.headerTitle = #"Some Title";
[localControllersArray addObject:navigationController];
[navigationController release];
[firstViewController release];
// I create the view controller attached to the second item in the TabBar
SecondViewController *secondViewController;
secondViewController = [[SecondViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
//[navigationController.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2];
navigationController.tabBarItem.image = [UIImage imageNamed:#"News.png"];
navigationController.tabBarItem.title = #"News";
[localControllersArray addObject:navigationController];
[navigationController release];
[secondViewController release];
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];
// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];
}
If you have UITabBarController, all the tabs should support your interface orientation. So if you have 3 tabs and 2 of them support portrait and landscape, but the last one supports only portrait, you application will never turn to landscape.

ViewDid appear not working xcode

Hai,
I have created a tab based iRestaura project. I use 5 tab bar items. One of the tabbar name is Customer. When I tap on customer another tabbar is created with 3 viewcontroller programmatically. When I use view didload method tabbarcontroller is created successfully. But when i use view didAppear then tabbar controller is not created tabbar. In both of cases the other three viewcontroller which is created programmatically , there is not view did appear is not working. But all of case I need to use viewDidAppear method .
Plz anyone can help me...
The viewDidAppear method which not working is given bellow .....
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UITabBarController *tabBarController1=[[UITabBarController alloc] init];
CustomerListViewController *customerListViewController=[[CustomerListViewController alloc] init];
customerListViewController.title=#"Customer List";
UIImage *anImage1 = [UIImage imageNamed:#"customer.png"];
UITabBarItem *theItem1 = [[UITabBarItem alloc] initWithTitle:#"CustomerList" image:anImage1 tag:0];
customerListViewController.tabBarItem = theItem1;
SelectedCustomerViewController *selectedCustomerViewController= [[SelectedCustomerViewController alloc] init];
selectedCustomerViewController.title=#"Selected Customer";
UIImage *anImage3 = [UIImage imageNamed:#"selectedCustomer.png"];
UITabBarItem *theItem = [[UITabBarItem alloc] initWithTitle:#"Selected Customer" image:anImage3 tag:1];
selectedCustomerViewController.tabBarItem = theItem;
InvoiceListViewController *invoiceListViewController=[[InvoiceListViewController alloc] init];
invoiceListViewController.title=#"Invoice List";
UIImage *anImage2 = [UIImage imageNamed:#"invoiceNo.png"];
UITabBarItem *theItem2 = [[UITabBarItem alloc] initWithTitle:#"Invoice List" image:anImage2 tag:2];
invoiceListViewController.tabBarItem = theItem2;
NSMutableArray *controllers=[[NSMutableArray alloc] init];
[controllers addObject:customerListViewController];
// [controllers1 addObject:vc1];
[controllers addObject:selectedCustomerViewController];
[controllers addObject:invoiceListViewController];
///[controllers1 addObject:vc4];
tabBarController1.viewControllers=controllers;
[self.navigationController setNavigationBarHidden:YES animated:YES];
[[self view] addSubview:tabBarController1.view];
for (int t=0; t<[controllers count]; t++)
{
NSLog(#"controller%#",[controllers objectAtIndex:t]);
}
}
Try this:
Firstly use the below function on your Tabbar controller .m file:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:YES];
}
And in your "viewDidLoad" function of Tabbar controller .m file, use "localNavigationController.delegate = self;" as used in given example below.
UserListing *nearBy = [[UserListing alloc] init];
nearBy.tabBarItem.image = [UIImage imageNamed:#"icoTabNearby.png"];
nearBy.tabBarItem.title = #"Nearby";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:nearBy];
localNavigationController.delegate = self;
Hope it works for you.

How to resize UITableView within UINavigationController within UITabBarViewController?

So my app has a UITabBarController. Within that, one of the tabs is a UIViewController containing a UINavigationController. Within that is a UITableViewController.
The problem is that for some reason the size of the TableView is off, and part of the bottom entry of the table is obscured by the TabBar. I've tried everything I can think of to resize the tableview but to no avail. Here are the relevant parts of the code:
AppDelegate
tabBarController = [[UITabBarController alloc] init];
LogbookViewController *firstVC = [[LogbookViewController alloc] init];
PaceCalcViewController *secondVC = [[PaceCalcViewController alloc] init];
SettingsTableViewController *thirdVC = [[SettingsTableViewController alloc] init];
...
// Add them as children of the tab bar controller
tabBarController.viewControllers = [NSArray arrayWithObjects: firstVC, secondVC, thirdVC, nil];
LogbookViewController (subclass of UIViewController)
- (void)viewDidLoad {
[super viewDidLoad];
navigationController = [[UINavigationController alloc] init];
[self.view addSubview:navigationController.view];
WorkoutListTableViewController *listController = [[WorkoutListTableViewController alloc] init];
listController.managedObjectContext = self.managedObjectContext;
[navigationController pushViewController:listController animated:NO];
[listController release];
}
The WorkoutListTableViewController (subclass of UITableViewcontroller) currently doesn't have anything in particular in it that I would think would affect it, but here's the ViewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Workouts";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"List" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *fileName = #"short_bg.png";
NSString *filePath = [paths stringByAppendingPathComponent:fileName];
UIImage *paper = [[UIImage alloc] initWithContentsOfFile:filePath];
UIImageView *paper_bg = [[UIImageView alloc] initWithImage:paper];
self.tableView.backgroundView = paper_bg;
[fileName release];
[paper release];
//self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];
/*UIBarButtonItem *importButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Import" style:UIBarButtonItemStylePlain target:self action:#selector(importDialogAction:)];
self.navigationItem.leftBarButtonItem = importButtonItem;
[importButtonItem release];*/
}
}
I've tried things like listController.view.frame = CGRectMake(whatever) in the LogbookViewcontroller, or self.tableView.frame - CGRectMake(whatever) in the WorkoutListTableViewController, but nothing has worked. Any ideas? I can post more of the code if that helps.
You should be added the Navigation Controller directly to the UITabBarController, then it should size appropriately:
tabBarController = [[UITabBarController alloc] init];
WorkoutListTableViewController *listController = [[WorkoutListTableViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:listController];
PaceCalcViewController *secondVC = [[PaceCalcViewController alloc] init];
SettingsTableViewController *thirdVC = [[SettingsTableViewController alloc] init];
...
// Add them as children of the tab bar controller
tabBarController.viewControllers = [NSArray arrayWithObjects: navController, secondVC, thirdVC, nil];
You don't need the View Controller in the 1st tab to add a UINavigationController to the view, just go ahead and add the TableViewController as the root of the nav controller and add the nav controller into the 1st tab.