0
votes

I am trying to make a button on a navigation controller that causes a transition to another view controller (that has a back button to the previous nav view). I'm trying to do this programmatically. I made a UINavigationController and a UITabBarController. The nav controller is the opening tab of the tab controller. I made a subclass of the UIViewController called SubVieController. I haven't added anything to the Sub classes, only the auto generated material. I've made all my edits to the didFInishLaunchingWithOptions method in Appdelegate.m.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SubViewController * firstTab= [[SubViewController alloc] initWithNibName:@"SubViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];
SecondViewController *secondTab = [[SecondViewController alloc] initWithNibName:@"SecondViewController"    bundle:nil];

SubTableController *table = [[SubTableController alloc]initWithNibName:@"SubTableController" bundle:nil];
navigationController1.tabBarItem.title = @"First Nav";
secondTab.tabBarItem.title =@"Second";
table.title = @"Third Table";
tabBarController.viewControllers = @[navigationController1,secondTab, table];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}

And I try to move a Button onto the SubViewController.xib file to connect it into the code so I can make a button action. But when I ctrl+drag, nothing inserts into the code like usual.

What do I do to have the button cause a transition from the starting view to a separate view that is still considered to be on the 1st tab, with a back button to the first view which is also in the first tab?

EDIT: This code seems to work for the button action: - (IBAction)transition:(UIButton *)sender{ SubViewController * view2 = [[SubViewController alloc]init]; [self.navigationController pushViewController:view2 animated:YES]; }

1

1 Answers

0
votes

As you have correctly done, the UITabBarController should not push a UIViewController but a UINavigationController. That UINavigationController should have a rootViewController.

Not being able to Control-Drag in Interface Builder generally means that the Custom Class in the Identity Inspector is not set properly.

Connecting IBAction

  1. In Xcode project, Select SubViewController.xib
  2. Select your button
  3. Xcode > View > Utilities > Show Connections Inspector
  4. Xcode > View > Assistant Editor > Show Assistant Editor
  5. Ensure that Automatic > SubViewController.m > No Selection is present. If not, you may not have the Custom Class set properly in IB
  6. In Connections, click Touch Up Inside grommet and drag onto Assistant, right below @implementation SubViewController
  7. Enter name, such as buttonTappedAction
  8. Add code to push the UIViewController as needed:

Obj-C

UIViewController * vc = [[UIViewController alloc] initWithNibName:@"id" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];

Swift

let vc = UIViewController(nibName: "id", bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)

Storyboard

Architecture suggestion: Start over using Storyboard

There is no need to write a single line of code to implement the above solution using a Storyboard!

enter image description here