2012-04-20 130 views
0

我有一個選項卡控制器與2個選項卡「我的信息」和「他們的信息」,這只是一堆文本框。我想在頂部導航控制器,這樣我就可以回去,但後退按鈕不會顯示出來...這裏是我的代碼:導航控制器在選項卡控制器不顯示返回按鈕

按下按鈕顯示形式:

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

在的FormController .M - (ID)initWithNibName:(的NSString *)nibNameOrNil包(一個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; 

的標籤正確顯示和導航欄是有,但沒有後退按鈕顯示取回。

編輯:這裏有文件: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 
+1

你可以在這裏找到我的答案http://stackoverflow.com/questions/10229805/i-want-to-use-both-tab-bar-and-navigation-bar-in-same-view-控制器/ 10229914#10229914 – Charan 2012-04-20 14:39:50

+0

發佈你想要顯示的按鈕上的那些視圖控制器文件的一些代碼。例如。當在MyInformationController選項卡上點擊一些按鈕(可能是backgroundButton,我猜);你會看到一個新的觀點。發佈你想要插入返回按鈕的「新視圖」文件的代碼。 – 2012-04-20 18:51:26

+0

我通過將導航控制器添加到選項卡控制器來實現它的功能!感謝您的幫助 – user1154920 2012-04-20 21:01:24

回答

1

到目前爲止,我知道這是增加的好方法你們六ews to tabbar。所以試試這個

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

要添加回欄項目,下面的ine也被使用。

self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; 
+0

我在寫信之前沒有看到您的答案。你應該知道,當有人給出答案時,瀏覽器不會刷新。那我怎麼看到你的答案?你爲什麼降低我的聲譽??????? – sandy 2012-04-20 15:04:56

+0

可能是你的互聯網不是很快..「不要忘了,這個網站是非常快速的,每隔幾秒給予udates」。在你完成之前,當你鍵入答案並且某人已經發布時,那麼它會通過橙色閃光提醒你有人發佈了答案。即使你編輯帖子你會得到相同的閃光效果通知有關變化。 – 2012-04-20 15:09:41

+0

downvote原因在評論 - >「initWithRootViewController」中出現錯誤。它是一個NSSting,但viewContrller實例 – 2012-04-20 15:11:53

-1

應替換爲以下代碼:

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

讓我知道任何別的東西,你需要........

+0

哎什麼的.. u必須抄我的答案我已經回答了這個問題...&BTW「initWithRootViewController:」 u必須寫一個NSString的..所以downvote爲 – 2012-04-20 15:02:15

+0

編輯自己的烏爾在我的c後回答爲了報仇而降低我的答案,並且在生命中不會成功。我們在這裏通過給予正確的答案來幫助別人而不是錯誤的答案。是運動的人 – 2012-04-20 15:23:08

0

請修改你的方法是這樣

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

} 
+0

後退按鈕仍然沒有出現這個:( – user1154920 2012-04-20 15:29:10