2012-07-23 11 views
0

這是我第二次問這個問題,但我確實感覺有點更多的經驗,也許這次可以說得更好。TabView應用程序,推動和彈出選項卡內的意見

我有一個iPhone應用程序有3個選項卡,代表有一個tabBarController和3個視圖出現在屏幕上的每個選項卡分別由「FirstViewController」,「SecondViewController」和「ThirdViewController」控制。

第二個和第三個視圖全部排序。

這就是我的問題。在第一個選項卡中,我有20個UIButton,都連接到它們各自的IBOutlet按鈕和第一個視圖控制器中的方法。目前,我有各種方法只是隱藏屏幕上的所有信息,所以我理論上可以在按下時隱藏信息,如文本框,但這在IB中非常混亂。

本質上我想每個按鈕,當按下時,提出一個新的屏幕/視圖,它有自己的.xib文件,我可以把所有信息包括一個後退按鈕(另一個UIButton)屏幕並返回到tabA的原始視圖。

這將如何完成?

我嘗試創建一個新的類,一個自帶.xib的UIView類。然後我在其中一個方法中設置了3行代碼,基本上用.xib設置新視圖,然後將其添加爲子視圖。這工作得很好,並帶來了新的屏幕,但我無法刪除此特定的子視圖返回到原始視圖。

謝謝你的幫助。

#import <UIKit/UIKit.h> 
#import "E87view.h" 

@interface FirstViewController : UIViewController { 

IBOutlet UIButton *E87, *E81, *F20, *E36, *E46, *E90, 
        *E34, *E39, *E60, *E63, *E64, *E38, 
*E65, *F01, *X3, *X5, *Z3, *Z4, *M3, *M5, *backButton; 

IBOutlet UILabel *ones, *threes, *fives, *sixs, *sevens, *xs, *zs, *ms; 

IBOutlet UITextView *mainText; 

} 

-(IBAction) E87Pressed; 
-(IBAction) E81Pressed; 
-(IBAction) F20Pressed; 
-(IBAction) E36Pressed; 
-(IBAction) E46Pressed; 
-(IBAction) E90Pressed; 
-(IBAction) E34Pressed; 
-(IBAction) E39Pressed; 
-(IBAction) E60Pressed; 
-(IBAction) E63Pressed; 
-(IBAction) E64Pressed; 
-(IBAction) E38Pressed; 
-(IBAction) E65Pressed; 
-(IBAction) F01Pressed; 
-(IBAction) X3Pressed; 
-(IBAction) X5Pressed; 
-(IBAction) Z3Pressed; 
-(IBAction) Z4Pressed; 
-(IBAction) M3Pressed; 
-(IBAction) M5Pressed; 
-(IBAction) backButtonPressed; 

-(void) hideAll; 
-(void) showAll; 

@end 

而.m文件。顯然,不要關注隱藏和顯示方法中的隱藏元素。顯然,我不想使用這種方法,我想推動和彈出新的屏幕。保持我的標籤

#import "FirstViewController.h" 
#import "E87view.h" 


@implementation FirstViewController 


-(void) hideAll{ 
E87.hidden = 1; 
E81.hidden = 1; 
F20.hidden = 1; 
E36.hidden = 1; 
E46.hidden = 1; 
E90.hidden = 1; 
E34.hidden = 1; 
E39.hidden = 1; 
E60.hidden = 1; 
E63.hidden = 1; 
E64.hidden = 1; 
E38.hidden = 1; 
E65.hidden = 1; 
F01.hidden = 1; 
X3.hidden = 1; 
X5.hidden = 1; 
Z3.hidden = 1; 
Z4.hidden = 1; 
M3.hidden = 1; 
M5.hidden = 1; 
ones.hidden = 1; 
threes.hidden = 1; 
fives.hidden = 1; 
sixs.hidden = 1; 
sevens.hidden = 1; 
xs.hidden = 1; 
zs.hidden = 1; 
ms.hidden = 1; 
mainText.hidden = 1; 
backButton.hidden = 0; 
} 

-(void) showAll{ 
E87.hidden = 0; 
E81.hidden = 0; 
F20.hidden = 0; 
E36.hidden = 0; 
E46.hidden = 0; 
E90.hidden = 0; 
E34.hidden = 0; 
E39.hidden = 0; 
E60.hidden = 0; 
E63.hidden = 0; 
E64.hidden = 0; 
E38.hidden = 0; 
E65.hidden = 0; 
F01.hidden = 0; 
X3.hidden = 0; 
X5.hidden = 0; 
Z3.hidden = 0; 
Z4.hidden = 0; 
M3.hidden = 0; 
M5.hidden = 0; 
ones.hidden = 0; 
threes.hidden = 0; 
fives.hidden = 0; 
sixs.hidden = 0; 
sevens.hidden = 0; 
xs.hidden = 0; 
zs.hidden = 0; 
ms.hidden = 0; 
mainText.hidden = 0; 
} 

-(IBAction) backButtonPressed{ 
[self showAll]; 
backButton.hidden = 1; 
//[self.navigationController popToRootViewControllerAnimated:YES]; 

//for (UIView *subview in [self.view subviews]) { 

// [subview removeFromSuperview]; 
//} 
//[self showAll]; 


} 

-(IBAction) E87Pressed{ 
[self hideAll]; 
//E87view *e87view = [[E87view alloc] initWithNibName:@"E87view" bundle:[NSBundle mainBundle]]; 
//[self.navigationController pushViewController:e87view animated:YES]; 
//[self.view addSubview:e87view.view]; 
} 


// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
    [self showAll]; 
} 
[self showAll]; 
return self; 
} 



// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 

[self showAll]; 
} 



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
[super viewDidLoad]; 

//backButton.hidden = 1; 
[self showAll]; 
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BackdropService.png"]]; 
backButton.hidden = 1; 


} 


/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  { 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (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. 
} 

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


- (void)dealloc { 
[super dealloc]; 
} 

@end 

當然,我有一個新的.h和.m叫E87view它有自己的.xib文件。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions {  

// Override point for customization after application launch. 

// Add the tab bar controller's view to the window and display. 
[self.window addSubview:tabBarController.view]; 
[self.window makeKeyAndVisible]; 


return YES; 
} 

回答

0

您可以在UINavigationController和使用嵌入FirstViewController

[self.navigationController pushViewController:theButtonController animated:YES]; 

,然後使用在導航欄上創建或擺脫了導航欄,並創建自己的後退按鈕返回按鈕使用:

[self.navigationController popViewControllerAnimated:YES]; 

此方法將使您的選項卡保持在底部。

如果你不關心讓你的標籤存在,那麼您可以使用modalViewController像這樣:

[self presentModalViewController:theModalController animated:YES]; 

然後,我會創建一個theModalController協議,將回調到FirstViewController它完成時。然後FirstViewController將使用此代碼以關閉theModalController

[self dismissModalViewControllerAnimated:YES]; 

所以兩條路走。如果您想保留標籤並將viewController推入堆棧,請使用UINavigationController。如果您在使用這些新的viewController時不關心保留標籤,那麼您可以使用上面的modalViewController,它們將接管整個屏幕。

希望這會有所幫助!

編輯:這是你如何設置導航控制器,您將需要#import.h文件爲每個viewControllers在你的AppDelegate.h

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions {  

    self.tabBarController = [[[UITabBarController alloc] init]; 

    FirstViewController* vc1 = [[FirstViewController alloc] init]; 
    SecondViewController* vc2 = [[SecondViewController alloc] init]; 
    ThirdViewController* vc3 = [[ThirdViewController alloc] init]; 

    UINavigationController* navController = [[UINavigationController alloc] 
          initWithRootViewController:vc1]; 

    NSArray* controllers = [NSArray arrayWithObjects:navController, vc2, vc3, nil]; 
    tabBarController.viewControllers = controllers; 

    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    window.rootViewController = tabBarController; 
    [window makeKeyAndVisible]; 

} 

之後,FirstViewController將嵌入UINavigationController並可以使用上面我使用的pop和push代碼。

+0

你以爲今晚晚些時候可以和我一起上Skype嗎?在說,2小時的時間? 不會讓我們長久,我更多的是一個「猴子看,猴子做」類型的學習者。例如,我不知道你的意思是將FirstViewController嵌入到UINavigationController中。你的意思是在接口文件中加入有點像繼承? – 2012-07-23 15:40:46

+0

我將無法使用Skype,但您是否偶然使用故事板? – 2012-07-23 15:51:50

+0

不,我正在使用Xcode 3.2哈哈。我不是買獅子,因爲你知道,這不是我的MAC。但是我已經更新了我的帖子以包含代碼,因此如果您想重新發布包含您的解決方案的代碼,那將是非常棒的。 – 2012-07-23 16:00:35

相關問題