Trying to create a variable data level tableview structure - ios

I have a tableviewcontroller embedded in a navigationcontroller which is part of a tabbarcontroller.
When the initial tableviewcontroller is shown, it lists items. Some of these items may have subitems which may also have subitems, and some may not. In other words, the levels deep each item has is variable.
I'm trying to have the tableview controller instantiate a new instance of itself when a user clicks on an item using this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
ProtocolViewController *newVC = (ProtocolViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"ProtocolView"];
[self.navigationController pushViewController:newVC animated:YES];
}
But, it keeps crashing when I press an item in the tableviewcontroller (initial) and highlighting the storyboard line. What's going on? Each level will have different data, which I think I can get figured out how to load different data by passing some sort of reference as to what data it should be showing, but right now, I'm just trying to get it to push a new instance of the same tableviewcontroller over and over.
any help is appreciated....

Well... I guess it was just the pointer code to the storyboard that was jacked up because either of these ways of referencing the storyboard makes this work.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:#"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];
-or-
UIStoryboard *mainStoryboard = self.storyboard;
:)

Related

self.navigationController pushViewController: animated: gives me a black screen

I'm using Objective-C. I have this code in my .m file.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
detailViewController *detail = [[detailViewController alloc]init];
[self.navigationController pushViewController:detail animated:YES];
}
And I created my Detail View Controller Scene in storyboard. It looks like this [
And every time I run this in my simulator and tap on one of the tableview cell, it pushes the screen to a complete black screen like this:
Can someone help me? I would thank you very much!
Yes it will give a blank screen, because you haven't attached any visual interpretation on detailViewController class.
Use this method
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
DetailViewController *vc = [sb instantiateViewControllerWithIdentifier:#"detailViewController"];
[self.navigationController pushViewController:vc animated:YES];
You will have to set storyboard id by clicking on view controller and set storyboard id as shown in picture
Note : for above pic replace Storyboard id "MessagingVC" with "detailViewController"
When using a storyboard, you don't want to use alloc+init to create the view controller. Instead, you need to assign a Storyboard ID in Interface Builder and then use:
[self.storyboard instantiateViewControllerWithIdentifier:my_identifier];
A better option may be to set a push segue on the table view controller. This will cause the instantiation and push to happen automatically. You can override prepareForSegue:sender: to set properties, if needed.

Dynamically build a menu with a TableViewController in Objective-C

I am quite new to Objective-C and Xcode so I don't really know how to handle a dynamic menu with TableViews.
I am generating Table Rows and want to load an other ViewController by clicking at the rows.
I have tried to link the row to a NavigationController and wanted to load the furthers views there with:
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"Fremdbearbeitung"];
[self presentViewController:vc animated:YES completion:nil];
but I have problems that the view is not in the view hierarchy at the moment. Also I don't know to tell there dynamically which instantiateViewControllerWithIdentifier should be loaded by passing information from the rows.
Can you give me tips how to handle that best practice?
Can I tell the table rows at they are initialized where they should linked when they are clicked?
Thanks in advance.
EDIT: Found a way that works for me:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
testclass *test = [self.array objectAtIndex:indexPath.row];
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:test.second];
[self.navigationController pushViewController:vc animated:YES];
}
So i can dynamically go to the view with identifier which i gave to the function in my object test and property second.
I don't need a extra navigation controller for that, because i can do that in my main TableView.
Is this a good way?
You want to push the viewController and not present it. It will push the view controller onto the navigation controller’s stack and updates the display.
Read the Concepts clearly again. Also Learn about Storyboards and segues.
Also the ViewController Programming Guide.
Also you will find tons of tutorials for all the basics you want to learn, use Google like crazy..!
Edit : Using Storyboard Segues

Calling storyboard from UIViewcontroller

I don't know if this is possible or not but I wrote a project using xibs and want to modally switch to a storyboard project.
I know that to switch VC's you do something like
NewViewController *NewVC = [[NewViewController alloc] init];
[self presentModalViewController:NewVC animated:YES];
upon clicking a button, that I'll call "switch"
There is another project using solely storyboards that I want to call using "switch"
How do I go about doing this? Is it possible to do this?
Yes it is possible. You can do something like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YourStoryboardName" bundle:nil];
MainMenuViewController *mainmenuvc = [storyboard instantiateViewControllerWithIdentifier:#"mainMenuViewController"];
[self.navigationController pushViewController:mainmenuvc animated:YES];
EDIT
If I understand your comment, you want to change the root view controller. You can do so by:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"RVCStoryboard" bundle:nil];
MainMenuViewController *mainmenuvc = [storyboard instantiateViewControllerWithIdentifier:#"mainMenuViewController"];
[[UIApplication sharedApplication].delegate].window.rootViewController = mainmenuvc;
Please note that for this to work, in your RVCStoryboard you have to select the viewcontroller you are trying to load, and assign a name to it so that the instantiateViewControllerWithIdentifier: method will work. Check the attached image, the field "Storyboard ID":

Cannot load viewController using MFSidemenu

I am building an app using storyboard. I am using the MFSidemenu library for creating a sidemenu like facebook app. the side menu appears properly but when i tap a cell on the side menu, the menu doesnt go away and it doesnt load another view controller. Please note that i am using storyboard.
Thank you.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
AlbumsTableVIewController *AlbumsViewController = [storyboard instantiateViewControllerWithIdentifier:#"Albums"];
AlbumsViewController.title=[NSString stringWithFormat:#"Demo Controller #%d-%d", indexPath.section, indexPath.row];
NSArray *controllers = [NSArray arrayWithObject:AlbumsViewController];
self.sideMenu.navigationController.viewControllers = controllers;
[self.sideMenu setMenuState:MFSideMenuStateClosed];
}
you have to implement delegate method to your MainMenu Controller, because if u place your code like above, it will change your sideMenu not ur MainMenu. i am working with storyboard here. this is my code on my MainMenuController
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UINavigationController *navigationController = (UINavigationController *)self.navigationController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
UITableViewController *SideMenu=(UITableViewController *)leftSideMenuViewController;
SideMenu.delegate=self; //here is the delegate
[MFSideMenu menuWithNavigationController:navigationController
leftSideMenuController:SideMenu
rightSideMenuController:nil];
so you need to hook up with your viewController from here, navigating thru delegate pattern.

Linking a new viewcontroller to Storyboard?

There is probably a simple solution but I can't figure it out.
I am using storyboards for the interface.
I start with a tab bar controller, but before the user is allowed to use the app the user has to authenticate himself trough a loginview which is modally pushed at the start.
I want to configure the loginview at the same storyboard, but I can't seam to figure out how to link the view controller at the storyboard and my code.
What I have done:
Create a new UIViewController subclass trough file > new > new file.
Drag a new UIViewController in the story board
Set the class in the custom class tab
drags a UILabel for test purpose.
run
No label...
Pull on a new UIViewController that will act as the login view controller onto the MainStoryboard. In the attribute inspector change the identifier to LoginViewController (or something appropriate). Then add
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
to the First view controller and the login screen will be loaded from your storyboard and presented.
Hope this helps.
The answer by Scott Sherwood above is most correct answer I found after lot of searching. Though very slight change as per new SDK (6.1), presentModalViewController shows deprecated.
Here is very small change to above answer.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:#"LoginView"];
[hvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:hvc animated:YES completion:nil];
I'm new in this field. But if the first view controller is a navigation view controller and its rootviewcontroller is a table view controller. If you want to push a view controller like the LoginViewController when you click the cell, and you also want to go back to the table view by using the navigation bar. I recommend this way:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
In this way, you can have the navigation.
By the way, I don't know why this kind of problem you asked will appear. I guess when the loginviewcontroller is created in the code, its view is not the view in the storyboard. If someone know the cause, please tell me! thanks!

Resources