Present view controller before initial view controller is shown - ios

I have an app that has the following structure:
The main view controller is a tab bar view controller that has 4 tabs.
Users can only access these view controllers when they are logged in. When app launches, it loads it's initial view controller (the tab bar one), and then I check to see if user is authenticated. If it's not, I present a login view controller.
The problem I'm having is that when app launches, it's loading the tab bar controller and from that controller it presents the login view controller, but it does so with a small time window that shows the tab bar controller's view on the screen, before the login's view. I need to directly present the login view, from the tab bar controller, but without showing tab bar controller's view in that small time interval, as it's not user friendly.
I read some answers on stackoverflow about presenting the new view controller without animation, and that's what I'm doing, but the problem persists.
I hope I've been clear enough on the issue, if you need more information just let me know.
EDIT: I'm presenting the login view controller on applicationDidBecomeActive: on applicationDelegate.m

In app delegate, In didFinishLaunchingWithOptions,
In condition if login or not,
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController =[storyboard instantiateViewControllerWithIdentifier:#"your identifier"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];

You should something like this when you have view controller in storyboard.
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FirstViewController *firstviewController = [storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
SecondViewController *secondviewController = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
firstviewController.tabBarItem.image = [UIImage imageNamed:#"24-around-7"];
firstviewController.tabBarItem.title = #"First";
secondviewController.tabBarItem.title = #"Second";
secondviewController.tabBarItem.image = [UIImage imageNamed:#"60-around-7"];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
tabBarController.viewControllers = #[firstviewController,secondviewController];
self.window.rootViewController = tabBarController;
And in FirstViewController's viewDidAppear you should check for user logged in or not and present view controller.
BOOL loginStatus = [[NSUserDefaults standardUserDefaults] boolForKey:#"isLoggedIn"];
if(loginStatus == NO){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LoginViewController *loginViewcontroller = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self presentViewController:loginViewcontroller animated:YES completion:nil];
}
After successful login just dismiss login view controller . If user logout just present the loginviewcontroller

CREATE NEW .H .M
(this is my code)
yourController.h and yourController.m
then open
yourcontroller.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSLog(#"initWithFrame");
[self setupPinPopUp];
}
return self;
}
-(void)setupPinPopUp{
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurEffectView.frame = CGRectMake(0, 66, kSCREEN_WIDTH, kSCREEN_HEIGHT-66);
blurEffectView.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[blurEffectView addGestureRecognizer:singleFingerTap];
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:blurEffectView];
UIView *popUpMainView = [[UIView alloc]initWithFrame:CGRectMake(kSCREEN_WIDTH/2-150, kSCREEN_HEIGHT/2-160, 300, 270)];
popUpMainView.backgroundColor = Clear;
[self addSubview:popUpMainView];
UIView *popUpInsideView = [[UIView alloc]initWithFrame:CGRectMake(kSCREEN_WIDTH/2-150, kSCREEN_HEIGHT/2-102, 300, 210)];
popUpInsideView.backgroundColor = White;
popUpInsideView.layer.cornerRadius = 2.0;
popUpInsideView.clipsToBounds = TRUE;
[self addSubview:popUpInsideView];
UIImageView *imgCircleView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 0, 100, 100)];
imgCircleView.layer.cornerRadius = 50;
imgCircleView.backgroundColor = White;
imgCircleView.clipsToBounds = TRUE;
[popUpMainView addSubview:imgCircleView];
UIImageView *imgInnerCircleView = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 50, 50)];
imgInnerCircleView.backgroundColor = Clear;
imgInnerCircleView.image = [UIImage imageNamed:#"support"];
[imgCircleView addSubview:imgInnerCircleView];
UILabel *lblHeading = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, popUpMainView.frame.size.width, 45)];
lblHeading.text = #"CUSTOMER SUPPORT";
lblHeading.numberOfLines = 0;
lblHeading.textColor = Black;
lblHeading.textAlignment = NSTextAlignmentCenter;
lblHeading.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:21];
[popUpInsideView addSubview:lblHeading];
UIButton *btnPhoneNumber = [[UIButton alloc]initWithFrame:CGRectMake(0, lblHeading.frame.size.height+lblHeading.frame.origin.y+10, popUpMainView.frame.size.width, 45)];
[btnPhoneNumber setTitle:#"18002345678" forState:UIControlStateNormal];
[btnPhoneNumber setTitleColor:Black forState:UIControlStateNormal];
btnPhoneNumber.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
btnPhoneNumber.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
[btnPhoneNumber addTarget:self action:#selector(callSupport:) forControlEvents:UIControlEventTouchUpInside];
[popUpInsideView addSubview:btnPhoneNumber];
UIButton *btnEmail = [[UIButton alloc]initWithFrame:CGRectMake(0, btnPhoneNumber.frame.size.height+btnPhoneNumber.frame.origin.y+10, popUpMainView.frame.size.width, 45)];
[btnEmail setTitle:#"support#vurify.com" forState:UIControlStateNormal];
[btnEmail setTitleColor:Black forState:UIControlStateNormal];
btnEmail.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
btnEmail.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
[btnEmail addTarget:self action:#selector(emailSupport:) forControlEvents:UIControlEventTouchUpInside];
[popUpInsideView addSubview:btnEmail];
}
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
[self removeFromSuperview];
}
-(IBAction)callSupport:(id)sender{
NSString *phoneNumber = [#"tel://" stringByAppendingString:#"180023456789"];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber] options:#{} completionHandler:^(BOOL success)
{
if (success) {
NSLog(#"Opened url");
}
}];
}
-(IBAction)emailSupport:(id)sender{
[self removeFromSuperview];
}
//call the method like this wherever you want to call
//IN APP DELEGATE
UIView *rectView;
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[rectView removeFromSuperview];
rectView = [[VurifyAppValidation alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT)];
[self.window addSubview:rectView];
}

Related

UINavigationBar with UISegmentedControl partially covers childViews

I have read many other threads on this and the Apple docs, but haven't found a solution yet for my particular problem.
My app uses a UITabBarController as the rootViewController, and in one of the tabs I have a UISegmentedControl in the navigationBar to switch between three child UITableViewControllers.
(In the real app two of the childVCs are a custom UIViewController, I'm just using three UITableViewControllers for the sample app).
The segmentedControl setup and the switching all works fine. The thing that goes wrong is that only the first UITableViewController is shown correctly. For the second and third one, part of the first cell is hidden under the navigationBar. When I click through all three, the first one is still ok.
I have made a little sample app to show what's going on, using very bright colors for demonstration purposes: https://www.dropbox.com/s/7pfutvn5jba6rva/SegmentedControlVC.zip?dl=0
Here is also some code (I'm not using storyboards):
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
FirstViewController *fvc = [[FirstViewController alloc] init];
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController: fvc];
SecondViewController *svc = [[SecondViewController alloc] init];
UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController: svc];
// Initialize tab bar controller, add tabs controllers
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = #[firstNavigationController, secondNavigationController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
// FirstViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"One";
self.view.backgroundColor = [UIColor orangeColor];
UITableViewController *vc1 = [[UITableViewController alloc] init];
UITableViewController *vc2 = [[UITableViewController alloc] init];
UITableViewController *vc3 = [[UITableViewController alloc] init];
vc1.view.backgroundColor = [UIColor redColor];
vc2.view.backgroundColor = [UIColor blueColor];
vc3.view.backgroundColor = [UIColor greenColor];
self.viewControllers = #[vc1, vc2, vc3];
self.segmentTitles = #[#"Red", #"Blue", #"Green"];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems: self.segmentTitles];
[self.segmentedControl addTarget: self
action: #selector(segmentClicked:)
forControlEvents: UIControlEventValueChanged];
self.navigationItem.titleView = self.segmentedControl;
self.segmentedControl.selectedSegmentIndex = 0;
// set the first child vc:
UIViewController *vc = self.viewControllers[0];
[self addChildViewController: vc];
vc.view.frame = self.view.bounds;
[self.view addSubview: vc.view];
self.currentVC = vc;
}
- (void)segmentClicked:(id)sender
{
if (sender == self.segmentedControl)
{
NSUInteger index = self.segmentedControl.selectedSegmentIndex;
[self loadViewController: self.viewControllers[index]];
}
}
- (void)loadViewController:(UIViewController *)vc
{
[self addChildViewController: vc];
[self transitionFromViewController: self.currentVC
toViewController: vc
duration: 1.0
options: UIViewAnimationOptionTransitionFlipFromBottom
animations: ^{
[self.currentVC.view removeFromSuperview];
vc.view.frame = self.view.bounds;
[self.view addSubview: vc.view];
} completion: ^(BOOL finished) {
[vc didMoveToParentViewController: self];
[self.currentVC removeFromParentViewController];
self.currentVC = vc;
}
];
}
So obviously my question is, why does this happen, and what can I do to fix it?
Edit: adding screenshots.
EDIT: Based on the answer below I changed the code in the animation block to:
[self.currentVC.view removeFromSuperview];
if ([vc.view isKindOfClass: [UIScrollView class]])
{
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, self.bottomLayoutGuide.length, 0);
[UIView performWithoutAnimation: ^{
vc.view.frame = self.view.bounds;
((UIScrollView *)vc.view).contentInset = edgeInsets;
((UIScrollView *)vc.view).scrollIndicatorInsets = edgeInsets;
}];
}
else
{
vc.view.frame = self.view.bounds;
}
[self.view addSubview: vc.view];
Now it works. I'm going to try this with a custom UIViewController as well.
The issue is that you do not set the correct content inset to each table view. The system attempts to do it for you, but I guess your setup is too complex for it, and it only does it for the first tableview that is loaded in viewDidLoad. In your loadViewController: method, when replacing the currently displayed view, make sure to set both the contentInset and scrollIndicatorInsets to the values of the previous view. I think the system will manage to set the correct insets later, in case you rotate to landscape. Try it. If it doesn't, you will need to do it on your own in viewDidLayoutSubviews.

Button to Present ViewController After Tutorial (which Loads For 1st Time Only)

I have a tutorial ViewController called tutorialViewController which only presents itself for the first time the app is launched. This is how I do that:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL hasRunBefore = [defaults boolForKey:#"FirstRun"];
if (!hasRunBefore) {
[defaults setBool:YES forKey:#"FirstRun"];
self.window.rootViewController = [tutorialViewController new];
self.window.backgroundColor = [UIColor whiteColor];
// RESideMenu Stuff that Must Be Done
leftSideMenuViewController *leftMenuViewController = [[leftSideMenuViewController alloc] init];
rightSideMenuViewController *rightMenuViewController = [[rightSideMenuViewController alloc] init];
musicPlayerViewController *navigationController = [[homeViewController alloc] init];
RESideMenu *sideMenuViewController = [[RESideMenu alloc] initWithContentViewController:navigationController
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
sideMenuViewController.menuPreferredStatusBarStyle = 1;
sideMenuViewController.delegate = self;
sideMenuViewController.contentViewShadowColor = [UIColor blackColor];
sideMenuViewController.contentViewShadowOffset = CGSizeMake(0, 0);
sideMenuViewController.contentViewShadowOpacity = 0.6;
sideMenuViewController.contentViewShadowRadius = 12;
sideMenuViewController.contentViewShadowEnabled = YES;
self.window.backgroundColor = [UIColor blackColor];
}
else
{
NSLog (#"Not the first time this controller has been loaded");
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[homeViewController alloc] init]];
// RESideMenu Stuff that Must Be Done
leftSideMenuViewController *leftMenuViewController = [[leftSideMenuViewController alloc] init];
rightSideMenuViewController *rightMenuViewController = [[rightSideMenuViewController alloc] init];
RESideMenu *sideMenuViewController = [[RESideMenu alloc] initWithContentViewController:navigationController
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
sideMenuViewController.menuPreferredStatusBarStyle = 1;
sideMenuViewController.delegate = self;
sideMenuViewController.contentViewShadowColor = [UIColor blackColor];
sideMenuViewController.contentViewShadowOffset = CGSizeMake(0, 0);
sideMenuViewController.contentViewShadowOpacity = 0.6;
sideMenuViewController.contentViewShadowRadius = 12;
sideMenuViewController.contentViewShadowEnabled = YES;
self.window.rootViewController = sideMenuViewController;
self.window.backgroundColor = [UIColor blackColor];
}
This works well so far - if I load the app for the first time, it shows tutorialViewController. If I close it and open it again, it shows homeViewController.
I'd just like to know how to add an IBAction to take me from tutorialViewController to homeViewController. Currently, I wrote this:
- (IBAction)goToHomeViewController:(id)sender {
homeViewController *navigationController = [homeViewController new];
[self presentViewController:navigationController animated:YES completion:^{
}];
NSLog(#"Start button pressed");
}
It presents homeViewController but it does not show the navigation bar, etc. - as it would if homeViewController was the rootViewController.
Here are some more details
tutorialViewController does not show a status bar or a navigation bar. It is simply a scrollview that takes up the whole screen.
'homeViewControllerhas a navigation bar with a left bar button and right bar button that brings up otherviewControllers`.
I've searched other SO questions but they are mostly to do with Storyboards, which I am not using. Just .xibs (and .h and .m).
How do I present homeViewController from tutorialViewController just as it would display if homeViewController was the rootViewController?
Wrap homeViewController in a UINavigationController should work.
[self presentViewController:[[UINavigationController alloc] initWithRootViewController:homeViewController] animated:YES: completion:nil]
You can also do like this.
In your 'if' condition add tutorialViewController as
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[tutorialViewController alloc] init]];
And in the goToHomeViewController function add,
homeViewController *navigationController = [homeViewController new];
[self.navigation pushViewController:navigationController animated:NO];
So first time the tutorialViewController act as rootview controller and if you want to hide NavigationBar add following code,
[[self navigationController] setNavigationBarHidden:YES animated:YES];

Switch view to another view on storyboard

I am new in iOS programming.
What I am trying to do is:
I have some views in a storyboard and I'd like to switch between the views programatically. For example, when a button is clicked, I call a method and I want to change views here (I can call the method successfully). The buttons are also created programatically in different positions.
I have searched and I think it happens with NavigationController. I have a navigation controller which I created like so: menu Editor -> Embed In -> NavigationController. How could I do this using this NavigationController?
#Madhu and #Dilip ,I found a solution with xib filed class
icerik *secondViewController = [[icerik alloc] initWithNibName:#"icerik" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
navigationController.topViewController.title = #"SecondViewController";
//[self presentModalViewController:navigationController animated:YES];
if([self respondsToSelector:#selector(presentViewController:animated:completion:)])
[self presentViewController:navigationController animated:YES completion:^{/* done */}];
else if([self respondsToSelector:#selector(presentViewController:animated:)])
[self presentModalViewController:navigationController animated:YES];
I have a class with xib file named icerik, I solved it like this. It is opening but when I want to turn back, What can I do it ?
You can create btn using this code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
and for going to another vc use this code,if you want navigation bar:
-(void)aMethod
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:SecondViewController];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
navigationController.topViewController.title = #"SecondViewController";
[self presentModalViewController:navigationController animated:YES];
}
Else use this code:
-(void)aMethod
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:secondViewController animated:YES];
}
And for come back to frist vc fromm second vc add this code in second vc.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = closeButton;
}
- (void)backAction:(id)sender {
[self dismissModalViewControllerAnimated:NO];
}
If your new to Objective-c first go with Views/ViewControllers. i.e. use addSubView property of UIView
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 250.0, kCCCellHeaderHeight)];
[subView setBackgroundColor:[UIColor redcolor]];
[self.view addSubview:subView];
If your little known of UINavigationCOntroller Use pushViewController
CCFilteredVideosController *filteredController = [[CCFilteredVideosController alloc] initWithNibName:#"CCFilteredVideosController" bundle:nil];
[self.navigationController pushViewController:filteredController animated:YES];
[filteredController release];

Greedy UITabBarController?

I'm making a custom UIViewController which holds to sub view controllers. They are put inside the main view controller this way:
- (void) setHeaderViewController:(UIViewController *)vc
{
[self setViewController:vc isHeader:YES];
_headerViewController = vc;
}
- (void) setDetailViewController:(UIViewController *)vc
{
[self setViewController:vc isHeader:NO];
_detailViewController = vc;
}
- (void) setViewController:(UIViewController*) viewController isHeader:(BOOL)isHeader
{
UIViewController* viewControllerRef = ((isHeader) ? _headerViewController : _detailViewController);
if(viewControllerRef == viewController)
return;
const CGSize selfSize = self.view.frame.size;
CGRect viewFrame;
if (isHeader)
{
viewFrame = CGRectMake(0, 0, selfSize.width, 150);
}
else
{
viewFrame = CGRectMake(0, 150, selfSize.width, selfSize.height-150);
}
UIView* viewControllerView = viewController.view;
[viewControllerView setFrame:viewFrame];
[viewControllerView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | ((isHeader) ? UIViewAutoresizingFlexibleBottomMargin : UIViewAutoresizingFlexibleTopMargin))];
[self.view addSubview:viewControllerView];
}
If I put two UIViewControllers inside of each sub view controller slot, the sizes defined in the method is respected. However, when putting a UITabBarController, it gets greedy and occupies the whole space:
UIViewController *vc = [[WIFCustomerDetailHeaderViewController alloc] initWithNibName:#"WIFCustomerDetailHeaderViewController" bundle:nil];
[self setHeaderViewController:vc];
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = #"Personal Data";
vc1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Personal Data" image:nil tag:0];
vc1.view.backgroundColor = [UIColor redColor];
vc1.view.frame = CGRectMake(0,400,100,100);
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = #"Diagnosis";
vc2.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Diagnosis" image:nil tag:0];
vc2.view.backgroundColor = [UIColor purpleColor];
vc2.view.frame = CGRectMake(0,500,50,50);
NSArray *tabs = [[NSArray alloc] initWithObjects:vc1, vc2, nil];
WIFCustomerDetailTabViewController *tvc = [[WIFCustomerDetailTabViewController alloc] init];
[tvc setViewControllers:tabs];
[self setDetailViewController:tvc];
Does anyone knows what can be happening here? I can't find the cause...
Images below..
Documentation for the UITabBarController class says, "you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller".
It seems that you're attempting a design that's specifically not supported.

IOS UINavigationController, pushViewController not working

Good evening,
I currently have two UIViewControllers. My appDelegate looks like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
struct CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.x = rect.origin.y = 0.0f;
_viewController = [[sandboxViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_viewController];
_window = [[UIWindow alloc] initWithFrame:rect];
[_window makeKeyAndVisible];
[_window addSubview:nc.view];
return YES;
}
The viewController looks like this:
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = #"Master View";
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
}
- (void)switchView:(id)obj {
if(![self navigationController])
NSLog(#"navigationController IS NIL!!!");
if(!_secondView)
_secondView = [[secondViewController alloc] init];
[self.navigationController pushViewController:_secondView animated:YES];
}
By clicking on the info button, that was added to the right side on the navigation bar, I want to switch to the secondView. This, however, is not happening because navigationController logs as nil ! What am I missing here?
Any help is truly appreciated!
You don't have to create the window, it should already exist.
//_window = [[UIWindow alloc] initWithFrame:rect]; //remove this line
[self.window makeKeyAndVisible]; //use the ivar
[self.window addSubview:nc.view];

Resources