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

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.

Related

how could i push view controller to navigation controller

I want to go from my Login Page to the MainMenuViewController but the MainMenuViewController is a navigation Controller.
I searched for a while but it still doesn't work correctly. Could anyone tell me what's going on?
My code:
MainMenuViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier: #"MainMenuViewController"];
[self presentViewController:vc animated:YES completion:nil];
What exactly is the issue? This will present a view controller that you choose, if you want to get the navigation view to show up correctly you will want to transition not to the MainMenuViewController, but the root view controller for that. When you look at the storyboard, you can see the navigation controller that segues to the MainMenuViewController, present that view controller instead of the MainMenuViewController.
Set the storyboard ID for your navigation controller and write the below code in the function.
UINavigationController * navigationController = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"SecondNavigationController"];
[self presentViewController:navigationController animated:YES completion:nil];
Hope this helps. :)

How to call a View Controller from not related View Controller programmatically?

as you see on the image, on the normal case I click the button1 to call the second view and then I click the button2 to call the last view, and in a particular scenario I want to click the button1 to call the last view.
I added this on the methode viewDidLoad:
if(condition)
[self performSegueWithIdentifier:#"thirdview" sender:self];
but it is not working, any help please.
thank you.
You need to add a segue from ViewController1 to Viewcontroller3.
But if you do this with many viewControllers then it becomes messy, I call it the spaguetti segues.
The alternative is to present that viewController modally.
Example:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
ViewController3 *viewController =
[storyboard instantiateViewControllerWithIdentifier:#"viewController"];
[self presentViewController:viewController animated:YES completion:nil];
(make sure you give ViewController3 a storyboardId)

Attempt to present a viewcontroller whose view is not in the window hierarchy

I have a tab bar application. My requirement is I select the 'scan' tab to scan the qr code and navigate/jump immediatley to another 'list' tab. Both 'scan' and 'list' tab are there in the viewControllers array in didFinishLaunchingWithOptions After referring this link, i don't think i need to set the delegate as both the tabs are already present in the hierarchy.
I get this warning in the following line
if(x)
{
listViewCntrl = [[ListViewController alloc] initWithNibName:#"ListViewController" bundle:nil];
listViewCntrl.getFlag = YES;
[self presentViewController:listViewCntrl animated:YES completion:Nil]; // I get the warning here
}
If I comment out the above code and add
[self.tabBarController setSelectedIndex:1];
then I would not be able to get the subView of the listViewController (set flag to show the subview) which i need to display inside the list tab after scanning.
App crashes if I add
[self.tabBarController setSelectedViewController:listViewCntrl];
So how do I display the listView's subview after scanning?
You can try this if you use storyboard:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController * destViewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:destViewController animated:YES];
You have to set an identifier for your controller in your storyboard.
ListViewController *listController = (ListViewController*)[self.tabController viewControllers][1];
listController.getFlag = 1;
[self.tabBarController setSelectedIndex:1];
The problem is that you're creating an entirely new ListViewController. You say you already have one in the tab controller - you don't need a new one.
You can't use your 3rd option because, again, the two ListViewController's are different objects (they may be of the same class, but they point to a different address).

Initiating a segue from uiview or uivewcontroller that is not on storyboard

How can I initiate segue from a custom UIView or custom UIViewController that is not on Storyboard? - they are created programmatically inside a parent UIViewController.
Although the destination UIViewController is on the storyboard.
Then just do it the old fashioned way. Instantiate it and present it modally or just push it if you are in a navigation controller. Hope this helps.
EDIT:
You can talk about segue only if it is in the storyboard. If Your source View Controller is not in it, you just present the next one as I said. You can instantiate your destination view controller from the storyboard:
MyViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self presentViewController:controller animated:YES];
From this point you will be "back in the storyboard" so you can perform segues in the destination View Controller.
First give storyboard id. Select the view controller than write a identifier(Storyboard ID)
Secondly, just open your .m file then write belove codes.
UIStoryboard * st = [UIStoryboard storyboardWithName:#"nameOfStoryboard" bundle:nil];
MyViewController *viewController = [st instantiateViewControllerWithIdentifier:#"YourStoryboardID"];
[self.navigationController pushViewController:viewController animated:YES];
Last line of code. Might also be
[self presentModalViewController:viewController animated:YES];

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