iOS: MFSideMenu shows black screen on simulator - ios

i am importing MFSideMenu to my dummy project. i am using the following codes
.h file
#import <UIKit/UIKit.h>
#import "MFSideMenu.h"
#import "RightSideViewController.h"
#import "MFSideMenuContainerViewController.h"
#interface xyzAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property(strong,nonatomic)UINavigationController * navigationController;
#property(strong, nonatomic) RightSideViewController * rightViewController;
#property(strong, nonatomic) MFSideMenuContainerViewController * container;
and .m file is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window= [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UIStoryboard * mainstoryboard = [UIStoryboard storyboardWithName:#"main" bundle:nil];
xyzViewController * vc = [mainstoryboard instantiateViewControllerWithIdentifier:#"xyzViewController"];
[self.navigationController pushViewController:vc animated:YES];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:vc];
self.rightViewController = [[RightSideViewController alloc]init];
self.container = [MFSideMenuContainerViewController containerWithCenterViewController:self.navigationController leftMenuViewController:nil rightMenuViewController:_rightViewController];
self.window.rootViewController = _container;
[self.window makeKeyAndVisible];
return YES;
}
the problem arise when i run the project it shows black screen only nothing else on simulator.
please help me to solve this issue

I solve it by myself Thanks #pkc456
i added the following code to my .m file and now it works fine
self.window= [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UIStoryboard * mainstoryboard = [UIStoryboard storyboardWithName:#"main" bundle:nil];
xyzViewController * vc = [mainstoryboard instantiateViewControllerWithIdentifier:#"xyzViewController"];
[self.navigationController pushViewController:vc animated:YES];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:vc];

Related

NavigatorController is nil

AppDelegate.m
_viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:_viewController];
nav.navigationBar.barStyle = UIBarStyleBlackOpaque;
[_window addSubview:nav.view];
ViewContoller.m
UINavigationController *nav = self.navController;
[nav pushViewController:controller animated:YES];
I don't know why the UINavigationController always nil.
Please help!!
Instead of adding navigationController's view as window's subview try adding navigationController as window's rootViewController
window.rootViewController = nav;
[_window makeKeyAndVisible];
homeViewController = (mainStoryboard.instantiateViewControllerWithIdentifier("register") as? RegisterViewController)!
let navigationController :UINavigationController = UINavigationController()
navigationController.pushViewController(homeViewController, animated: true)
navigationController.navigationBarHidden = false
window?.rootViewController = nil
window?.rootViewController = navigationController
window?.makeKeyWindow()
#interface AppDelegate ()
#property (strong, nonatomic) UINavigationController *navigationController;
#end
//In Your Appdelegate didfinishlaunching method:
self.window = [[UIWindow alloc] init];
[self.window makeKeyAndVisible];
self.navigationController = [[UINavigationController alloc] initWithRootViewController: YourViewController];
self.window.rootViewController = self.navigationController;
//In Your View controller:
[self.navigationController pushViewController:controller animated:YES];
NEW SOLUTION
try this
in AppDelegate.m
#implementation AppDelegate
{}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *_navController = [[UINavigationController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = _navController;
ViewController* _viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[_navController pushViewController:_viewController animated:YES];
return YES;
}
in ViewContoller.m now this will work:
UINavigationController *nav = self.navigationController;
[nav pushViewController:controller animated:YES];
OLD SOLUTION
try this code
in AppDelegate.h be sure to have this at least
#interface AppDelegate : NSObject <UIApplicationDelegate>
{}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
#end
in AppDelegate.m at least this
#implementation AppDelegate
{}
#synthesize window=_window;
#synthesize navController=_navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *_navController = self.window.rootViewController;
UIViewController* _viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[_navController pushViewController:_viewController animated:YES];
return YES;
}
#end
in storyboard be sure to have created a NavigationController binded to a ViewController, defined as initial view controller and binded as root view controller for the binded view controller
in the app general settings tab be sure to have set the storyboard in the deployment info section (also in this section you could set status bar style)
in ViewContoller.m now this will work:
UINavigationController *nav = self.navigationController;
[nav pushViewController:controller animated:YES];
to edit the navigationBar style you can do it this way
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
}

Starting navigation controller from appDelegate

Can anyone tell me how to start UINavigationContoller from ÀppDelegate?
I can start arootViewContollerbut cannot start a specificUIViewControllerlike I was trying in commented code.
The commented code starts the **ChooseTableViewController** but does not displayUINavigationBar`.
whats the better approach?
Here is my code
- (void)setRootViewController:(NSString *)storyBoardName {
//set the Root ViewController
UIStoryboard *story = [UIStoryboard storyboardWithName:storyBoardName
bundle:nil];
UINavigationController *newViewController =
[story instantiateInitialViewController];
self.window.rootViewController = newViewController;
/*
ChooseTableViewController *chooseTableViewController =
[story instantiateViewControllerWithIdentifier:#"ChooseTableViewController"];
self.window.rootViewController = chooseTableViewController;
*/
}
Appdelegate.h
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navigationController;
Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.navigationController = [storyboard instantiateViewControllerWithIdentifier:#"navigation"];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"ChooseTableViewController"];
navigationController=[[UINavigationController alloc]initWithRootViewController:viewController];
self.window.rootViewController =self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
// Your main storyboard
UIStoryboard *story = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
// Your root navigation controller
UINavigationController *newViewController = [story instantiateInitialViewController];
// Your root view controller for root navigation controller
ChooseTableViewController *chooseTableViewController = [story instantiateViewControllerWithIdentifier:#"ChooseTableViewController"];
// Set your view controller as root view controller of your root navigation controller
newViewController.rootViewController = chooseTableViewController;
// set your root navigation controller
self.window.rootViewController = newViewController;

Always display tabBarController when pushed to any viewController

I have 1 UITabbarController with 3 ViewControllers belong to its named aVC-bVC-cVC(VC = ViewController). Then I have 1 MenuSideBar have others 3 ViewControllers named dVC-eVC-fVC.
All I want is whenever I push to dVC or eVC or fVC my UITabbarController always display. How I can do that ? I really stuck at this point. I have to try many ways even custom a tabbarController myself but still not working.
Please help me. I really need help on this case.
Very thank you.
I have made the sample demo for you in Objective C using SWRevealViewController as you had tagged that in your question. I have used the XCode 7.1.1 version. Make sure to open this demo in 7.x versions of XCode. This problem can also be solved by other side menu libraries like MFSideMenu etc
Here is the link of the demo sample made by me according to your requirements.
https://github.com/RajanMaheshwari/TabBarControllerApp
EXPLANATION:
In order to keep a tab bar always in your app you have to add a TabBar on every controller which is not a added viewController of UITabBarController. In my sample demo I have taken the various Controllers
1.LeftSideViewController - which will be the left side panel for whole app.
2.MainTabBarViewController - which will be the UITabBarController subclass and it will be connected to two other view controllers which will be the part of MainTabBarViewController.
----a.TabFirstViewController
----b.TabSecondViewController
3.FirstViewController- which will be the one that will from LeftSideViewController's table view and has its own UITabBar added.
4.SecondViewController- which will be the one that will from LeftSideViewController's table view and has its own UITabBar added.
Import the SWRevealViewController folder.
In your AppDelegate's didFinishLaunchingWithOptions method we need to make the root view controller as the MainTabBarViewController.
AppDelegate.h
#import <UIKit/UIKit.h>
#import "SWRevealViewController.h"
#interface AppDelegate : UIResponder<UIApplicationDelegate,SWRevealViewControllerDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) SWRevealViewController *viewController;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "LeftSideViewController.h"
#import "SWRevealViewController.h"
#import "MainTabBarViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//Here Adding some notification observers which will be fired whenvever a tabbar index in clicked of the viewcontrollers whose parent is not UITabBarController class and the left side menu ones too.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(backToTabIndexControllerFirst) name:#"backToTabIndexControllerFirst" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(backToTabIndexControllerSecond) name:#"backToTabIndexControllerSecond" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(firstVCWithoutNAV) name:#"firstVCWithoutNAV" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(secondVCWithoutNAV) name:#"secondVCWithoutNAV" object:nil];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:#"LeftSideViewController"];
MainTabBarViewController *mainTabBarController = [storyBoard instantiateViewControllerWithIdentifier:#"MainTabBarViewController"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:leftSideController frontViewController:mainTabBarController];
mainRevealController.delegate = self;
self.viewController = mainRevealController;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Methods Definations of all observers added
AppDelegate.m
-(void)firstVCWithoutNAV{
self.window.rootViewController = nil;
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController * firstVC = [storyBoard instantiateViewControllerWithIdentifier:#"FirstNAV"];
LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:#"LeftSideViewController"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:leftSideController frontViewController:firstVC];
mainRevealController.delegate = self;
self.viewController = mainRevealController;
self.window.rootViewController = self.viewController;
}
-(void)secondVCWithoutNAV{
self.window.rootViewController = nil;
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController * secondVC = [storyBoard instantiateViewControllerWithIdentifier:#"SecondNAV"];
LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:#"LeftSideViewController"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:leftSideController frontViewController:secondVC];
mainRevealController.delegate = self;
self.viewController = mainRevealController;
self.window.rootViewController = self.viewController;
}
-(void)backToTabIndexControllerFirst{
UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:#"LeftSideViewController"];
MainTabBarViewController *mainTabBarController = [storyBoard instantiateViewControllerWithIdentifier:#"MainTabBarViewController"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:leftSideController frontViewController:mainTabBarController];
mainRevealController.delegate = self;
self.viewController = mainRevealController;
self.window.rootViewController = self.viewController;
}
-(void)backToTabIndexControllerSecond{
UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LeftSideViewController *leftSideController = [storyBoard instantiateViewControllerWithIdentifier:#"LeftSideViewController"];
MainTabBarViewController *mainTabBarController = [storyBoard instantiateViewControllerWithIdentifier:#"MainTabBarViewController"];
//To make selected index as the one which is clicked from tab bar
mainTabBarController.selectedIndex = 1;
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:leftSideController frontViewController:mainTabBarController];
mainRevealController.delegate = self;
self.viewController = mainRevealController;
self.window.rootViewController = self.viewController;
}
In the Controllers which have no UITabBarController parent class, we will add a tabBar and set tabBar's delegate to self and add the tabBar's didSelectItem delegate method.
FirstViewController.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController<UITabBarDelegate>
#property (weak, nonatomic) IBOutlet UITabBar *tabBar;
#end
FirstViewController.m
#import "FirstViewController.h"
#import "SWRevealViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
SWRevealViewController *revealController = [self revealViewController];
[self.view addGestureRecognizer:revealController.panGestureRecognizer];
UIBarButtonItem* revealButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"reveal-icon.png"] style:UIBarButtonItemStylePlain target:revealController action:#selector(revealToggle:)];
self.navigationItem.leftBarButtonItem = revealButtonItem;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSUInteger indexOfTab = [[tabBar items] indexOfObject:item];
NSLog(#"Tab index = %u", (int)indexOfTab);
if(indexOfTab == 0){
[[NSNotificationCenter defaultCenter] postNotificationName:#"backToTabIndexControllerFirst" object:nil];
}else{
[[NSNotificationCenter defaultCenter] postNotificationName:#"backToTabIndexControllerSecond" object:nil];
}
}
The notifications fired from here will change the root controller of the application.
Similarly we will code for SecondViewController
Assuming that there is a Table For selection of various other controllers from LeftSidePanel, we will add the tableView's didSelectRowAtIndexPath method as follows:
LeftSideViewController.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SWRevealViewController *revealController = self.revealViewController;
[revealController revealToggleAnimated:YES];
// Used this just to show the animation completion and then changing the root
[self performSelector:#selector(changeControllers:) withObject:indexPath afterDelay:0.3];
}
-(void)changeControllers:(NSIndexPath*)indexPath{
if(indexPath.row == 0){
[[NSNotificationCenter defaultCenter] postNotificationName:#"firstVCWithoutNAV" object:nil];
}else{
[[NSNotificationCenter defaultCenter] postNotificationName:#"secondVCWithoutNAV" object:nil];
}
}
We can also use other libraries as I already stated for side panel. Example
https://github.com/mikefrederick/MFSideMenu
Hope this solves the problem!

AppDelegate and .xib are not implementing properly, but build is successful?

I really don't know how to explain this without pasting all my code, but ill give it a shot. "Assuming" my .hs and .ms are accurate, i have a feeling my .xib is not set correctly, but i cant really paste the code from that. Instead i've zipped the files and uploaded the source code. (if you are brave enough, it's here: http://bit.ly/ZtDkGi ) Im getting a successful build, but my emulator's screen is just black after the app launches.
Essentially, i had to manually add an appDelegate object. i set the class to the appropriate class - but its still not pulling. If someone would be kind enough to help, that would be great.
here's my Test_TableViewAppDelegate.h
#import <UIKit/UIKit.h>
#interface Test_TableViewAppDelegate : NSObject <UIApplicationDelegate>
{
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
#end
here's my new Test_TableViewAppDelegate.m
#import "Test_TableViewAppDelegate.h"
#implementation Test_TableViewAppDelegate
#synthesize window=_window;
#synthesize navController=_navController;
- (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];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.backgroundColor = [UIColor greenColor];
self.window = window;
UIViewController *fvc = [[UIViewController alloc] init];
UIViewController *rootController = [[UIViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:rootController];
//UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:fvc];
self.navController = nc;
//[self.window addSubview: nc.view];
//[self.window makeKeyAndVisible];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController {
NSMutableArray *petsArray;
}
#end
RootViewController.m
#import "RootViewController.h"
#interface RootViewController ()
#end
#implementation RootViewController
and last but not least, main.m ( i think this might be an issue too)
#import "Test_TableViewAppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([Test_TableViewAppDelegate class]));
}
}
thanks in advance. i'd appreciate it :D
in your delegate Test_TableViewAppDelegate
why you adding views two times to the window?
// you could remove these two lines
[self.window addSubview: nc.view];
[self.window makeKeyAndVisible];
//keep these two lines
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
And this view you are adding to the navigationController it is not initalized with any nib name
UIViewController *fvc = [[UIViewController alloc] init];
initialization should be like this instead in your delegate
RootViewController *rootController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:rootController];
I believe the reason that you're getting a black screen is that you are not properly allocating and initializing your navigation controller!
Instead, you should try this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// create the base window
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.backgroundColor = [UIColor greenColor];
self.window = window;
[window release];
// this is the home page from the user's perspective
FirstViewController *fvc = [[FirstViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:fvc];
self.navigationController = nc;
[fvc release];
[nc release];
// show them
[self.window addSubview: nc.view];
[self.window makeKeyAndVisible];
return YES;
}
Hope this works!

Pushing View Controller with Two Nav Controllers

I've got an app where I am pushing a modal view controller. It is working fine, but I am concerned I haven't coded it in the most correct fashion. I have instanstiated two navigation controllers, which seems a bit dodgy to me.
Basically I've created a tab bar controller with 3 tabs, then made one of those tabs / view controllers the root. Later I am (using some home-grown markup on core text) popping a view controller when the user touches a particular word in a paragraph. The pushed view controller has a back button which works fine and the app seems to be OK.
Like I said it all works, but it seems I am coding in circles here. Is this correct?
AppDelegate.h
#import <Foundation/Foundation.h>
#interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
UIWindow *window;
UITabBarController *tabBarController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
From AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
ViewController2 *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
ViewController3 *viewController3 = [[ViewController3 alloc] initWithNibName:#"ViewController3" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav, viewController2, viewController3, nil];
self.tabBarController.delegate = self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
ViewController3.h
#import <UIKit/UIKit.h>
#import "JSCoreTextView.h"
#import "PopupViewController.h"
#class JSTwitterCoreTextView;
#interface ReadingViewController : UIViewController <JSCoreTextViewDelegate>
{
JSTwitterCoreTextView *_textView;
UIScrollView *_scrollView;
}
#end
From ViewController3.m
Here I am instantiating another navigation controller. Is this a good idea?
- (void)textView:(JSCoreTextView *)textView linkTapped:(AHMarkedHyperlink *)link
{
PopupViewController *popupVC = [[PopupViewController alloc] initWithNibName:#"PopupViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:popupVC];
[nav setModalPresentationStyle:UIModalPresentationFullScreen];
[nav setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:nav animated:YES];
}
From PopupViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(done:)]];
}
- (void)done:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
It appears the answer is "yes". I was under the impression there is a single Navigation Controller for the app, but it's more like one per tab, depending on if there are going to be further pushes from that tab.

Resources