I have main ViewController which is named ViewController(if i run my project it runs firstly). If i push my DetailViewController, it is showing my ViewController view.
Here is my part of code:
AppDelegate.m
ViewController *viewController = [[ViewController alloc] initWithNibName:Nil bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
Viewcontroller.m
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:nil bundle:NULL];
[self.navigationController pushViewController:detailVC animated:YES];
Check following code
DetailViewController *detailVC = [[DetailViewController alloc] init];
[self.navigationController pushViewController:detailVC animated:YES];
In your Button's Action, implement this
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
[self.navigationController pushViewController:detailVC animated:YES];
You are not initializing with its nib file
Related
I am using MFSideMenu in TabBarController In SideMenu i have added UITableview so when i click on cell then it navigate to another view controller but its not navigate to that view controller
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
centerViewController = [storyboard instantiateViewControllerWithIdentifier:#"Tabbarcontroller"];
TabViewController1 *tableView = [storyboard instantiateViewControllerWithIdentifier:#"TabViewController1"];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tableView];
TabViewController2 *tableView2 = [storyboard instantiateViewControllerWithIdentifier:#"TabViewController2"];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:tableView2];
centerViewController.viewControllers = #[nav1,nav2];
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"SideViewController"];
container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController
leftMenuViewController:leftSideMenuViewController
rightMenuViewController:nil];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}
SideMenu.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController3 *objEdit =[self.storyboard instantiateViewControllerWithIdentifier:#"ViewController3"];
AppDelegate *objApp = (AppDelegate *)[[UIApplication sharedApplication]delegate];
UINavigationController *navigationController = (UINavigationController *) objApp.centerViewController.navigationController;
NSLog(#"navigationcontroller :%#",objApp.centerViewController.navigationController);
[navigationController pushViewController:objEdit animated:YES];
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
}
objApp.centerViewController is tabbarcontroller, So you need to find out selected view controller from tabor controller first. If you want to push controller in selected tab, You should get Navigationcontroller from SelectedViewCOntroller of tabbarcontroller.
UINavigationController *navigationController = (UINavigationController *) objApp.centerViewController.selectedViewController;
[navigationController pushViewController:objEdit animated:YES];
didSelectRowAtIndexPath: is firing but the view controller is not. I have tried different view controllers that I know are working. Since the method is firing then I can only speculate that
[self.navigationController pushViewController:detailVC animated:YES];
is not reaching the navigation stack.
But I call the Calendar View Controller from a tabbarcontroller so I do not know what to do to fix this.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
GlobalSingleton *myAppID = [GlobalSingleton sharedSingleton];
myAppID.globalAppID = #"627";
UIViewController *calendarVC;
calendarVC = [[[CalendarViewController alloc] initWithNibName:#"CalendarViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[calendarVC];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
self.tabBarController.customizableViewControllers = nil;
return YES;
}
CalendarViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row %#",[events objectAtIndex:indexPath.row] );
CalendarDetailVC *detailVC = [[CalendarDetailVC alloc] initWithNibName:#"CalendarDetailVC" bundle:nil];
detailVC.dictEvent = [events objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailVC animated:YES];
}
You don't have a navigation controller in your hierarchy, so you can't so a push. You need to add a line to create the navigation controller,
calendarVC = [[[CalendarViewController alloc] initWithNibName:#"CalendarViewController" bundle:nil] autorelease];
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController: calendarVC] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[nav];
I have a tabBar application. One of the tabs has a rootviewcontroller that creates a UITableView and adds it to the subview. When a user clicks a cell in the UITableView I want to push a new rootviewcontroller but I cant get it to work.
In my appDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Create the window
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//Create the UIViewCOntrollers for each tab
_viewController1 = [[[LocavoreRetroFirstViewController alloc] initWithNibName:#"LocavoreRetroFirstViewController" bundle:nil] autorelease];
_viewController2 = [[[LocavoreRetroSecondViewController alloc] initWithNibName:#"LocavoreRetroSecondViewController" bundle:nil] autorelease];
UIViewController *viewController3 = [[[LocavoreRetroThirdViewController alloc] initWithNibName:#"LocavoreRetroThirdViewController" bundle:nil] autorelease];
_viewController4 = [[[LocavoreRetroFourthViewController alloc] initWithNibName:#"LocavoreRetroFourthViewController" bundle:nil] autorelease];
UIViewController *viewController5 = [[[LocavoreRetroFifthViewController alloc] initWithNibName:#"LocavoreRetroFifthViewController" bundle:nil] autorelease];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController1];
//[_viewController1 release];
NSArray* controllers = [NSArray arrayWithObjects:navigationController, _viewController2, viewController3, _viewController4, viewController5, nil];
//Create the tab controller
_tabBarController = [[[UITabBarController alloc] init] autorelease];
[_tabBarController setViewControllers:controllers];
//Initialize the tab controller with the views
// _tabBarController.viewControllers = #[_viewController1, _viewController2,
// viewController3, _viewController4, viewController5];
//Set the window to the tabcontroller view and make it visible
_window.rootViewController = _tabBarController;
_tabBarController.delegate=self;
[_window makeKeyAndVisible];
return YES;
}
In my subview didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RecipePageController *recipePageController = [[RecipePageController alloc] initWithNibName:#"RecipePageController" bundle:nil];
[self.navigationController pushViewController:recipePageController animated:YES];
[recipePageController release];
}
For each tab you need to create a separate navigation controller
I want set a tabbar in my app and in my appDelegate I do this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
[viewController2 viewDidLoad];
UIViewController *viewController3 = [[[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil] autorelease];
[viewController3 viewDidLoad];
UIViewController *viewController4 = [[[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil] autorelease];
[viewController4 viewDidLoad];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4];
self.viewController = [[[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
you can see that I want to start my app not immediately with tab bar, in fact I start my app with an HomeViewController
inside my HomeViewController i open viewController1 with:
FirstViewController *first = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
[self presentViewController:first animated:YES completion:nil];
[first release];
but in my firstviewcontroller I don't see my tabbar, why? (I state that the app work fine with tabbar if I start without homeviewcontroller)
You don't see tabbar because in HomeViewController you create new instance of FirstViewController and present it modally. You should create and present the whole UITabBarController inside HomeViewController:
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
inside HomeViewController:
FirstViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
SecondViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
ThirdViewController *viewController3 = [[[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil] autorelease];
FourthViewController *viewController4 = [[[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4];
[self presentViewController:self.tabBarController animated:YES completion:nil];
Ok, so I a a Master Detail application, and when a user clicks on a cell, it loads loads a webpage in the DetailViewController. The problem is, that on the iPhone version (this is a universal app) I have to click on the cell, then go back to the MasterViewController and then click on the cell again to load webpage. I only have to do this once, I think it is like initiating the webpage or something the first time. On the iPad version though, it loads it one the first time. So what's up?
Here is the code for my applicationDidFinishLaunching AppDelegate.m file, where I init the DetailViewController
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController_iPhone" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
masterViewController.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController_iPhone" bundle:nil];
} else {
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController_iPad" bundle:nil];
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController_iPad" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
masterViewController.detailViewController = detailViewController;
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = #[masterNavigationController, detailNavigationController];
self.window.rootViewController = self.splitViewController;
}
here is the code for the didSelectCellAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *object = _objects[indexPath.row];
NSURL *ex = [NSURL URLWithString:[object objectForKey:#"url"]];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.detailViewController.detailItem = object;
[self.detailViewController loadURL:ex];
self.detailViewController.detailItem = object;
[self.detailViewController loadURL:ex];
[self.navigationController pushViewController:self.detailViewController animated:YES];
} else {
self.detailViewController.detailItem = object;
[self.detailViewController loadURL:ex];
}
}
On the iPad, you have the 2 controllers in a split view controller, so they are instantiated, and their view's loaded when the app starts up. On the iPhone, that second controller is instantiated, but it's view isn't loaded until you push it -- I think that's the difference. You should try moving the detailController method loadURL: to it's viewDidAppear method, instead of having it in the master controller's didSelectRowAtIndexPath method.