thank you for taking time to answer me !
I'm inexperienced in IOS development, after several searches on google, I turn to you.
My problem is that : I will wish to use UINavigationController, with new main view, to allow me to create a back button.
Here is my AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MainViewController *mvc = [[MainViewController alloc]init];
//self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//UINavigationController * navController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = mvc;
[self.window makeKeyAndVisible];
return YES;
}
and my AppDelegate.h :
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
//#property (strong, nonatomic) ViewController *viewController;
#end
During development I changed the main view of the application as you can see above the two lines commented out.
I would like to use "UINavigationController" with this new main view, what does it change? and where?
Thank you and sorry if i'm not clear ! (Google Translate)
Set your MainViewController *mvc as the root of your navController and make it the root view controller of the window.
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController: mvc];
self.window.rootViewController = navController;
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
It has been a while since I created a project from scratch. Back then in XCode 4 / 5 the developer was given the choice between storyboard or Xib to get the project started. In Xcode 6 though every template is forced to get started as StoryBoard.
I have removed the story board. Then I have opened info.plist in Supporting files and set Main storyboard file base name : Main to Main storyboard file base name: <blank>.
Then I created a basic testViewController with Xib (same name).
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
testViewController *firstViewController = [[testViewController alloc] initWithNibName:nil bundle:nil];
self.navigationController.viewControllers = [NSArray arrayWithObject:firstViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
And this still shows me a black view controller. I checked the xib file, the File owner is set to the view controller and the view delegate is also set.
What am I missing please?
change these two things:
#property (nonatomic, retain) IBOutlet UIWindow *window;
testViewController *firstViewController = [[testViewController alloc] initWithNibName:nil bundle:nil];
To this, and add this code, with the real name or you xib file:
#property (nonatomic, retain) UIWindow *window; // Are you using ARC?
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// I think your xib files is testViewcontroller.xib, if not change it.
testViewController *firstViewController = [[testViewController alloc] initWithNibName:testViewController.xib bundle:nil];
// change this also
self.navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
I have SingleView application. Now I want to add a mainwindow to my project.
How can I add the window into my project?
Thanks in advance.
First Add Your LoginViewController as self.window.rootViewController such like
Add this code in your Appdelegate.h file
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
}
#property (nonatomic, retain) UIWindow *window;
#end
Add this code in your Appdelegate.m file
(Here i also added UINavigationController too)
#synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
LoginViewController *loginViewController = [[LoginViewController alloc] init];
UINavigationController *loginNVController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
loginNVController.navigationBarHidden = YES;
self.window.rootViewController = loginNVController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
OR Add Directly
Check this link for adding window.xib .
Check this link
If you have choosen single view application. You can give the xib name as per the view. That is more general in sense. If you have selected empty application then go to you xcode project and add new file. which extends from UIViewController.
AppDelegate can not be referenced in any .xib files. the main method will instantiate AppDelegate object and AppDelegate object will then instance other view controllers.
For more you can read some apple documentation.
Thanks
Rinku
I'm very new to ios developing and many of the tutorials i watch follow the xib approach since they were recorded before XCode 5. How can i go with the xib approach in Single View Application? When i delete storyboard and select one of the xib's as the main interface it's not working. Thanks for any tip.
add new file in that select the cococatouch and select the Objective -C class and then go to the next click.
you show the below screen
at the bottom must select the with xib for user Interface.and next
AppDelegate.h:
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
ViewController *viewObj;
UINavigationController *navObj;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewObj;
#property (strong, nonatomic) UINavigationController *navObj;
AppDelegate.m:
#synthesize viewObj,window,navObj;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
viewObj = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navObj = [[UINavigationController alloc] initWithRootViewController:viewObj];
window.rootViewController=navObj;
[window makeKeyAndVisible];
return YES;
}
Select Empty application template while creating new project.
Select Cocoa touch from left pane -> objective-c class -> then give name of viewController and tick option of Xib user interface.
and then in apply below code in appDelegate file:
appDelegate.h File:
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
#interface HomeAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong,nonatomic)HomeViewController *homeViewController;
#property (strong,nonatomic)UINavigationController *navigationController;
appDelegate.m File:
- (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];
self.homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.homeViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
#end
you can do it by following these steps:-
Create a new project
Select Single View Application
Set ProjectName and other settings
Save Project at location
Select Project in Navigator Panel in Left
Remove Main.storyboard file from project
Add New .xib file in Project
Set Main Interface to your .xib file in "General Tab" on right panel.
Paste following code in didFinishLaunchingWithOptions method
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Courtesy:- MKR
https://stackoverflow.com/a/19633059/1865424
Just check it have you missed any of the step.
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