I want to change view by clicking button with NavigationController
So I added a button to Main.storyboard and write some codes like
#property (weak, nonatomic) IBOutlet UIButton *button;
in my ViewController.m (Created automatically when I made my project)
And I added method
- (IBAction)buttonClicked:(id)sender {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
}
(I made SecondViewController.m, SecondViewController.h, SecondViewController.xib)
After this, I started the application and clicked the button but the screen didn't change.
Actually, when I added log like
NSLog(#"%#", self.navigationController);
null was printed.
I think I need to add some code about NavigationController to AppDelegate.m but I don't know what to do. Please help me.
Try This,
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.viewController = [[ViewController alloc] initWithNibName:#"ViewController"
bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}`
You need to embed Navigation Controller through code or Storyboard.
First select your initial viewcontroller in storyboard and embed it in NavigationController.
Then give a storyboard identifier to the second viewcontroller.
Then lastly instantiate Second ViewController from storyboard and push it.
- (IBAction)buttonClicked:(id)sender {
SecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
[self.navigationController pushViewController:entryController animated:YES];
}
Related
I'm creating an application that has 2 main view controllers at the moment. The app loads into the initial viewController, and clicking a button inside should bring up the second viewController. Here's what I have:
AppDelegate.h
#import <UIKit/UIKit.h>
#import "ViewController1.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController1 *mainViewCtr;
#property (strong, nonatomic) UINavigationController *navigationController;
#end
AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
_mainViewCtr = [[ViewController1 alloc] initWithNibName:#"mainViewCtr" bundle:nil];
_navigationController = [[UINavigationController alloc] initWithRootViewController:_mainViewCtr];
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_window.rootViewController = _navigationController;
_navigationController.delegate = self;
_navigationController.navigationBarHidden = YES;
[_window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];
}
and my button method inside viewcontroller1:
- (IBAction)SessionNickNameSubmit:(id)sender {
ViewController2 *secondViewCtrl = [[ViewController2 alloc] initWithNibName:#"secondViewCtrl" bundle:nil];
[self.navigationController pushViewController:secondViewCtrl animated:YES];
}
but when I click the button the view doesn't change. I tried debugging and the code is hit, but nothing happens.
am I missing a setting somewhere?
UPDATE
I've updated all viewController variable names:
instead of ViewController1/2 I'm using mainViewCtrl and secondViewCtrl
but still no use :(
You made a typo:
it's
_window.rootViewController = _navigationController;
not
_window.rootViewController = _joinViewController;
And NeverHopeless's suggestion is also spot on. It's probably the typo AND the fact that you add your second viewcontroller as ViewController2 and not using a proper variable name.
Another suggestion is making a storyboard (if you are not using one) and adding a segue for the transition. Simply assign the segue processing to the button. Like this:
-(IBAction)SessionNicknameSubmit:(id)sender
{
[self performSegueWithIdentifier:#"identifier" sender:self ];
}
Here is a nice description of how it works and how to use it plus some useful pointers!
Obj-C is a case sensitive language, class name and instance name should not be the same like ViewController2. Try like this:
- (IBAction)SessionNickNameSubmit:(id)sender {
ViewController2 *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
[self.navigationController pushViewController:viewController2 animated:YES];
}
The reason is that you have set the window's rootViewController to ViewController1.
You need to set you navigation controller to the window's rootViewController.
So that when you try to access the self.navigationController on the press of the button, it will access the navigation controller in which the self resides i.e. your window's rootViewController now.
Then it will push the next view controller properly.
After looking at almost every tutorial and every stack overflow answer, I finally found a solution that worked. I had to make an instance of the storyboard in the app delegate and use that to create my first view controller instance.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.joinViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:joinViewController];
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
navigationController.navigationBarHidden = YES;
_window.rootViewController = navigationController;
[_window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
I think the problem was that when I was creating an instance of ViewController, it was creating a new instance and binding the navigation controller to it (independent of the view controller that was showing up in the simulator). So when I was using the push method it wasn't recognizing self.NavigationController (that's why NSLog(self.NavigationController == nil) was logging 1
I am working on single view application. On click of button i want to switch to next page(let say menu controller). Please specify what changes I have to make in appdelegate to add a navigation controller as I am completely new to iOS.
[button addTarget:select action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; and implement the selector as
-(void)buttonClick{
UIViewController *menu = [[UIViewController alloc] init];
[self.navigationController pushViewController:menu animated:YES];
}
Add this in the app delegate for the root view controller:
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"view name" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = navigationController;
Inside the button click:
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"view name" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
you have to set UINavigationController as Window.rootViewController like below.
FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
You should subclass UIViewController and implement the view however you want. Views can be built programmatically or in interface builder.
Then you would either use segues, storyboard identifiers, or a xib file to load the view.
Might look like this: (assuming you set up the view controllers in a storyboard and connected them with appropriate segues & the identifier below)
-(void)buttonClick{
[self performSegueWithIdentifier:#"MySegueIdentifier"];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString: #"MySegueIdentifier"]) {
MyCustomViewController* vc = (MyCustomViewController*)[segue destinationViewController];
//do something with your view controller, like set a property
}
}
Or maybe like this
-(void) buttonClick {
MyCustomViewController* vc = (MyCustomViewController*)[[UIStoryboard storyboardWithName: #"MyStoryboard"] instantiateViewControllerWithIdentifier: #"MyStoryboardIdentifier"];
[self.navigationController pushViewController: vc animated: YES]; //assuming current view controller is a navigation stack. Or could also do presentViewController:animated:
}
You would add a class subclassing UIViewController to your project, and import it (#import "MyCustomViewController.h" in the .m file of the view controller you're pushing from).
Could also use a xib file, but I won't bother with those since Storyboards are much easier to work with.
Without a storyboard:
Inside the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] init]; //navigation controller is a property on the app delegate
// Override point for customization after application launch.
FirstViewController *firstViewController = [[FirstViewController alloc] init];
[navigationController pushViewController: firstViewController animated:NO];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
Inside your FirstViewController:
-(void) buttonClick {
MyCustomViewController* vc = [[MyCustomViewController alloc] init]; // or maybe you have a custom init method
[self.navigationController pushViewController: vc animated: YES];
}
Very simple:
-(void)ButtonClickActionMethod
{
[button addTarget:select action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
}
Clicked Action:
-(void)buttonClick{
YourViewController *view = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[self.navigationController pushViewController:view animated:YES];
}
I am creating a class object from my UIViewController and trying to push a controller from it, and it won't work.
I have been doing research but found nothing, any idea?
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.newClass = [[MyNewClass alloc] init];
self.newClass.view = self.view;
self.newClass.navigationController = self.navigationController;
[self.newClass connect];
}
...
#end
MyNewClass.h
#interface MyNewClass : NSObject<UINavigationControllerDelegate>
#property(nonatomic, retain) UIView *view;
#property(nonatomic, retain) UINavigationController *navigationController;
-(void) connect;
#end
MyNewClass.m
-(void)connect
{
OtherViewController * otherVC =
[[OtherViewController alloc] init];
self.navigationController pushViewController:otherVC animated:YES];
}
...
add folloeing code into appdelegate's didFinishLaunchingWithOptions method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self copyDatabaseIfNeeded];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window makeKeyAndVisible];
return YES;
}
and then remove all other UINavigationController declaration and allocation. Like MyNewClass's NavigationVontroller. Because here you declare and allocate navigationcontroller in appdelegate so you can use it in whole app.
When viewDidLoad is called, the view has just been loaded but the view controller hasn't necessarily been added to a navigation controller yet. So using viewDidLoad as your trigger is not useful.
A better approach is to explicitly pass the navigation controller to the view controller when it's created. Or to implement didMoveToParentViewController: and do your configuration there.
You are pushing a viewController from a controller, which is not a part of navigationController, so first make it part of navigationController, then try
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 have a tab bar application in Xcode 4.3 and I'm trying to insert a login screen before the tabbar is shown. The app works OK if presentModalViewController has animated:YESbut if it is without animation the view is not showing.
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
LogInViewController *logViewController = [[LogInViewController alloc] initWithNibName:#"LogInViewController" bundle:nil];
[self.window addSubview:_tabBarController.view];
[self.tabBarController presentModalViewController:logViewController animated:YES];
//This wont work
//[self.tabBarController presentModalViewController:logViewController animated:NO];
[self.window makeKeyAndVisible];
return YES;
}
-(void)loginDone{
NSLog(#"back to the app delegate");
[self.tabBarController dismissModalViewControllerAnimated:YES];
}
Is this the right way to do it?
Why wont the code work with animated:NO ?
I also get this on output Unbalanced calls to begin/end appearance transitions for <UITabBarController: 0x689d350>.
First of all, move [self.window makeKeyAndVisible]; before your view controller setup.
Additionally, you should be presenting the modal view controller within the viewWillAppear: method of the view controller that will be visible first, to make sure your apps view hierarchy has been fully initialized before presenting your login screen.
Don't do this:
[self.window addSubview:_tabBarController.view];
Do this:
self.window.rootViewController = _tabBarController;
This will put the tabBarController on the screen. But that's not exactly what you want... My advise is:
1) Start by putting the logViewController has the rootViewController as I showed you above.
2) Once you got what you want (login is successful) just tell the AppDelegate to switch the rootViewController. This can be done in with delegation or notifications.
Also, as Toastor indirectly pointed out, you should start the presentViewController from the UIViewController who actually initiates it (and not from the AppDelegate).