0

I have a tab controller with 2 tabs "My information" and "their Information", which is just a bunch of text boxes. I want a Navigation controller at the top so I can go back, but the back button won't show up... here is my code:

Button pressed to show Form:

-(IBAction)onIncidentAidFormPressed:(id)sender {
    FormController *aidForm = [[FormController alloc] initWithNibName:@"formController" bundle:nil];
    aidForm.managedObjectContext = self.managedObjectContext;
    [self.navigationController pushViewController:aidForm animated:YES];
}

In FormController.m - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil:

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    self.title = NSLocalizedString(@"Aid Form", @"Aid Form title");
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:@"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:@"TheirInformationController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithNibName:@"navController" bundle:nil];

[navController pushViewController:viewController1 animated:YES];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return self;

The tabs show up correctly, and the navigation bar is there but no back button is shown to get back.

Edit: Here are the files: IncidentAidStartViewController.h

#import <UIKit/UIKit.h>

@interface IncidentAidStartViewController : UIViewController {
    IBOutlet UIButton *incidentAidForm;
    IBOutlet UIButton *emergencyContacts;
}

-(IBAction)onIncidentAidFormPressed:(id)sender;
@property (strong, nonatomic) IncidentAidStartViewController *detailViewController;

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end

IncidentAidStartViewController.m

#import "IncidentAidStartViewController.h"
#import "IncidentAidForm.h"
@implementation IncidentAidStartViewController

@synthesize detailViewController = _detailViewController;
@synthesize managedObjectContext = __managedObjectContext;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Incident Aid", @"Incident Aid title");
    }
    return self;
}

#pragma mark - Button handlers
-(IBAction)onIncidentAidFormPressed:(id)sender { 
    IncidentAidForm *aidForm = [[IncidentAidForm alloc] initWithNibName:@"IncidentAidForm" bundle:nil]; 
    aidForm.managedObjectContext = self.managedObjectContext; 
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:nil]; 
    [self.navigationController pushViewController:aidForm animated:YES];

}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

IncidentAidForm.h

#import <UIKit/UIKit.h>

@interface IncidentAidForm : UIViewController <UITabBarControllerDelegate> {

}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UITabBarController *tabBarController;

@property (strong, nonatomic) IncidentAidForm *detailViewController;

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end

IncidentAidForm.m

#import "IncidentAidForm.h"
#import "MyInformationController.h"
#import "TheirInformationController.h"

@implementation IncidentAidForm

@synthesize detailViewController = _detailViewController;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Incident Aid Form", @"Incident Aid Form title");
    }
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:@"MyInformationController" bundle:nil];
    UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:@"TheirInformationController" bundle:nil];
    UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
    UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

MyInformationController.h

#import <UIKit/UIKit.h>

@interface MyInformationController : UIViewController {

    IBOutlet UIScrollView *scrollView;
    IBOutlet UITextField *firstName;
    IBOutlet UITextField *lastName;
    IBOutlet UITextField *phoneNumber;
    IBOutlet UITextField *address;
    IBOutlet UITextField *carMake;
    IBOutlet UITextField *carModel;
    IBOutlet UITextField *carYear;
    IBOutlet UITextField *licensePlate;
    IBOutlet UITextField *insuranceCarrier;
    IBOutlet UITextField *insurancePolicy;
    IBOutlet UIButton *backgroundButton;

}

@property(nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UITextField *firstName;
@property (nonatomic, retain) IBOutlet UITextField *lastName;
@property (nonatomic, retain) IBOutlet UITextField *phoneNumber;
@property (nonatomic, retain) IBOutlet UITextField *address;
@property (nonatomic, retain) IBOutlet UITextField *carMake;
@property (nonatomic, retain) IBOutlet UITextField *carModel;
@property (nonatomic, retain) IBOutlet UITextField *carYear;
@property (nonatomic, retain) IBOutlet UITextField *licensePlate;
@property (nonatomic, retain) IBOutlet UITextField *insuranceCarrier;
@property (nonatomic, retain) IBOutlet UITextField *insurancePolicy;

- (void) animateTextField: (UITextField*) textField up: (BOOL) up;
-(IBAction)hideKeyboard;
@end

MyInformationController.m

#import "MyInformationController.h"

@implementation MyInformationController

@synthesize scrollView, firstName, lastName, phoneNumber, address, carMake, carModel, carYear, licensePlate, 
insuranceCarrier, insurancePolicy;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"My Information", @"My Information");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];

    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;

    // Do any additional setup after loading the view, typically from a nib.
    firstName.delegate = self;
    lastName.delegate = self;
    phoneNumber.delegate = self;
    address.delegate = self;
    carMake.delegate = self;
    carModel.delegate = self;
    carYear.delegate = self;
    licensePlate.delegate = self;
    insuranceCarrier.delegate = self;
    insurancePolicy.delegate = self;

    [scrollView setContentSize:self.view.frame.size];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //[self animateTextField: textField up: YES];
    scrollView.frame = CGRectMake(0,44,320,200); //44:NavigationBar ; 200: Keyoard
    [scrollView scrollRectToVisible:textField.frame animated:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //[self animateTextField: textField up: NO];
    if (textField.tag == 10) {
        scrollView.frame = CGRectMake(0,44,320,416); //original setup
        // [textField resignFirstResponder];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    switch (textField.tag) {
        case 1:
            [lastName becomeFirstResponder];
            break;
        case 2:
            [phoneNumber becomeFirstResponder];
            break;
        case 3:
            [address becomeFirstResponder];
            break;
        case 4:
            [carMake becomeFirstResponder];
            break;
        case 5:
            [carModel becomeFirstResponder];
            break;
        case 6:
            [carYear becomeFirstResponder];
            break;
        case 7:
            [licensePlate becomeFirstResponder];
            break;
        case 8:
            [insuranceCarrier becomeFirstResponder];
            break;
        case 9:
            [insurancePolicy becomeFirstResponder];            
            break;
        case 10:
            [textField resignFirstResponder];
            break;
        default:
            break;
    }

    return TRUE;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (IBAction)hideKeyboard {
    [self.firstName resignFirstResponder];
    [self.lastName resignFirstResponder];
    [self.phoneNumber resignFirstResponder];
    [self.address resignFirstResponder];
    [self.carMake resignFirstResponder];
    [self.carModel resignFirstResponder];
    [self.carYear resignFirstResponder];
    [self.licensePlate resignFirstResponder];
    [self.insurancePolicy resignFirstResponder];
    [self.insuranceCarrier resignFirstResponder];
    scrollView.frame = CGRectMake(0,44,320,416); //original setup

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
user1154920
  • 465
  • 1
  • 6
  • 21
  • 1
    you can find my answer here http://stackoverflow.com/questions/10229805/i-want-to-use-both-tab-bar-and-navigation-bar-in-same-view-controller/10229914#10229914 – Charan Apr 20 '12 at 14:39
  • post some code of those view controller files that ur pushing & on which u want to show back button. eg. when tap some button (may be backgroundButton I guess)on MyInformationController tab; u'll see a new view. Post code of that "new view" file on which u want to insert back button. – hp iOS Coder Apr 20 '12 at 18:51
  • I got it to work by adding the navigation controller to the tab controller! Thanks for the help – user1154920 Apr 20 '12 at 21:01

3 Answers3

1

So far I know this is a good way to add ur views to tabbar. So try this

UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:@"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:@"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];

To add back bar item , below ine is also used.

self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
hp iOS Coder
  • 3,230
  • 2
  • 23
  • 39
  • I did not see your answer before I wrote. You should know that when any one giving answer that time browser is not going to refresh. then how can i see your answer.?? and you decrease my reputation why??????? – sandy Apr 20 '12 at 15:04
  • may be ur internet is not fast.. "Dont forget that this site is very fast & gives udates every few seconds". While u type answer & someone has posted before u finish, then it gives u notification with orange flash that someone has posted the answer. Even if u edit the post u'll get the same flash effect notifying about the change.! – hp iOS Coder Apr 20 '12 at 15:09
  • downvote reason is mentioned in comment -> mistake in "initWithRootViewController". It shuldnt be a NSSting, but viewContrller instance – hp iOS Coder Apr 20 '12 at 15:11
  • that's ok but this mean not that I copied your code before then why u decrease my reputation?/???? – sandy Apr 20 '12 at 15:11
  • @sandy :editing ur own answer after my comment.. & downvoting my answer for the sake of revenge wont succeed u in life.. we are here to help others by giving correct answers not wrong answers.! Be sporty man – hp iOS Coder Apr 20 '12 at 15:24
  • The back button still did not appear... where should i be putting self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; ? – user1154920 Apr 20 '12 at 15:27
  • put that line in view-controllers "viewDidLoad" method. Like in ur code after u tap eg. some button on MyInformationController tab; u'll see a new view, in that view put this line – hp iOS Coder Apr 20 '12 at 15:31
  • As answered by 'Neel'; Even this line may help if u put it in viewDidLoad.. see for anyone of this that works for u.. self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:nil]; – hp iOS Coder Apr 20 '12 at 15:35
  • I added it there and it still is not showing up :( I also added neel's line but still no back button... – user1154920 Apr 20 '12 at 15:35
  • some of this should work... as it is common way to do.. if it is not working then.. can u pls share the code in zip file..if u are allowed to do so.. i may help u then – hp iOS Coder Apr 20 '12 at 15:37
  • I cant zip everything, but i can send you the 4 files that are being used .. what is the best way to send them? Or should I just add an edit to the end of my code and copy it there? – user1154920 Apr 20 '12 at 15:42
  • exactly .. u just edit ur answer & include those 4 files at the bottom of ur code..while editing better u delete ur code lines – hp iOS Coder Apr 20 '12 at 15:43
0

Please modify your method like this

-(IBAction)onIncidentAidFormPressed:(id)sender
 {
    FormController *aidForm = [[FormController alloc] initWithNibName:@"formController" bundle:nil];
    aidForm.managedObjectContext = self.managedObjectContext;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:nil];
    [self.navigationController pushViewController:aidForm animated:YES];

}
hp iOS Coder
  • 3,230
  • 2
  • 23
  • 39
Nilesh Kikani
  • 2,628
  • 21
  • 37
-1

you should replace your code with the following:

UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:@"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:@"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
UINavigationController *navController2 = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];

let me know any thing else you need........

sandy
  • 1,908
  • 3
  • 19
  • 23
  • hey whats this.. u have copied my answer. I have answered it before u... &BTW "initWithRootViewController:" u have wrote a NSString.. so downvote for that – hp iOS Coder Apr 20 '12 at 15:02
  • editing ur own answer after my comment.. & downvoting my answer for the sake of revenge wont succeed u in life.. we are here to help others by giving correct answers not wrong answers.! Be sporty man – hp iOS Coder Apr 20 '12 at 15:23