How to add BackButton Facility MFSlidemenu? - ios

I have implemented MFSideMenu in my project .It works great, but now i want to implement back button facility to every view.
I try this but not working:
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];

Here is the Solution
HomeView *Home = [[HomeView alloc]initWithNibName:#"HomeView" bundle:nil];
NSArray *controllers = [NSArray arrayWithObject:Home];
self.navigationController.viewControllers = controllers;

Related

How to replace Detail in UISplitViewController in Swift?

Usually I can replace my detail view of splitview by this code in Objective-C:
[self.splitViewController viewWillDisappear:YES];
NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
[viewControllerArray removeLastObject];
[viewControllerArray addObject:self.detailViewController];
[[self.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];
[self.splitViewController viewWillAppear:YES];
Try to rewrite it in Swift, but I even can't get the viewControllers in this code:
NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
Swift:
let test: UIViewController = (self.splitViewController?.viewControllers[1])!
It should have test.viewControllers , but it doesn't as seen in below pic
Does Swift handle the replace in splitview in different way?
This works for me but I am not sure if this is the best practice:
let detailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DetailNavigationViewController") as! UINavigationController
self.splitViewController?.viewControllers[1] = detailViewController
Only need to get the desired viewController and assign it to index 1 of splitViewController

How to change positions of UITabBarItems according to certain situations in the app, programmatically

I have 3 items in my UITabBar and my app runs 2 Languages(English and Arabic). I need to change the position of the items when the app is switched to Arabic since A Right to Left reading is required
ie. the Tab called "One Way" must appear at the right side and Multi City has to appear at the left side. Is there a way to do that?
I tried this method but That was not allowed
UITabBarItem *realItemOne=[self.tabBar.items objectAtIndex:0];
UITabBarItem *realItemTwo=[self.tabBar.items objectAtIndex:1];
UITabBarItem *realItemThree=[self.tabBar.items objectAtIndex:2];
if (_isEnglish) {
self.tabBar.items=[[NSArray alloc] initWithObjects:realItemOne,realItemTwo,realItemThree, nil];
}
else{
self.tabBar.items=[[NSArray alloc] initWithObjects:realItemThree,realItemTwo,realItemOne, nil];
}
Also Tried this
self.tabBar.items=[[self.tabBar.items reverseObjectEnumerator] allObjects];
Another method that I tried
UIViewController *viewControllerOne=[self.tabBarController.viewControllers objectAtIndex:0];
UIViewController *viewControllerTwo=[self.tabBarController.viewControllers objectAtIndex:1];
UIViewController *viewControllerThree=[self.tabBarController.viewControllers objectAtIndex:2];
if (_isEnglish) {
NSArray *englishOrder=[[NSArray alloc] initWithObjects:viewControllerOne,viewControllerTwo,viewControllerThree, nil];
[self.tabBarController setViewControllers:englishOrder];
}
else{
NSArray *arabicOrder=[[NSArray alloc] initWithObjects:viewControllerThree,viewControllerTwo,viewControllerOne, nil];
[self.tabBarController setViewControllers:arabicOrder];
}
Do not change items, change just text and image of tab item.
if (_isEnglish) { change just image and text on 0. and 2. tab } else { also just change image and text }
when the user tap on the item, you check for language and then do appropriate method. If user tap on 0. element and the language is english, you call oneWay method and if language is arabic you call multiCity
I hope this helps.
You can get the number of controllers in tabBarController in NSArray format as below:
NSArray *vcArray = self.tabBarController.viewControllers;
Now change the sequence of the objects of this "vcArray" as you want(for demo purpose I have used 3 controllers. you can put your logic whatever you want) and then re-assign "vcArray" to your tabBarController like below:
self.tabbarController.viewControllers = vcArray;
Actual code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *fvc = [[FirstViewController alloc] init];
SecondViewController *svc = [[SecondViewController alloc] init];
ThirdViewController *tvc = [[ThirdViewController alloc] init];
fvc.tabBarItem.title = #"First";
svc.tabBarItem.title = #"Second";
tvc.tabBarItem.title = #"Third";
_tbc = [[UITabBarController alloc] init];
_tbc.viewControllers = [NSArray arrayWithObjects:fvc, svc, tvc, nil];
self.window.rootViewController = _tbc;
[self.window makeKeyAndVisible];
return YES;
}
--------------- A method to swap controllers in tabViewController in AppDelegate -----------
NSArray *arr = self.tbc.viewControllers; //here self means appDelegate
self.tbc.viewControllers = [NSArray arrayWithObjects:arr[2], arr[1], arr[0], nil];
Hope this helps!
Attached images for your reference.
This worked for me:
Place this code in the TabBArController ViewWillAppear method
NSArray *actualItems= self.viewControllers;
NSArray* reversedArray = [[actualItems reverseObjectEnumerator] allObjects];
[self setViewControllers:reversedArray animated:YES];

Issue while remove viewController from navigation stack

I am making demo in which I have to remove some of viewControllers from the NavigationController, and for that I have implemented below code but it give me issue.
I have pushed VC1,VC2,VC3 and now I want to push VC4 and remove VC2...
ViewController4 *VC4=[[ViewController4 alloc]initWithNibName:#"ViewController4" bundle:nil];
[self.navigationController pushViewController:VC4 animated:YES];
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
for(UIViewController *objVC in viewControllers)
{
if([objVC isKindOfClass:[ViewController2 class]])
{
[viewControllers removeObjectIdenticalTo:objVC];
}
}
self.navigationController.viewControllers =viewControllers ;
This code works fine with iOS8 but in iOS7 with VC2 also VC3 removes automatically when I press the back button in VC4.
Even if I put below code the controller automatically removes from stack.
ViewController4 *VC4=[[ViewController4 alloc]initWithNibName:#"ViewController4" bundle:nil];
[self.navigationController pushViewController:VC4 animated:YES];
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
self.navigationController.viewControllers =viewControllers ;
Here is the fix, working fine in iOS7 and iOS8:
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
// Find the things to remove
NSMutableArray *toDelete = [NSMutableArray array];
for(UIViewController *objVC in viewControllers)
{
if([objVC isKindOfClass:[ViewController2 class]])
{
[toDelete addObject:objVC];
}
}
[viewControllers removeObjectsInArray:toDelete];
self.navigationController.viewControllers =viewControllers ;
ViewController4 *VC4=[[ViewController4 alloc]initWithNibName:#"ViewController4" bundle:nil];
[self.navigationController pushViewController:VC4 animated:YES];

How to set the Animation-Style from code?

I am replacing a view inside a navigation controller with the setViewControllers-method.
But i dont like the look of the transition. How can i set the animation-style when using the following code ?
UIViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:#"SuccessScreen"];
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
[viewControllers removeLastObject];
[viewControllers addObject:newVC];
[[self navigationController] setViewControllers:viewControllers animated:YES];
Something like
CATransition *yourAnimation=...;
[self.navigationController.view.layer addAnimation:yourAnimation forKey:nil]
[[self navigationController] setViewControllers:viewControllers animated:NO];

UIPopoverController with UIPageViewController shows no content

I try to show views of different view controllers within a popover controller's view. I tried for three hours now but without success. Basically this is what I've done:
- (IBAction)buttonTapped:(id)sender {
NSDictionary *options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey];
UIPageViewController *pageVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad"
bundle:[NSBundle mainBundle]];
UIViewController *firstVC = [sb instantiateViewControllerWithIdentifier:#"FirstTableViewController"];
UIViewController *secondVC = [sb instantiateViewControllerWithIdentifier:#"SecondViewController"];
NSArray *viewControllers = [[NSArray alloc] initWithObjects:firstVC, secondVC, nil];
[pageVC setViewControllers:viewControllers direction:UIPageViewControllerTransitionStylePageCurl animated:NO completion:nil];
UIButton *button = sender;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:pvc];
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
And this is what I get to see:
What am I missing here?
Ok, if someone encounters a similar problem, I solved it:
It seems that right after initialization you have to set exactly the number of view controllers that are needed at a time.
So, if you create a UIPageViewController with one page displayed at a time, put an array containing exactly(!) one view controller in setViewControllers:direction:animated:completion:. More than one caused my controller to display nothing. Other view controllers can later be added lazily by implementing the UIPageViewControllerDelegate and UIPageViewControllerDataSource protocols.

Resources