I had Implemented MFSideMenu in my Application to display left side slide in-out menu.Now I would like to recognise swipe in and swipe out (i.e. Sidemenu open or closed status)by creating delegate method in ThirdParty class MFSideMenuContainerViewController.h as
#import <UIKit/UIKit.h>
#import "MFSideMenuShadow.h"
#class MFSideMenuContainerViewController;
#protocol MFSideMenuContainerViewControllerViewDelegate <NSObject> //define delegate protocol
- (void)swipedLeftSidemenu:(BOOL)isOpen; //define delegate method to be implemented within another class
#end //end protocol
#interface MFSideMenuContainerViewController : UIViewController<UIGestureRecognizerDelegate>
//custom delegate property
#property (nonatomic, weak) id <MFSideMenuContainerViewControllerViewDelegate> delegate1;
#end
and then call delegate method from below method of MFSideMenuContainerViewController.m
- (void)openLeftSideMenuCompletion:(void (^)(void))completion
{
if(!self.leftMenuViewController) return;
//call as below
[self.delegate1 swipedLeftSidemenu:YES];
//below is default thirdparty implementation
[self.menuContainerView bringSubviewToFront:[self.leftMenuViewController view]];
[self setCenterViewControllerOffset:self.leftMenuWidth animated:YES completion:completion];
}
Delegate method used in another viewcontroller class to detect side menu is open or closed for that I had implemented below code
in DemoViewController.h file
#import "MFSideMenuContainerViewController.h"
#interface DemoViewController : UIViewController <ContactViewDelegate,MFSideMenuContainerViewControllerViewDelegate>
in DemoViewController.m file
- (void)viewDidLoad
{
[super viewDidLoad];
MFSideMenuContainerViewController *vc2 = [[MFSideMenuContainerViewController alloc] init];
vc2.delegate1 = self;
}
//DelegateMethod Implementation
-(void)swipedLeftSidemenu:(BOOL)isOpen
{
if(isOpen)
{
// code is here if side menu is open by swiping right
}
else
{
// code is here if side menu is closed by swiping left
}
}
Now I having issue is above method in DemoViewController.m file is never called although sidemenu can be swiped left or right.Can any one guide how I can detect side menu is open or close from DemoViewController class? Or Why this method is never called ?
My appdelegate.m files having below code only
#interface AppDelegate ()
#end
#implementation AppDelegate
- (DemoViewController *)demoController
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DemoViewController *demoController = [storyboard instantiateViewControllerWithIdentifier:#"DemoViewController"];
return demoController;
}
- (UINavigationController *)navigationController
{
return [[UINavigationController alloc]
initWithRootViewController:[self demoController]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SideMenuViewController *leftMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"SideMenuViewController"];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:[self navigationController]
leftMenuViewController:leftMenuViewController
rightMenuViewController:nil];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}
Update your code In your AppDelegate.h file
#property (strong, nonatomic) UIWindow *window;
#property(strong,nonatomic)ViewController *viewController;
#property(strong,nonatomic)LeftSideViewController *leftSideViewController;
#property(strong,nonatomic)MFSideMenuContainerViewController *container;
#property(strong,nonatomic)UINavigationController *navigationController;
#end
and in your AppDelegate.m file update your code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.container = [MFSideMenuContainerViewController containerWithCenterViewController:self.navigationController leftMenuViewController:_leftSideViewController rightMenuViewController:nil];
self.window setRootViewController:_container];
[self.window makeKeyAndVisible];
return YES;
}
Instead of delegate method I had replaced it with NotoficationCenter Observer method and that code is as below :
STEP-1
in ThirdParty class MFSideMenuContainerViewController.m as
- (void)openLeftSideMenuCompletion:(void (^)(void))completion
{
if(!self.leftMenuViewController) return;
NSDictionary* userInfo = #{#"isOpen": #"1"};
[[NSNotificationCenter defaultCenter] postNotificationName:#"swipeClassDelegateMethod" object:nil userInfo:userInfo];
//below is default thirdparty implementation
[self.menuContainerView bringSubviewToFront:[self.leftMenuViewController view]];
[self setCenterViewControllerOffset:self.leftMenuWidth animated:YES completion:completion];
}
STEP-2 : then I called below code from my DemoViewController.m file viewDidLoad method
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(swipeClassDelegateMethod:)name:#"swipeClassDelegateMethod" object:nil];
STEP-3 : Finally I implemented this function with necessary code as below in same DemoViewController.m class
-(void)swipeClassDelegateMethod:(NSNotification*)notification
{
}
Related
I am new working with Objective C, I have good knowledge with storyboards but now I try to change the root view controller of a UINavigationController by code, it happens that it depends on a variable, I will set the root, but I don't know how to configure this action besides what I have my custom class for my UINavigationControllers. I hope you can help me.Keep in mind that this view is the 3rd screen in the app.
#interface PCCatalogNavigationController () <UINavigationControllerDelegate>
#end
#implementation PCCatalogNavigationController
- (void)viewDidLoad{
if (status == 1){
//TableViewViewController
} else {
//Optional StarViewController
}
}
#end
inside Your App delegate just add Property of you Navigation Controller
#property (nonatomic , strong) UINavigationController *navigationController;
and Inside
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TableViewViewController *TableViewView = [[UIStoryboard
storyboardWithName:#"Main" bundle:nil]
instantiateViewControllerWithIdentifier:#"TableViewViewController"];
StarViewController *StarView = [[UIStoryboard
storyboardWithName:#"Main" bundle:nil]
instantiateViewControllerWithIdentifier:#"StarViewController"];
if (status == 1){
self.navigationController =
[[UINavigationController alloc]
initWithRootViewController:TableViewView];
} else {
//Optional
StarViewController self.navigationController =
[[UINavigationController alloc]
initWithRootViewController:StarView];
}
}
instead UINavigationController you can use you customNavigationcontroller
I'm trying to implement my own custom UIWindow class. It's named SNWindow. I read that you have to implement your own getter method, and that's what I did but it never get's past "Point 1". It's infinitely logging "Point 1", showing a black screen on the iPhone.
AppDelegate.h
#import <UIKit/UIKit.h>
#import "SNWindow.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) SNWindow *window;
- (SNWindow *)window;
#end
AppDelegate.m
...
- (SNWindow *)window
{
NSLog(#"Point 1");
//
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
//
UIViewController *viewController = [storyboard instantiateInitialViewController];
//
_window = [[SNWindow alloc] init];
_window.rootViewController = viewController;
NSLog(#"Point 2");
return _window;
}
Any ideas on how to fix it?
Do not type the window property as SNWindow; type it as UIWindow, the normal way. Your code, in your App Delegate class, then needs to look like this:
- (UIWindow*) window {
UIWindow* w = self->_window;
if (!w) {
w = [[SNWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self->_window = w;
}
return w;
}
The rest of the App Delegate, such as application:didFinishLaunching..., should just go ahead and do what it would normally do (which might be no more than return YES).
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
I have one problem. in my app (it is tabbed style), I have one viewcontroller with some text and second with table view (RSS reader). When I have just the RSS and it is set to single view app, subview form rss works, but when I set up the tabbed app and click to some post in table view, subview didnt show up... Can anybody help me please?
Here are my codes:
AppDelegate.h
#import <UIKit/UIKit.h>
#interface MWFeedParserAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
AppDelegate.m
#import "MWFeedParserAppDelegate.h"
#import "ViewController1.h"
#import "RootViewController.h"
#implementation MWFeedParserAppDelegate
#synthesize window;
#synthesize navigationController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
UITabBarController *tbc = [[UITabBarController alloc]init];
ViewController1 *vc1 = [[ViewController1 alloc]init];
RootViewController *vc2 = [[RootViewController alloc]init];
[vc1.tabBarItem setTitle:#"Tab1"];
[vc2.tabBarItem setTitle:#"Tab2"];
[tbc setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil]];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
[window setRootViewController:tbc];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}
#end
From the dealloc, I see you are not using arc.
You have some memory leaks; be sure to release vc1 and vc2 in your didFinishLaunchingWithOptions, the tab bar controller will retain them.
You probably don't need navigationController property, recommend you delete that until you know you'll need it.
I think you'll want to add your RSS view (vc2?) to a nav controller before adding to the tab bar controller like this:
[tbc setViewControllers:[NSArray arrayWithObjects:vc1, [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease], nil]];
And delete this line:
[window addSubview:[navigationController view]];
Best of luck!!
edit Spelled out a tad more:
ViewController1 *vc1 = [[[ViewController1 alloc] init] autorelease];
RootViewController *vc2 = [[[RootViewController alloc] init] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease];
UITabBarController *tbc = [[[UITabBarController alloc] init] autorelease];
[tbc setViewControllers:#[vc1, navController]];
[window makeKeyAndVisible];
[window setRootViewController:tbc];
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
XCode 4.5.1, Application windows are expected to have a root view controller at the end of application launch
I'm a total noob in IOS app developing.
I use Xcode 4.5.1 with no storyboard.
I'm upgrading an IOS 4 app because it fails to run correctly on IOS 6 devices.
main view containing a question and five answers run once and stops there after user commit by pressing a button with the wanted answer, it should then reload itself with a new question and a new set of questions.
I get the infamous "Application windows are expected to have a root view controller at the end of application launch" in log output.
I've read and tried all comments and solutions in 7520971 but to no avail... still getting error and it seems to prevent me to load the view correctly.
here's what in my appDelegate.h
/*
* AnimViewAppDelegate.h
* AnimView
*
* Created by Administrateur local on 11-01-19.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*
*/
#import <UIKit/UIKit.h>
#import "RootNavigationController.h"
#interface PPScaleAppDelegate : NSObject <UIScrollViewDelegate> {
UIWindow *window;
RootNavigationController *RootNavigationViewController;
}
#property (nonatomic, retain) UIWindow *window;
#property (nonatomic, retain) RootNavigationController *RootNavigationViewController;
#end
my appDelegate.m
//
// AnimViewAppDelegate.m
// AnimView
//
// Created by Administrateur local on 11-01-19.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "PPScaleAppDelegate.h"
#import "QuestionView.h"
#implementation PPScaleAppDelegate
#synthesize window;
#synthesize RootNavigationViewController;
//- (void)applicationDidFinishLaunching:(UIApplication *)application {
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Create the main screen
//CGRect frame = [[UIScreen mainScreen] bounds];
//self.window = [[UIWindow alloc] initWithFrame:frame];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //2012
//Create the main view controller
RootNavigationViewController = [[RootNavigationController alloc] initWithNibName:NULL bundle:NULL];
//[window addSubview:RootNavigationViewController.view];
[self.window setRootViewController:RootNavigationViewController];
//Show the main window
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[window release];
[super dealloc];
}
#end
.h
//
// RootNavigationController.h
// IPhonePPS
//
// Created by Administrateur local on 11-02-11.
// Copyright 2011 Le Groupe CDGI Inc. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "QuestionView.h"
#import "ResultView.h"
#import "ResultTableView.h"
#interface RootNavigationController : UINavigationController {
QuestionView *QuestionViewController;
ResultView *ResultViewController;
ResultTableView *ResultTableViewController;
}
#property(nonatomic, assign) QuestionView *QuestionViewController;
#property(nonatomic, assign) ResultView *ResultViewController;
#property(nonatomic, assign) ResultTableView *ResultTableViewController;
-(void)switchToResultMode:(QuestionPath *)QuestionPath;
-(void)switchToResultTableMode;
-(void)switchBack:(BOOL)Reset;
#end
.m
//
// RootNavigationController.m
// IPhonePPS
//
// Created by Administrateur local on 11-02-11.
// Copyright 2011 Le Groupe CDGI Inc. All rights reserved.
//
#import "RootNavigationController.h"
#implementation RootNavigationController
#synthesize QuestionViewController, ResultViewController, ResultTableViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code.
QuestionViewController = [[QuestionView alloc] initWithNibName:NULL bundle:NULL];
ResultViewController = [[ResultView alloc] initWithNibName:NULL bundle:NULL];
ResultTableViewController = [[ResultTableView alloc] initWithNibName:NULL bundle:NULL];
//Set the navigation bar hidden
[self setNavigationBarHidden:YES];
//Push the question view on the stack
[self pushViewController:self.QuestionViewController animated:YES];
}
return self;
}
- (void)dealloc {
[super dealloc];
}
-(void)switchToResultMode:(QuestionPath *)QuestionPath {
[self pushViewController:ResultViewController animated:YES];
[ResultViewController setQuestionPath:QuestionPath];
}
-(void)switchToResultTableMode {
[self pushViewController:ResultTableViewController animated:YES];
}
-(void)switchBack:(BOOL)Reset{
if(Reset){
if([self.viewControllers count] == 3){
[self popToRootViewControllerAnimated:YES];
}else {
[self popViewControllerAnimated:YES];
}
[QuestionViewController resetAnswers];
}else {
[self popViewControllerAnimated:YES];
}
}
//-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
// if([self visibleViewController] == self.ResultTableViewController || toInterfaceOrientation == UIInterfaceOrientationPortrait){
// return YES;
// }else {
// return NO;
// }
//}
- (BOOL) shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
#end
spent two complete days trying to debug this but I give up and would really appreciate your help with this issue
PR
It would be better to just use [[alloc] init] if you don't have a nib for your navigation controller. Also, your navigation controller should be initialized with its own rootViewcontroller. I don't know which one you want to be first, but it should look something like this:
MyFirstViewControllerClass *rootVC = [MyFirstViewControllerClass alloc] initWithNibName:#"MyFirstViewController" bundle:nil];
RootNavigationController *nav = [[RootNavigationController alloc]initWithRootViewController:rootVC];
self.window.rootViewController = nav;
You're calling [self.window setRootViewController:RootNavigationViewController]; Notice - setRootViewController. It wants a viewController. Your RootNavigationViewController is a NavigationController as referenced here #interface RootNavigationController : UINavigationController not a viewController.
It looks like you should do something like this
RootNavigationViewController = [[RootNavigationController alloc] initWithNibName:NULL bundle:NULL];
[window makeKeyAndVisible];
[window addSubview:RootNavigationViewController.view];
(referenced from Programmatically build / navigate a Navigation Controller)
I'm not sure if using
[self pushViewController:self.QuestionViewController animated:YES];
in the RootNavigationViewController is the same as doing something like this
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
But that should point you in a good direction to debug your issue.
You should init your RootNavigationController with a rootViewController first :
[RootNavigationController initWithRootViewcontroller:QuestionViewController];
You can find this from the UINavigationController reference :
Because the UINavigationController class inherits from the
UIViewController class, navigation controllers have their own view
that is accessible through the view property. When deploying a
navigation interface, you must install this view as the root of
whatever view hierarchy you are creating.
For instance, in -(void)switchBack:(BOOL)Reset; you popToRootViewController without even having set it.