这是我第二次提出这个问题,但我确实感受到了更多的经验,也许这次可能会更好地说出来。
我有一个有3个标签的iPhone应用程序,委托有一个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
当然我有一个名为E87view的新的.h和.m,它有自己的.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 :(得分:0)
您可以在FirstViewController
中嵌入UINavigationController
并使用:
[self.navigationController pushViewController:theButtonController animated:YES];
然后使用导航栏上创建的后退按钮或删除导航栏并使用以下命令创建自己的后退按钮:
[self.navigationController popViewControllerAnimated:YES];
此方法会使您的标签显示在底部。
如果您不关心如何保留自己的标签,那么您可以像modalViewController
这样使用:
[self presentModalViewController:theModalController animated:YES];
然后我会在theModalController
中创建一个协议,该协议会在FirstViewController
完成时回调。然后,FirstViewController
将使用此代码关闭theModalController
:
[self dismissModalViewControllerAnimated:YES];
还有两种方法。如果要保留标签,请使用UINavigationController
,然后将viewController
推入堆栈。如果您在使用这些新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代码。