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];
Related
I have login and signup viewcontrollers in Login storyboard not in the Main storyboard.
Once signup or login is successful, then I am changing Login storyboard to Main. The following code works but when I select any tab, it does not call tab delegate method in the AppDelegate
However, if user is already succesfully signup or logged in, then it calls the following tabbar delegate method.
LoginViewController.m
if(isLoginSuccess)
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
CustomTabBarViewController *tbc = [mainStoryboard instantiateViewControllerWithIdentifier:#"tabbar"];
tbc.selectedIndex = 2;
[self presentViewController:tbc animated:YES completion:nil];
}
AppDeletage.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CustomTabBarViewController *tabBarController = (CustomTabBarViewController *)self.window.rootViewController;
tabBarController.delegate = self;
return YES;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 2) {
if(![self isRegistered])
{
UIStoryboard *loginStoryboard = [UIStoryboard storyboardWithName:#"LoginStoryboard" bundle:nil];
UIViewController *vc = [loginStoryboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[ROOTVIEW presentViewController:vc animated:YES completion:nil];
}
else
{
tabBarController.selectedIndex = 2;
}
}
}
Update :
AppDelegate is not getting called but I wonder why the following code does not open Loginstoryboard in the AppDelegate after user is logout.
#define ROOTVIEW [[[UIApplication sharedApplication] keyWindow] rootViewController]
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"LoginStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[ROOTVIEW presentViewController:vc animated:YES completion:nil];
Please find below code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *main = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabController = [main instantiateViewControllerWithIdentifier:#"TabbarController"];
tabController.delegate = self;
return YES;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(#"Selected Index - %lu",(unsigned long)tabBarController.selectedIndex);
}
On ViewController's Button Click Method,
- (IBAction)btnLoginTapped:(id)sender {
UIStoryboard *main = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabController = [main instantiateViewControllerWithIdentifier:#"TabbarController"];
tabController.selectedIndex = 1;
[self presentViewController:tabController animated:YES completion:nil];
}
then on Main.storyboard, Drag Object from the Object Library and put it below First Responder in Tab bar Controller Scene, Set the Object class to AppDelegate, then right click on Tab Bar Controller and set delegate to that Object Class as shown in below image.
Let me know it is working or not, I'm ready to help with the solution.
I would like to push to ParcelPickUpViewController after I clicked a button in LocationDetailsTableViewCell xib. But I've written the code below and it able to run. But the result won't happen at all. Need help on this issue. Thanks.
In LocationDetailsTableViewCell.h
#property (nonatomic, strong) UINavigationController *viewController;
In LocationDetailsTableViewCell.m
- (IBAction)selectBtn:(id)sender {
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ParcelPickUpViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier: # "pickUpVC"];
[self.viewController pushViewController:vc animated: YES];
[self.viewController setNavigationBarHidden:YES animated:YES];
}
I think you have button in UITableViewCell.Now you want to navigate to next view from the button click.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
cell = [nibObjects objectAtIndex:0];
}
cell.btnSelct.tag = indexPath.row;
[cell.btnSelct addTarget:self action:#selector(selectBtn:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)selectBtn:(id)sender
{
//This is for XIB
ParcelPickUpViewController *vc = [[ParcelPickUpViewController alloc]initWithNibName:#"pickUpVC" bundle:nil];
//If you use storyboard
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ParcelPickUpViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier: #"parcelpickup"];
[self.viewController pushViewController:vc animated: YES];
}
Try This ...
- (IBAction)selectBtn:(id)sender {
ParcelPickUpViewController *vc = [[HomeScreenVC alloc]initWithNibName:#"pickUpVC" bundle:nil];
[self.viewController pushViewController:vc animated: YES];
[self.viewController setNavigationBarHidden:YES animated:YES];
}
option 2
you are created the Navigation controller not allocated the memory of your instance navigation controller, so do like
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ParcelPickUpViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier: # "pickUpVC"];
self.viewController = [[UINavigationController alloc] init];
[self.viewController pushViewController:vc animated: YES];
[self.viewController setNavigationBarHidden:YES animated:YES];
if you no need the setNavigationBarHidden then use like
option 3
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ParcelPickUpViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier: # "pickUpVC"];
[self presentViewController:vc animated:YES completion:nil];
(1) Make sure you have set the isInitialViewController(the first controller you want to display when app launch) in storyboard.
(2) If you did not add UINavigationController in storyboard, create it programeticaly and set it as rootViewController w.r.t your InitialViewController in app delegate didFinishLaunchingWithOptions.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard *storyboard = storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:Nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"Your_First_View_Controller_Identifier"];
UINavigationController *rootNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = rootNavigationController;
return YES;
}
(3) While navigating to your ParcelPickUpViewController just simply add below code:
- (IBAction)selectBtn:(id)sender {
ParcelPickUpViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier: # "pickUpVC"];
[self.navigationController pushViewController:vc animated: YES];
}
how to go on home page on login button click
- (IBAction)loginClick:(id)sender {
HomeVC *secondView = [self.storyboard instantiateViewControllerWithIdentifier:#"home"];
[self.navigationController pushViewController:secondView animated:YES];
self.tabBarController.selectedIndex = 1;
}
First you have to set your login page as a Root controller and when you successfully logged in then you have to change your root controller and make your Tab Bar as a root controller.
Perform this code in your AppDelegate and call these method on your login and logout page.
#property (strong, nonatomic) UIWindow *window;
#property(nonatomic,readonly) UITabBar *tabBar;
#property(nonatomic,strong) UITabBarController *tabBarController;
- (void)Login{
[self.window setRootViewController:nil];
UIStoryboard *MainStoryboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle: nil];
UITabBarController *bar= (UITabBarController*)[MainStoryboard instantiateViewControllerWithIdentifier:#"tabbar"];
self.window.rootViewController=bar;
}
-(void)Logout
{
// self.window.rootViewController = nil;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UINavigationController*vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"homepage"];
// Homepage *controller = [[Homepage alloc] initWithDelegate:self];
self.window.rootViewController = vc;
}
Implement didFinishLaunching method this way :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"LoginView"];
BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:#"IS_LOGIN"];
if (isLogin == YES)
{
rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"MainHomeTabView"];
}
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
And set TRUE when you pressed login button
- (IBAction)login:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:#"IS_LOGIN"];
[[NSUserDefaults standardUserDefaults] synchronize];
TabListViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MainHomeTabView"];
AppDelegate *appDelagate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelagate.window.rootViewController = vc;
}
Hi i am trying to make UINavigationController but not in mainViewController(first viewControllerClass) , i need to put UINavigationController in second class. But If i will write these codes 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];
self.window.rootViewController = self.viewController;
// [window addSubview:[navigationController view]];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
Then UINavigationController appears in mainView. I am trying to put it in other class like that
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// strWhichTaleOnScreen=[masivTaleNames objectAtIndex:indexPath.row];
NSString *selectDay;
selectDay=#"first string";
NSLog(#"selecDay=%#",selectDay);
classDetailOfMessagesViewController *nesneDetailOfMessagesViewController = [[classDetailOfMessagesViewController alloc] initWithNibName:#"classDetailOfMessagesViewController" bundle:nil];
nesneDetailOfMessagesViewController.selectDay = selectDay;
[navigation pushViewController: nesneDetailOfMessagesViewController animated:YES];
nesneDetailOfMessagesViewController = nil;
}
But it doesn't work, I guess i have to create rootViewController in this second view but i dont know how.
I ll be happy if someone can show me way to solve it out.
The first mistake i saw was you are trying to set the window rootviewcontroller twice. The view hierarchy needs to be like window->navigation controller->view controller. so I made some changes with your code.
- (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;
}
In the second code sample I couldn't find the reference to the navigation. And also if you push (in your case initWithRootViewController) a view controller to navigationcontroller stack you can access the navigation controller by self.navigationController so your second part of the code should be like this;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectDay;
selectDay=#"first string";
NSLog(#"selecDay=%#",selectDay);
classDetailOfMessagesViewController *nesneDetailOfMessagesViewController = [[classDetailOfMessagesViewController alloc] initWithNibName:#"classDetailOfMessagesViewController" bundle:nil];
nesneDetailOfMessagesViewController.selectDay = selectDay;
[self.navigationController pushViewController: nesneDetailOfMessagesViewController animated:YES];
}
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