Question

This is the second time I am asking this question but I do feel a bit more experience and maybe could word it better this time.

I have an iPhone app that has 3 tabs, the delegate has a tabBarController and the 3 views that appear on the screen for each tab are controlled by "FirstViewController", "SecondViewController" and "ThirdViewController" respectively.

The second and third views are all sorted.

Right so here is my question. In the first tab I have 20 UIButton's that are all connected to their respective IBOutlet buttons and methods in the first view controller. At the moment I have each method just hiding all information on the screen so I could in theory unhide information such as a textbox when they are pressed but this gets very messy in the IB.

Essentially I would like each button, when pressed, to bring forward a new screen/view that has its own .xib file that I could put all information on including a back button (another UIButton) that when pressed destorys the current screen and returns back to tabA's original view.

How would this be done?

I did try creating a new class, a UIView class that comes with its own .xib. I then in one of the methods set 3 lines of code that basically setup the new view with .xib then add it as a subview. this worked fine and brought the new screen forward however I was unable to remove this specific subview to return to the original view.

Thank you for any help.

#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

And the .m file. Obviously pay no attention to the hidden elements in the hide and show methods. Obviously I dont want to use this method I wanna push and pop new screens. Keeping my tabs

#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

Of course I have a new .h and .m called E87view which has its own .xib file.

- (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;
}
Was it helpful?

Solution

You can embed FirstViewController in a UINavigationController and use:

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

and then use the back button that is created on the navigation bar or get rid of the navigation bar and create your own back button using:

[self.navigationController popViewControllerAnimated:YES];

This method would keep your tabs present at the bottom.

If you don't care about keeping your tabs present then you can use a modalViewController like so:

[self presentModalViewController:theModalController animated:YES];

I would then create a protocol in theModalController that would call back to the FirstViewController when it was completed. The FirstViewController would then use this code to dismiss the theModalController:

[self dismissModalViewControllerAnimated:YES];

So two ways to go. Use a UINavigationController if you want to keep your tabs and just push viewControllers onto the stack. If you don't care about keeping the tabs when you use these new viewControllers then you can use modalViewController above and they will take over the entire screen.

Hope this helps!

EDIT: This is how you can set up the navigation controller, You will need to #import the .h file for each of the viewControllers in your 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];

}

After that, FirstViewController will be embedded in a UINavigationController and can use the pop and push code that I used above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top