Load a tabbarcontroller when back button is clicked in xcode? - iphone

When i click on back button then Im loading a particular view.But to that particular view I have to add a tabbarController.How can i do it..?When i try to add it doesnot get added..Couldnot understand y ?
-(IBAction)switchtofirst {
AppViewController *first=[[AppViewController alloc] initWithNibName:#"AppViewController" bundle:nil];
Login *second=[[Login alloc]initWithNibName:#"Login" bundle:nil];
second.title=#"Login";
NSArray *viewArray=[[NSArray alloc] initWithObjects: first,second,nil];
tabBarController=[[UITabBarController alloc] init];
[tabBarController setViewControllers:viewArray animated:NO];
AppViewController *gp=[AppViewController alloc];
[gp.view addSubview:tabBarController.view];
[self presentModalViewController:gp animated:NO];
[gp release];
}

Try something like this:
-(IBAction)switchtofirst {
AppViewController *first = [[AppViewController alloc] initWithNibName:#"AppViewController" bundle:nil];
Login *second=[[Login alloc]initWithNibName:#"Login" bundle:nil];
second.title=#"Login";
NSArray *viewArray= [NSArray arrayWithObjects:first, second, nil];
tabBarController=[[UITabBarController alloc] init];
[tabBarController setViewControllers:viewArray animated:NO];
[self presentViewController:tabBarController animated:YES completion:nil];
}

Related

UISwipeGestureRecognizer making app crash changing screens?

I have a simple 3 page app, I can go from page 1 to page 2 swiping left.
However, when I am on page 2, I need to add the ability to go to either page 1 swiping right, or page 3 swiping left, but it just keeps crashing.
I suspect it may be a memory release issue, but I wonder if you could be so kind as to check the code below for any glaring errors?
Many thanks,
- (void)viewDidLoad {
UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
[self.view addGestureRecognizer:swipeLeftRight];
[UISwipeGestureRecognizer release];
}
- (IBAction)swipeLeftDetected:(UISwipeGestureRecognizer *)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController~ipad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}else{
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}
}
- (IBAction)swipeRightDetected:(UISwipeGestureRecognizer *)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}else{
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
ViewController *VC = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentModalViewController:VC animated:YES];
[Page2ViewController release];
}
}
EDIT:
You need to implement a method called handleGesture: since that's the action you're passing in to the gesture recognizer.
Fixed, I found an older post by a person called rptwsthi, I used some of that to fix it;
Changed the viewDidLoad {
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
//........towards left Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
And using this to switch the pages;
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do moving
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}else{
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
ViewController *VC = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentModalViewController:VC animated:YES];
[Page2ViewController release];
}
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
// do moving
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController~ipad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
}else{
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
}
}
Not sure how neat it is in a true coders view, but it works, sometimes you just gotta go with what works right! :)

how to add a NavigationController to a UIViewController?

I need to add a UINavigationController to a UIViewController,and don't use appDelegate.In my Controller I write like this
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:nil bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVC];
[self presendModalViewController:nav];
and it nothing to found.
It should be
[self presentModalViewController: nav animated:YES];
For more info, please visit UIViewController documentation
Try this :
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:#"OneViewControlelr" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVC];
[self presentModalViewController: nav animated:YES];
if you dnt want navigationcontroller just try this
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:#"xibname" bundle:nil];
[self presendModalViewController:oneVC animated:YES];
[oneVC release];
if u want navigationController try this code
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:#"xibname" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:oneVC];
[self presendModalViewController:navigationController animated:YES];
[oneVC release];
[navigationController release];
Just do like this in AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
SoapWebServiceViewController *root = [[SoapWebServiceViewController alloc] initWithNibName:nil bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:root];
// Add the view controller's view to the window and display.
[self.window addSubview:nav.view];
}
Hope this help U
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:nil bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVC];
AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
[[[appDelegate <viewController>]view] removeFromSuperview]; // put UIViewController reference into <viewController>
[[appDelegate window]addSubview:[[appDelegate nav]view]];

how to release a tabbarcontroller?

A UITabBarController's viewControllers are navigationController, when I release the tabbarcontroller, I found the memory would not released ?
You cant release them if you create them in .xib file. They will be released then your application end work !!!
If you are working in code you probably need to release the navigationControllers after adding them to the tabBarController...
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *controllerArray = [[NSMutableArray alloc] initWithCapacity:2];
UINavigationController *localNavigationController;
AccountViewController *accountViewController = [[AccountViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:accountViewController];
[controllerArray addObject:localNavigationController];
[localNavigationController release];
[accountViewController release];
AccountHistoryViewController *accountHistoryViewController = [[AccountHistoryViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:accountHistoryViewController];
[controllerArray addObject:localNavigationController];
[localNavigationController release];
[accountHistoryViewController release];
[tabBarController setViewControllers:controllerArray];
[controllerArray release];
you should release them in dealloc method of app delegate class
- (void)dealloc {
[tabbarcontroller release];
[window release];
[super dealloc];
}

How to add navigation controller to windowbased app

I have a window based app. I added two viewcontrollers and one Tabbarcontroller to that. Now I want to navigate each viewcontroller to next view. I tried but can't find the solution. Can anyone please help me?
When you add the tab bar on window, add like this-
NSMutableArray * viewControllers = [[NSMutableArray alloc]init];
FirstViewController * firstViewController = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController * nvc = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[firstViewController release];
[viewControllers addObject:nvc];
[nvc release];
SecondViewController * secondViewController = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
nvc = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[secondViewController release];
[viewControllers addObject:nvc];
[nvc release];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = viewControllers;
[window addSubview:tabBarController.view];
EDIT -
Whenever you want to navigate in any one of the controller. All you need is to call
[self.navigationController pushViewController:anotherViewController animated:YES];
And you will get exactly what you want. :)
UINavigationController *urNavController = [[UINavigationController alloc] initWithRootViewController:rootController];
If you want to show this on the window
[window addSubview:urNavController.view];
use like this
secondViewController *obj=[[[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil] autorelease];
UINavigationController *navBar=[[UINavigationController alloc] initWithRootViewController:obj];
[self.navigationController pushViewController:navBar animated:YES];
[navBar release];

Navigation Items in UITableViewController are not appearing?

I am displaying a UITableViewController inside of a UITabBarController that is being presented modally:
-(IBAction)arButtonClicked:(id)sender{
//this is a uitableviewcontroller
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
arTabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arViewController, lbViewController, nil];
arTabBarController.selectedViewController = arViewController;
[self presentModalViewController:arTabBarController animated:YES];
}
In my viewDidLoad for arViewController method I am setting the navigation items:
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
self.clearsSelectionOnViewWillAppear = NO;
self.title = #"AR";
leaderBoardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:#selector(leaderBoardButtonClicked:)];
self.navigationItem.rightBarButtonItem = leaderBoardButton;
}
My navigation bar doesn't appear when it is inside of the UITabBarController, but when I push the view itself I am able to see it.
What am I missing?
Heh, I've been stumped by this too. What you need to do is send the rootViewController.
I've never used a tabBar for anything except on the main screen but ur code will probably look like this:
after arTabBarController.selectedViewController = arViewController;
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController: arTabBarController] autorelease];
[self presentModalViewController: navController animated:YES];
Like I said I haven't done it with a tabBar but I'm pretty sure it will be something along these lines
I needed to add a UINavigationBar:
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
UINavigationController *arNavController = [[UINavigationController alloc] initWithRootViewController:arViewController];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
UINavigationController *lbNavController = [[UINavigationController alloc] initWithRootViewController:lbViewController];
arTabBarController = [[UITabBarController alloc] init];//initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arNavController, lbNavController, nil];
arTabBarController.selectedViewController = arNavController;
[self presentModalViewController:arTabBarController animated:YES];
There is a simple solution, put setting in view will appear
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
hope it help some newbies;