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];
}
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
In Xcode project, Select SubViewController.xib
Select your button
Xcode > View > Utilities > Show Connections Inspector
Xcode > View > Assistant Editor > Show Assistant Editor
Ensure that Automatic > SubViewController.m > No Selection is present. If not, you may not have the Custom Class set properly in IB
In Connections, click Touch Up Inside grommet and drag onto Assistant, right below #implementation SubViewController
Enter name, such as buttonTappedAction
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!
Related
I am new to IOS development . i got a open source project and loaded in xcode and it is running good.
the project is based on webview and loading url.
what i want to do is add a new screen at the launch of application which takes three inputs from textfields and on submit of button it should send the data to next screen which is a webview and currently it is being loaded first .
1) how can i add new view controller which launches first at launch of app where i can input textfields and click on button to move to second screen
2) get the data from first screen and give to the load url to load the url.
i have taken project files from here :
https://github.com/paulirish/iOS-WebView-App
viewcontroller option on right side doesnt have the option is initial view
i am using xcode 6.2
I think you are loading viewcontroller from code like we did in older versions. I hope this helps you.
In this code you can simply change MainViewController to your own view.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MainViewController* viewC =[[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:viewC];
self.window.rootViewController = navC;
[self.window makeKeyAndVisible];
You will have to create new UIViewController with your three textFields and then set this UIViewController as Initial View Controller from storyboard like this:
After doing this get data from your textfields like:
NSString *text1Data = self.yourtextField.text;//Repeat for all text fields.
After getting data make three variables in your webViewController and assign thse values to them.
Suppose WebVC is yoour WebViewController then you can pass data and go to web viw like this:
WebVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"your_WebVc_identifier"];
vc.text1DataFromFirstVC = text1Data; //repeat for all three variables
//go to web view controller
[self.navigationController pushViewController: vc animated:YES]
EDIT:For Non Storyboard use following code from your AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *Vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = Vc;
[self.window makeKeyAndVisible];
return YES;
}
I have been playing around with this for a couple of days and I cannot figure this out.
-> Basically I want to implement a simple login view that has a button when clicked, goes to go to the navigation controller ( in my case is "viewController" with buttons that link to mini math games which are other views).
-> Login screen should be displayed first, than navigation controller's root view when a button is clicked on the login screen
-> I have tried to declare the navigation controller when I click the button of the login screen but that seems to not work
-> Is it safe to say that a navigation controller can only be initialized in the apple delegate?
Currently I have this in my apple delegate declaring and setting my navigational controller:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; // self.viewController is the main screen
self.window.rootViewController = navigationViewController; // set root to navigationViewController
[self.window makeKeyAndVisible];
return YES;
}
Any ideas will be appreciated. Thank you for your time !
Your code in the app delegate looks ok. NavigationController does not need to be declared in the AppDelegate. In your case, it is definitely ok to declare it upon login button pressed.
Try this at the login event:
UIViewController *nextVC = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:nextVC];
[self presentViewController:navController animated:YES completion:^{
}];
What I would do would be set the loginViewController as the rootViewController of the navigation. And after check if login was done successfully, you could implement [self performSegueWithIdentifier:#"identifier"] setting the game viewController as destination. (Using Storyboard would make your life much easier). Well, that's my opinion :)
I wanted to know how to set up the app delegate in Xcode 5 since it's different than it was in previous versions. I want the generic view controller files (ViewController.h and .m) to be the files that control the rootViewController I set in my app delegate. Does this happen automatically or do you need to do something in the code? This is how I set up my appDelegate.m
*(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect viewRect = [[UIScreen mainScreen]bounds];
self.window = [[UIWindow alloc]initWithFrame:viewRect];
UIViewController *viewController = [[UIViewController alloc]init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
If I wanted my rootView Controller to be a table view controller or something else, would I need to embed it in a basic VC first?
To do it programmatically you can set it to be a UITableViewController since it is a subclass of UIViewController. If you want to use the already create ViewController just change the subclass in the .h file from UIViewController to UITableViewController and add the tableview delegates and datasources into the .m.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *mainViewController = [storyboard instantiateInitialViewControllerWithIdentifier:#"myViewController"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
or if you want to just draw the view in the view controller create a class and do this
MyViewController *viewController = [[MyViewController alloc]init];
self.window.rootViewController = viewController;
If you are wanting to set your root view via storyboards you can just check the __Is Initial view controller` option
If I wanted my rootView Controller to be a table view controller or something else, would I need to embed it in a basic VC first?
No.
You can set any UIViewController class to be the rootViewController. I am assuming you are building your viewController hierarchy in code, and you are not using InterfaceBuidler. Although also with Interface builder, you can use any viewContorller as the rootviewcontroller, either using storyboards or plain xibs.
Hi Fellow iOS Developers, I am a newbie developing a project with 5 tab Views and on the first and second tabs I have slide out menus using Container views from example code by Michael Frederick on his GitHub page Project Link: https://github.com/mikefrederick/MFSideMenu. He is using a nib (.xib) files though I am using Storyboard to achieve the same and struck with defining the container and child views. can kindly some one advice how to modify the below code to accommodate in my storyboard.
the original code in the AppDelegate.m is
- (DemoViewController *)demoController {
return [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
}
- (UINavigationController *)navigationController {
return [[UINavigationController alloc]
initWithRootViewController:[self demoController]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:[self navigationController],
[self navigationController], nil]];
SideMenuViewController *leftSideMenuController = [[SideMenuViewController alloc] init];
SideMenuViewController *rightSideMenuController = [[SideMenuViewController alloc] init];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:tabBarController
leftMenuViewController:leftSideMenuController
rightMenuViewController:rightSideMenuController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}
#end
how to modify the code to accommodate the container parent view and child views ?
where should i instantiate the code for the parent and child of the 2nd tab view ? in AppDelegate or the View Controller ?
If any other Details are required leave a comment please. Any Help Will be greatly appreciated. thanks in Advance.
I don't know if you still need this, but i had the exactly same problem today, too. What you need to do is:
remove the both methods over your app Delegate
put this in your app Delegate:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YOUR_STORYBOARD" bundle:[NSBundle mainBundle]];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"THE_IDENTITY_OF_YOUR_SIDEMENU"];
UITabBarController *centerViewController = [storyboard instantiateViewControllerWithIdentifier:#"IDENTITY_OF_YOUR_TABBARCONTROLLER"];
[container setCenterViewController:centerViewController];
[container setLeftMenuViewController:leftSideMenuViewController]; //for the right Side, its the same way...
[container setPanMode:MFSideMenuPanModeNone]; //remove this line, if you need the pan mode
return YES;
In your Storyboard you have to put a ViewController as a subclass from "MFSideMenuContainerViewController". Mark this View as the "Initial View Controller" in the Attribute Inspector. Now use a Segue from your new Initial View Controller and let it "push" to your TabBarController. To avoid a Warning rename the Segue.
After you have done this, you can add a UIBarButtonItem to every View, you like to add the SideMenu. In the Action Method of this UIBarButtomItem you only need to do this:
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{}];
finally make sure you have a UIViewController or a UITableViewController, that is your "SideMenu" and set the right Storyboard ID.
if you are still need help, comment this...
and sorry for my english :)
You can use https://github.com/ozcanakbulut/VoovilSideMenu. It's easy to embed in a tabBarController. It uses Storyboard and Arc.
I am doing a simple example of tab bar based application for iPad. I have got a controller responsible to tabs management:
#interface MainTabBarController : UITabBarController
and some view controller passed to MainTabBarController as below:
#interface WorkspaceViewController : UIViewController
//
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.tabBarRootController = [[MainTabBarController alloc] initWithNibName:#"MainTabBarController" bundle:nil];
UIViewController *workspaceController = [[[WorkspaceViewController alloc] initWithNibName:#"WorkspaceViewController" bundle:nil] autorelease];
self.tabBarRootController.viewControllers = [NSArray arrayWithObjects:workspaceController, nil];
self.window.rootViewController = self.tabBarRootController;
[self.window makeKeyAndVisible];
[workspaceController release];
return YES;
}
The WorkspaceViewController has it own xib file. Using this XIB file I'm adding some controls to the Workspace view and everything is fine. But when I want to add (drag and drop in xib file) a toolbar it does not appear when app it run.
This is how my WorkspaceViewController XIB file looks like:
Toolbar is connected with controller via outlet (using xib).
Could you tell what could be a reason and how to solve it? My toolbar has to be visible only when workspace view is visible and it should iteract only with workspace (workspace will be responsible for some drawing stuff and the toolbar will have options like cut, paste, copy etc.)
You are releasing the workspaceController again after calling autorelease.