push from a side bar menu to another view controller in ios - iphone

I use in my application, the side bar menu like in facebook, so I have different cells in this menu, what I want is to when click on a cell push me to another view controller. I face a problem here which is: the menu is a table view which I don't have it in storyboard, I use some classes from this site:github
and I've stucked here, in my application I use a storyboard, but this menu is programmed with code, and doesn't have a view in stroyboard,
in the didSelectRowAtIndexPath method: I use like this
for(int j=0; j< 9 ; j++)
{
if(indexPath.row == j)
{
DetailsSidebarViewController *essayView = [[DetailsSidebarViewController alloc] init];
essayView.detailItem = [jsonResults objectAtIndex:j];
NSLog(#"%#=%d",essayView.detailItem,j);
}
}
and I create a DetailsSidebarViewController as the new view controller when I push from menu item. in this class, I create a method to configure the view, and just I echo the result:
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
NSLog(#" %# ", self.detailItem);
}
}
the result is true like I want, but I want to push to another view controller, in fact nothing is happened when I click on an item into the menu.
How can I create the new view controller in storyboard? if the menu has not a storyboard, and how can I connect them with segues?
In fact, I am blue with it, please help!!

You should make an instance of the App Delegate class in the Side Menu view controller, and push it from there. You must have a reference to the Side Menu View and a reference to the Center View! Import your App Delegate into the Side Menu controller, and in the didSelectRowAtIndexPath, use:
DetailsSidebarViewController *essayView = [[DetailsSidebarViewController alloc] init];
essayView.detailItem = [jsonResults objectAtIndex:indexPath.row]; // note that you don't need the for loop to know what object you need
AppDelegate* myAppDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[myAppDelegate.sideMenuViewController toggleSideMenu]; // the idea here is that you have to close the side menu, i.e. it must dissapear
[myAppDelegate.centerViewController.navigationController pushViewController:essayView animated:YES];
This is all the code you need.
If I may, as a piece of advice, use Mike Frederik's MFSideMenu instead of the library you're using. In my opinion, it has a more simple and a more straightforward implementation, just look into the README file, everything is explained pretty simple, and it has exactly the code you need!
Hope this helps, good luck!

Related

how to populate an array or any object from one view controller to another when we present the view using story board

I have implemented my iphone app using story board
In my app *i need to present a view from one view to another view not the pus*h,
I have created a screen in story board xib for the second view .
i set up the connection for button in the first view to the second view with 'present' not pust.
Now i need to populate an array to the second view which was presented
i tried following ways but not work out
1:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender
{
//Populate detail to BuyView mail page
if ([segue.id
entifier isEqualToString:#"BuyView"])
{
BuyViewController *destViewController = segue.destinationViewController;
destViewController.itemDetailsArray = [_itemDetailsArray objectAtIndex:sender.tag] ;
}
}
2:
- (IBAction)buyItemClick:(UIButton *)sender
{
BuyViewController *destViewController = [[BuyViewController alloc] init];
destViewController.itemDetailsArray = [_itemDetailsArray objectAtIndex:sender.tag] ;
}
On top of what #rdelmar has stated you are passing an object of which may or may not be an array.
Which in turn would mean
destViewController.itemDetailsArray
Would be pointing to an object which may not be an array.
I think what you want to do is
destViewController.itemDetailsArray = _itemDetailsArray;

KVO with two UIButtons

I have a custom UITableViewCell subclass that gets presented in a UITableView in a UIPopoverController. The UIPopoverController is presented from a UIBarButtonItem.
When selections in the UITableViewCell are made, I send a NSNotification to the UIViewController class that is presenting the UIPopoverController. The selections in the table update my view in my viewController.
Now, there is a requirement to have another UIBarButtonItem that does EXACTLY the same thing as one of the buttons in the UITableViewCell in the popover. Basically the use case is that this feature seems to be the most commonly used feature in our popover and they want an easy way to just turn it on and off from another button.
So what I did was create a new UIBarButtonItem, and have a target attached to it:
- (void)FilterOn:(id)sender {
isFilterOn = !isFilterOn;
if ([sender isKindOfClass:[UIBarButtonItem class]]) {
UIBarButtonItem *filter = (UIBarButtonItem *)sender;
if (isFilterOn) {
[filter setTitle:#"Filter On"];
[self DoFilter];
}
else {
[aboutMeBBI setTitle:#"Filter Off"];
[self ClearFilter];
}
}
}
So this part works. It updates the model, the title of the button changes. The problem is, let's say I turn filter on, then in the popover, I turn the filter off. I pass the notification to my viewController class to its normal update method that handles filtering and a bunch of other stuff. I added this simple snippet to when the filter button is pressed from the popover:
- (void)FilterSortOptionDidSelect:(NSNotification *)notification {
NSDictionary *userDict = [notification userInfo];
NSInteger theTag = [[userDict objectForKey:#"CellTag"] integerValue];
switch (theTag) {
case FILTER: {
// update Model
[self.FilterBarButtonItem setTitle:#"Filter off"];
}
break;
default:
break;
}
}
This isn't quite what I want though since if I click back on the filter button since for starters, if I click back on the Filter button, it'll still say Filter Off, and do the action for Filter Off, and then if I click it again, it'll do Filter on actions. I try to fake it in the switch statement by changing the title, but that isn't really what I want to do. I read a little about key value observing and I wasn't sure if I could use something like that here, to register my buttons state with that of a button in a UITableViewCell subclass in a UIPopoverController. If anyone has any ideas that would be great. Thanks.

ViewController Hierarchy getting 'lost'

I've got a view hierarchy which is setup (programmatically) as follows:
Window.root = TabBarController-->UINavigationControllers-->UIViewControllers
I presume that's rather standard. Here's my problem:
I'm on Tab A. I want to navigate to Tab B, and call a method on the visibleViewController on Tab B.
// View Changes OK
[AppDelegate.tabBarController setSelectedIndex:tabB];
// nav = 0x387ABF i.e. Valid Address
UINavigationController *nav = (UINavigationController*)[AppDelegate.tabBarController selectedViewController];
// The problem:
nav.viewControllers; // this is nil
nav.topViewController; // as is this
nav.visibleViewContorller; // this too.
Even if I put the calls to nav.viewControllers in a separate method which is called from the Main Thread, I still get 0x0/nil.
What am I doing wrong?
A follow-up question is:
How can I pass information from one ViewController to another when changing tabs? (If I can't call methods on VC's from tabA to tabB)
I have a feeling it is related to my question here.
You should store the information in a common place, either a singleton or as you are a beginner just make a class and pass it down.
sharedDataObject = [[MySharedDataObject alloc] init];
firstViewController.myDataObject = sharedDataObject;
secondViewController.myDataObject = sharedDataObject;

How to add a badge to a UITabBar that is customizable?

I am adding a badge to my UITabBarController's UITabBar as such:
UITabBarItem *tbi = (UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:1];
tbi.badgeValue = #"2";
However, my UITabBarController is customizeable, so the index may change. How can I make sure the badge gets applied to the correct UITabBarItem?
One suggestion you might consider is setting a tag on each tab bar item. You can do this in Interface Builder or when you create the item by code. You can then loop through the view controllers in the tab bar controller looking for the one with the tab bar item you are interested in. For example:
// #define MyTabBarItemTag 999
for (UIViewController *viewController in stTabBarController.viewControllers) {
if (viewController.tabBarItem.tag == MyTabBarItemTag) {
viewController.tabBarItem.badgeValue = #"2";
}
}
UITabBarItem *tbi = (UITabBarItem *)self.tabController.selectedViewController.tabBarItem;
tbi.badgeValue = #"New";
Also works.
Swift version:
self.tabBarController?.selectedViewController?.tabBarItem.badgeValue="12";
I'd use an NSMutableDictionary property on the class that owns the tab bar controller, associating tab names with positions, and a method to retrieve by name:
-(UITabBarItem*)getTabByName:(NSString*)tabName {
return [stTabBarController.tabBar.items objectAtIndex:[[tabDict valueForKey:tabName] intValue]];
}
Initialize the dictionary in your setup code for each tab, since you know the tab index at that time:
[tabDict setValue:[stTabBarController.tabBar.items objectAtIndex:1] forKey:#"myTabName"];
Keep a reference to the tab bar item that you want to modify.
EDIT as a response to your code request:
I believe that there is a single place in your app where you update the badges on the tab bar items. Just add an array of tab bar items (or separate tab bar items) as a member(s) of that class (+ properties if needed) and update the items directly without fetching from the current tab bar items list ((UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:1];).
For instance, if you decide to keep references to the tab bar items directly (without an array) then the code might look like that:
// Put the next code right after initiating the tab bar and/or after adding new tab bar items to it...
self.newsTabBarItem = (UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:1];
self.friendsTabBarItem = (UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:2];
// etc.

Act on click of a button on the Nav Bar for moreNavigationController -- Can't pushviewcontroller

Okay, here is my issue: My app has a display of categories in the tab bar at the bottom of the iPhoneOS screen. This only allows 5 categories before it presents the MORE button. I have over 25 (please do not answer this by saying: "Rethink your application...etc" -- that was rudely said before. They are food, drink, etc categories and cannot be changed). I want to allow the user to put their favorites on the home page. The Apple moreNavigationController editing system only allows 20 tab bar items to be rearranged due to space constraints on the editing page. This is not enough so i need to implement my own Editing screen. I set the rightBarButtonItem to nil and created my own. Using NSLog, i can see the "click" happens when clicking the EDIT button, but I cannot push using pushViewController. Nothing happens. I think it has something to do with the navigationController I am addressing...but i am not sure. ps: This all happens in my App Delegate which DOES act as both UITabBarControllerDelegate & UINavigationControllerDelegate.
I tried to do the following:
- ( void )navigationController:( UINavigationController * )navigationController_local willShowViewController:( UIViewController * )viewController_local animated:( BOOL )animated
{
UIViewController * currentController = navigationController_local.visibleViewController;
UIViewController * nextController = viewController_local;
// Do whatever here.
NSLog(#"Nav contoller willShowViewController fired\n'%#'\n'%#'\nThere are currently: %d views on the stack\n",currentController,nextController,[self.navigationController.viewControllers count]);
if ( [nextController isKindOfClass:NSClassFromString(#"UIMoreListController")])
{
UINavigationBar *morenavbar = navigationController_local.navigationBar;
UINavigationItem *morenavitem = morenavbar.topItem;
morenavitem.rightBarButtonItem = nil;
NSLog(#"Is a UIMoreListController\n");
UIBarButtonItem *editTabBarButton = [[UIBarButtonItem alloc]
initWithTitle:#"Edit"
style:UIBarButtonItemStylePlain
target:self
action:#selector(editTabBar:)];
morenavitem.rightBarButtonItem = editTabBarButton;
[editTabBarButton release];
}
}
This works to place an EDIT button at the top right of the screen -- mimicking Apple's look and feel... but when that button is clicked, you cannot exit the darn moreNavigationController.
I have tried many things. UIAlerts work, etc...but pushing (or popping -- even popping to root view) a view controller on the stack does not.
- (void) editTabBar:(id)sender {
NSLog(#"clicked edit tabbar\n");
NSLog(#"Total count of controllers: %d\n",[self.navigationController.viewControllers count]);
TabBarViewController *tabBarViewController2 = [[TabBarViewController alloc] initWithNibName:#"TabBarView" bundle:nil];
tabBarViewController2.navigationItem.title=#"Edit Tab Bar";
[self.navigationController pushViewController:tabBarViewController2 animated:YES];
[tabBarViewController2 release];
NSLog(#"finished edit tabbar\n");
}
If you click the edit button on the moreNavigationController's display page, you get the log entries like expected AND (this is strange) the views on the stack climbs -- but no page change occurs. I marked it down to not using the correct navigation controller...but I am lost on how to find which one TO use.
this is a weird one too. In the edit function if i just do this:
- (void) editTabBar:(id)sender {
self.tabBarController.selectedIndex = 0;
}
It DOES take me home (to tabbarcontroller 0)
BUT doing this:
- (void) editTabBar:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
does not work.
Does the moreNavigationController have some special quality that screws with the rest of the system?
I would try reimplementing the whole "More" functionality from scratch. In other words, store the four home tabs in your user defaults and add a dummy fifth tab that switches to your own complete reimplementation of the more view controller stack.
You could even write a lightweight subclass of UITabBarController that handled this for you.
UITabBarController is evil, so I wouldn't be at all surprised if MoreController had some special properties, too.
I have had success intercepting the More Controller in shouldSelectViewController to change the data source; you may be able to find some workaround there.
PS I am inclined to agree that you could consider redesigning your app so that you didn't need an unlimited number of viewControllers attached to the tab bar just to select categories; you might have better luck using a tool bar with a single, scrollable, custom view in it. If that's really the best way of picking categories for your app, of course.