Dynamically build a menu with a TableViewController in Objective-C - ios

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

Related

Navigation from one view to another view in IOS (objective c)

I am new in IOS. I am confused in navigation.I have looked 3,4 methods to navigate from one view to another view controller.
First
DashboardViewController *dashboard = [self.storyboard instantiateViewControllerWithIdentifier:#"DashboardViewController"];
[self.navigationController pushViewController:dashboard animated:YES];
Second :- Using push segue on clicking button.
Third :- I'm not clear for it, That is array of view controller. In which we get the view controller and then navigate.
Fourth:-
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
UserDashboardViewController *users =
[storyboard instantiateViewControllerWithIdentifier:#"UserDashboardViewController"];
[self presentViewController:users
animated:YES
completion:nil];
I am confused which is best way for navigation, and most important how to navigate using array of view controllers. Please help me, Thanks.
This is the best way in my point of view.

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.

Trying to create a variable data level tableview structure

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;
:)

Passing data between the Tabs of a TabBarViewController

I have a UITabBarController based application and I want to pass data from one view to another. I am doing this in storyboard and I am just doing some testing, before bringing it into the main application.
I am just trying this with a NSString at the moment.
I am able to pass data to the VC in question when I use a modal transition using this code:
NSString *sendingString = #"This string has some content";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
NextViewController *nVC = (NextViewController *)[storyboard instantiateViewControllerWithIdentifier:#"goToNextVC"];
nVC.receivingString = sendingString;
[self presentViewController:nVC animated:YES completion:nil];
Now this pushes that VC up and passes it as I want it, but instead of pushing up the VC I want it to be pushed to another Tab Bar.
Now I can flick to the desired TabBar with this code:
self.tabBarController.selectedIndex = 1;
Where I get stuck is, how do I send data to this ViewController???
You could either subclass your TabBarController, and add a property to it, or create a singleton (e.g. DataManager), to which all your ViewControllers will have access. You can pass your data to it.

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