fetching tabledata in ios from oneview controller to anoher view controller [duplicate] - ios

This question already has answers here:
calling methods across view controllers
(2 answers)
Closed 9 years ago.
Please give the suggestion of these code
appdelegate.h
#import <UIKit/UIKit.h>
#import "MainViewController.h"
#interface MainAppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigationController;
}
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, retain) UINavigationController *navigationController;
#property (nonatomic, retain) MainViewController *MainviewController;
appdelegate.m
#import "MainAppDelegate.h"
#import "MainViewController.h"
#implementation MainAppDelegate
#synthesize navigationController, MainviewController;
#synthesize matri;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
matri=#"";
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.MainviewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] init];
self.window.rootViewController = self.navigationController;
[self.navigationController pushViewController:self.MainviewController animated:YES];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.window makeKeyAndVisible];
return YES;
} //plaese provide detail of code
mainviewcontroller.h
#import <UIKit/UIKit.h>
#import "SecViewController.h"
#interface MainViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
mainviewcontroller.m
#import "MainViewController.h"
#import "MainAppDelegate.h"
#interface MainViewController ()
#end
#implementation MainViewController
{
NSArray *tableData;
MainAppDelegate *appDelegate;
}
//#synthesize pun;
- (void)viewDidLoad
{
[super viewDidLoad];
//appDelegate=(MainAppDelegate*)[[UIApplication sharedApplication]delegate ];
tableData = [NSArray arrayWithObjects:#"1",#"2",#"3",#"4", nil];
NSLog(#"tabledata : %#", tableData);
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"tabledata : %#", tableData);
return [tableData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
// NSLog(#"Value at index:%#",cell.textLabel.text);
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecViewController *SecView = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
SecView.newarray =[tableData objectAtIndex:indexPath.row];
[self.navigationController pushViewController:SecView animated:YES ];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
i dont understand how to call secondview from first view help me out
the hole code file are for creating simple table view and to call secviewcontroller
can anyone help me to provide code how to call secview object and how to use in mainview controller
and i also created nsmutablearray with newarray object and synthesize it
can any one understand this code and give the brief description to that code and suggest me to what i miss sothat i can check this and call secviewcontroller from mainviewcontroller.

use this code into app delegate.m file
YourviewController *loginVc = [[YourviewController alloc] initWithNibName:#"YourviewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:loginVc];
self.window.rootViewController = self.navigationController;
self.navigationController.navigationBarHidden =YES;
[self.window makeKeyAndVisible];

CHeck this:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
matri=#"";
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.MainviewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.MainviewController]; //Embedding your navigationController in mainviewcontroller
self.window.rootViewController = self.navigationController;
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.window makeKeyAndVisible];
return YES;
}
And then in your mainViewCOntroller.m
#import"appdelegate.h"
And then in
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecViewController *SecView = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
SecView.newarray =[tableData objectAtIndex:indexPath.row];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:SecView animated:YES ];
}

Do this one.
self.MainviewController = [[MainviewController alloc] initWithNibName:#"MainviewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.MainviewController];
self.window.rootViewController = self.navigationController;
self.navigationController.navigationBarHidden =YES;
[self.window makeKeyAndVisible];

Related

show side bar for iPad like facebook in landscapemode in ios

I am having an iPad app in which I want to implement a Side Bar functionality like we have in facebook app.
I am using this Demo for this.
With this I have successfully implemented the Side Bar functionality and its working well but with that my first view doesn't show me well.
Below is the screenshot.
As you can see from the screenshot, there is a black background and my whole view is not showing in full screen when the app launches.
It should be like below.
Also on clicking the button the Side View is showing like below.
It should be small as I have taken the view size with width = 300 and height = 768.
But it is showing bigger than that.
Here is my code which I change in my appdelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self createEditableCopyOfDatabaseIfNeeded];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
SlidingViewController *slidingView = [[SlidingViewController alloc]initWithNibName:#"SlidingViewController" bundle:nil];
self.slideMenuController = [[SlideMenuController alloc] initWithCenterViewController:navController];
self.slideMenuController.leftViewController = slidingView;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
- (IBAction)sideBarPressed:(id)sender
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.slideMenuController.position == TKSlidePositionCenter) {
[appDelegate.slideMenuController presentLeftViewControllerAnimated:YES];
} else {
[appDelegate.slideMenuController presentCenterViewControllerAnimated:YES];
}
}
I want this for my iPad for Landscape mode only.
Please tell me What is wrong here?
I am stuck here for quite a while.
Any help will be appreciated.
Thanks in advance.
So better u need to use UISplitViewController
//in app delegate do like this
//in appDelegate.h file
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, retain) UISplitViewController *splitViewCOntroller;
#end
//in appDelegate.m file
#import "AppDelegate.h"
#import "SplitMasterViewController.h" //create a UITableviewController
#import "SplitViewDetailController.h" //create a UIViewController
#implementation AppDelegate
#synthesize splitViewCOntroller = _splitViewCOntroller;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
SplitMasterViewController *masterController = [[SplitMasterViewController alloc]initWithNibName:#"SplitMasterViewController" bundle:nil]; //this is the master menu controller
UINavigationController *masterNavController = [[UINavigationController alloc]initWithRootViewController:masterController];
SplitViewDetailController *detailViewController = [[SplitViewDetailController alloc]initWithNibName:#"SplitViewDetailController" bundle:nil]; //this is the master detail controller
UINavigationController *detailNavController = [[UINavigationController alloc]initWithRootViewController:detailViewController];
masterController.detailViewController = detailViewController;
_splitViewCOntroller = [[UISplitViewController alloc]init]; //initilise split controller
_splitViewCOntroller.delegate = detailViewController; //set the delegate to detail controller
_splitViewCOntroller.viewControllers = [NSArray arrayWithObjects:masterNavController,detailNavController, nil]; //set the splitview controller
self.window.rootViewController = _splitViewCOntroller; //finally your splitviewcontroller as the root view controller
[self.window makeKeyAndVisible];
return YES;
}
//in SplitMasterViewController.h this must be a table that contains your side bar menu items
#import "ViewController.h" //comment this if it shows any error
#import "SplitViewDetailController.h"
#interface SplitMasterViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
#property (nonatomic, retain) SplitViewDetailController *detailViewController; //to get the detailview from masterMenu controller
#property (nonatomic, retain) NSArray *Names;
#end
// in SplitMasterViewController.m file
#import "SplitMasterViewController.h"
#import "SplitViewDetailController.h"
#interface SplitMasterViewController ()
#end
#implementation SplitMasterViewController
#synthesize Names;
#synthesize detailViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
Names = [[NSArray alloc] initWithObjects:#"apple", #"banana",
#"mango", #"grapes", nil];
[self.tableView selectRowAtIndexPath:
[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO
scrollPosition:UITableViewScrollPositionMiddle];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// [self configureCell:cell atIndexPath:indexPath];
cell.textLabel.text = [Names objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SplitViewDetailController *detailController = self.detailViewController;
detailController.myLabel.text = [Names objectAtIndex:indexPath.row]; //set the name from the array
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
//in SplitViewDetailController.h
#import "ViewController.h"
#interface SplitViewDetailController : UIViewController<UISplitViewControllerDelegate>
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) IBOutlet UILabel *myLabel;
#property (nonatomic, retain) UIBarButtonItem *leftBarButtonItem; //to show a button on left side
#end
//in SplitViewDetailController.m
#import "SplitViewDetailController.h"
#interface SplitViewDetailController ()
{
UIPopoverController *masterPopoverController;
}
#end
#implementation SplitViewDetailController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// _leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Menu" style:UIBarButtonItemStyleBordered target:self action:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//these are the call back to detail view controller to hide or show the button
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
_leftBarButtonItem = barButtonItem;
_leftBarButtonItem.style = UIBarButtonItemStyleBordered;
_leftBarButtonItem.title = #"Menu";
[self.navigationItem setLeftBarButtonItem:_leftBarButtonItem animated:YES];
}
// Called when the view is shown again in the split view, invalidating the button
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
}
#end
I successfully implemented this side bar menu functionality with this Demo code and its working perfectly well in my case.
Hope it works to someone else also.
Thanks for your help and suggestions.
i am using this and it's good
https://github.com/ECSlidingViewController/ECSlidingViewController

Multiple PDFs in different tabs

I have a Tabbar iOS application with three tabs. I have each tab set up to be a UIWebView. I want each web view to display a different PDF. How do I assign, through code, each webview to its corresponding pdf?
It will look something like this in your AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
WebViewController *webViewController1 = [[WebViewController alloc] initWithPDFAtURL:#"urlToPDF1"];
WebViewController *webViewController2 = [[WebViewController alloc] initWithPDFAtURL:#"urlToPDF2"];
WebViewController *webViewController3 = [[WebViewController alloc] initWithPDFAtURL:#"urlToPDF3"];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:webViewController1, webViewController2, webViewController3, nil]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You will need to have a view controller that holds the UIWebView, and could look something like:
// WebViewController.h
#interface WebViewController : UIViewController
#end
// WebViewController.m
#interface WebViewController ()
#property (nonatomic, strong) NSURL *_pdfURL;
#end
#implementation WebViewController
#synthesize _pdfURL;
- (id)initWithPDFAtURL:(NSURL *)url {
self = [super init];
if (self) {
_pdfURL = url;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Load the PDF into a UIWebView
}

Property requires a method to be defined

I'm trying to create a SplitViewController view but I get the following warning:
Property splitViewController requires a method setSplitViewController to be defined -use #Synthesize,#dynamic or provide a method implementation in this class implement.
Here is the code
///AppDelegate.h
#class ViewController;
#class DetailViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate, UISplitViewControllerDelegate>
{
UISplitViewController *splitViewController;
ViewController *viewcontroller;
DetailViewController *detailViewController;
}
#property (nonatomic,retain) UIWindow *window;
#property (nonatomic,retain) DetailViewController *detailViewController;
#property(nonatomic,retain) UISplitViewController *splitViewController;
#property (nonatomic,retain) ViewController *viewController;
#end
///AppDelegate.m"
#import "ViewController.h"
#import "DetailViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize splitviewController;
#synthesize detailViewController;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewController *rootViewController = [[ViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
rootViewController.detailViewController = detailViewController;
splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil];
splitViewController.delegate = detailViewController;
[self.window makeKeyAndVisible];
return YES;
}
///ViewController.h
#import <UIKit/UIKit.h>
#class DetailViewController;
#interface ViewController : UITableViewController{
DetailViewController *detailViewController;
NSMutableArray *phone;
}
#property (nonatomic,retain)IBOutlet DetailViewController *detailViewController;
#property (nonatomic,retain) NSMutableArray *phone;
#end
///ViewController.m
#import "ViewController.h"
#import "DetailViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize detailViewController,phone;
- (CGSize)contentSizeForViewInPopoverView {
return CGSizeMake(320, 600);
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.phone = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"phone" ofType:#"plist"]] retain];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [phone count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Configure the cell.
cell.textLabel.text = [self.phone objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
When a row is selected, set the detail view controller's detail item to the item associated with the selected row.
*/
detailViewController.detailItem = [self.phone objectAtIndex:indexPath.row];
}
- (void)dealloc {
[detailViewController release];
[super dealloc];
}
#end
In ur code, you haven't synthesized your splitViewController property. Since You have not synthesized it the property, the compiler is issuing a warning asking u to either synthesize the property so that it can generate setters and getters automatically for your convinienvce (You can use the generated setters and getters using . notation as in self.splitViewController so synthesize it as
#synthesize splitViewController = _splitViewController
or
implement your own custom setter and getter as
//setter
- (void)setSplitViewController:(UISplitViewController*)splitViewController_ {
//assuming your property has retain identifier
if (splitViewController != splitViewController_) {
[splitViewController release];
splitViewController = [splitViewController_ retain];
}
}
//getter
- (UISplitViewController*)splitViewController {
return splitViewController;
}
or
declaring the property as dynamic using #dynamic splitViewController . This means that the setter and getter for the property will be provided from somewhere else.
EDIT:
replacedidFinishLaunchingWithOptions method in appDelegate.m with the following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_viewController = [[ViewController alloc] initWithNibName:#"ViewController's nib name" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
rootViewController.detailViewController = detailViewController;
splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil];
splitViewController.delegate = detailViewController;
self.window.rootViewController = splitViewController;
return YES;
}
also edit the dealloc:
- (void)dealloc
{
[_window release];
[_viewController release];
[splitViewController release];
[detailViewController release];
[super dealloc];
}
And in viewController viewDidLoad replace self.phones line with this
self.phone = [[NSArray arrayWithObjects:#"Cell ONE",#"Cell TWO",#"Cell THREE",#"Cell FOUR",#"Cell FIVE",#"Cell SIX", nil];
this is just for testing that the array part is loading properly..so that you can see the cells if they are getting created. put a break point in cellForRowAtIndexPath method and see if its getting called
and then finally in didSelect see if the detailItem iVar is not nil.
And Yes, check the NIB names properly before loading them, and also that all outlets in the NIB are properly connected.
Cheers and Have fun.
The problem is you misspelled splitViewController in your #synthesize statement -- you didn't capitalize the v.
You wouldn't run into this problem if you did it the easy way. There's no need for either the instance variables or the #synthesize statements any more -- you get both automatically when create the property.
In your case add #synthesize splitViewController = _splitViewController; and detailViewController = _ detailViewController;
Here is useful code for, how can i add UISplitViewController.
/// AppDelegate.h file
#import <UIKit/UIKit.h>
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate, UISplitViewControllerDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong) UISplitViewController *splitViewController;
#property (nonatomic, strong) MasterViewController *masterVC;
#property (nonatomic, strong) DetailViewController *detailVC;
#property (nonatomic, strong) UINavigationController *mvcNavCon;
#property (nonatomic, strong) UINavigationController *dvcNavCon;
#end
/// AppDelegate.m File
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.masterVC = [[MasterViewController alloc] init];
self.mvcNavCon = [[UINavigationController alloc] initWithRootViewController:self.masterVC];
self.detailVC = [[DetailViewController alloc] init];
self.dvcNavCon = [[UINavigationController alloc] initWithRootViewController:self.detailVC];
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = self;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:self.mvcNavCon, self.dvcNavCon,nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
return YES;
}

"Applications are expected to have a root view controller at the end of application launch"

My app was launching fine earlier today and now I'm getting this error
"Applications are expected to have a root view controller at the end of application launch"
I've looked at other threads saying to change my code, but I never changed any code to get to this point.
Delegate.h
#interface halo4AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>{
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
Delegate.m
#implementation halo4AppDelegate
#synthesize window;
#synthesize tabBarController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(3);
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
The xib for my FirstViewController is titles FirstView.xib , ext
This is not an error, more like a warning.
In your application delegate there is a method named application:didFinishLaunchingWithOptions: in this method you have to make this line before the end of the method self.window.rootViewController = [Some UIViewController]
again, this is not an error, you can ignore the rootViewController IF you have another way to create this rootViewController.
EDIT
This is what your method should looks like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

UITabBars and UITableViews. Keeping the Tab bar on screen when a table cell is clicked

I'm trying to get a GUI with a UITabBar and UITableViews set up.
I've got a UITabView that is programmatically created.
One of the Tabs displays a UITableView that is also programmatically created.
This UITableView then displays other views when didSelectRowAtIndexPath is called.
Unfortunately, when a table cell is clicked, my tab view goes away and the new table view is displayed.
What I can't get my head around is how to structure the views so that the tabBar stays on the screen.
Is it as simple as making the UITableViews shorter, or is there some window/view mojo that I'm missing?
Thanks
You should use a UITabBarController to display the UITabBar rather than doing it directly.
Then use a UITableViewController as the view controller for a given tab. Though I get the impression that you want to present descendent UITableViews when a row is selected. If this is the case, you ought to use a UINavigationController as the tab bar's view controller, and let it manage your UITableViewControllers.
Remember that on iOS you really need to use the view controller pattern - the frameworks take care of a lot of things for you under the hood.
Follow-up:
OK, the following straightforward implementation works just fine for me. Please ignore the many obvious issues with this code (beginning with the fact that I hacked it all together in the application delegate!); it's intended purely as a model for how your controllers should be glued together.
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navController;
#end
#implementation AppDelegate
#synthesize window = _window;
#synthesize navController = _navController;
- (void)dealloc
{
[_navController release];
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UITableViewController *rootTVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[[rootTVC tableView] setDelegate:self];
[[rootTVC tableView] setDataSource:self];
[rootTVC setTitle:#"Root Table"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootTVC];
[rootTVC release];
[navController setTitle:#"My Table"];
UIViewController *anotherViewController = [[UIViewController alloc] init];
[anotherViewController setTitle:#"Not the table"];
UITabBarController *tbc = [[UITabBarController alloc] init];
[tbc setViewControllers:[NSArray arrayWithObjects:navController, anotherViewController, nil]];
[self setNavController:navController];
[navController release];
[anotherViewController release];
[[self window] setRootViewController:tbc];
[tbc release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = #"foo";
UITableViewCell *cell = [[tableView dequeueReusableCellWithIdentifier:reuseIdentifier] retain];
if (! cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
[[cell textLabel] setText:[NSString stringWithFormat:#"Row %d", [indexPath row]]];
return [cell autorelease];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewController *newTVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[newTVC setTitle:[NSString stringWithFormat:#"Table %d", [indexPath row]]];
[[newTVC tableView] setDelegate:self];
[[newTVC tableView] setDataSource:self];
[[self navController] pushViewController:newTVC animated:YES];
}
#end
Use UITabBarController instead of UITabBar.

Resources