I have two views, actualView and nextView. Actually in the actualView, I would like to open the nextView and passing some properties (for example nextView.date = actualView.date) and killing the actualView.
I would like to know if it is possible ?
I used that code :
NSLog(#"Views : %#",self.navigationController.viewControllers);
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
FormViewController *formView = (FormViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"formView"];
formView.delegate = self;
[self presentViewController:formView
animated:YES
completion:nil];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
When I use self.navigationController.viewControllers in this "actualView", it knows where it is : ,
but when I use self.navigationController.viewControllers in the actualView, it is (null)...
So may be a problem with the delegate ?
You can connect the two views with a present modally segue which will kill the actual view and go to the next view.
It is explained quite nicely here https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/Chapters/StoryboardSegue.html
As for passing values from one view to another check out this function:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "segue identifier connecting the 2 views") {
let viewController = segue.destinationViewController as! UINavigationController
let detailview = viewController.topViewController as! nextViewViewController
detailview.element defined in nextview = value to send
}
There are a lot of examples you can find online with performing segue with identifier. It is a very common question.
Read something about removeFromSuperview() in Apple documentation
You can use a modal Segue.
This way actualView will be deallocated when you move to nextView.
I didn't find an easier way than this :
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *sourceViewController = self;
UIViewController *destinationController = [mainStoryboard instantiateViewControllerWithIdentifier: #"View3"];
UINavigationController *navigationController = sourceViewController.navigationController;
// Pop to root view controller (not animated) before pushing
[navigationController popToRootViewControllerAnimated:NO];
[navigationController pushViewController:destinationController animated:YES];
Related
Team,
I have two storyboard.
One For authentication
And another for My application Dashboard.
For Authentication Storyboard The initialisation screen is loginScreen.
After login successfully i am loading Dashboard Storyboard.
For dashboard storyboard the initial screen is MainViewController.
Here I am implemented logout from in DashboardStoryboard. So now i want to switch back to my Authentication Storyboard.
Here its going back to loginScreen.
But I thing its not proper way for implementing.
It will be more helpful is there a way I can do better?
-(void)logout{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Authentication" bundle: nil];
LoginViewScreenController *loginViewScreenController = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewScreenController"];
[self.navigationController pushViewController: loginViewScreenController animated:NO];
}
Your feedback is highly appreciated.
It's Very easy using segue and Storyboard Reference. Please follwo steps and screenshots.
Step-1)
Drag and drop Storyboard Reference from Object Library in First(Main) Story board.
Step-2)
Add segue from your source ViewController to Storyboard reference.
Step-3)
Select another(second) storyboard.
Reference ID: StoryboardID of your destinationViewControler(second View Controller) which is available in Second.Storyboard
-(void)logout
{
UIViewController *aVCObj = [[UIApplication sharedApplication]delegate].window.rootViewController;
if ([aVCObj isKindOfClass:[UINavigationController class]]) {
UINavigationController *aNavController = (UINavigationController *)aVCObj;
[aNavController popToRootViewControllerAnimated:YES];
}
}
Here is a trick to do this by setting up a key true using NsuserDefaults when user logged in otherwise false and navigate your application when starts accordingly using presentViewController method without animation so the user will not get any option to go back previous vc.
Have a look below code illustrating above sentence:
if ([[[NSUserDefaults standardUserDefaults] valueForKey:#"isloggedIn"] isEqualToString:#"true"]) {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"loginView"];
[self presentViewController:vc animated:NO completion:nil];
}else{ // when logout
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"logoutView"];
[self presentViewController:vc animated:NO completion:nil];
}
If you need to apply some effects when vc appear just ad these two lines before presentViewController method see:
[vc setModalPresentationStyle:UIModalPresentationCustom];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
Note: set the key false when user logout.
let sb = UIStoryboard(name: "Main", bundle: nil)
let homeVC = sb.instantiateViewController(withIdentifier: "TabBarController")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = homeVC
I used code from this question Display UIViewController as Popup in iPhone it is working fine but I don't want to use segue or I can say I can't use segue because all the code is created programmatically, so I cant set segue on button.
I used this code but it didn't work
- (IBAction)open:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *newVC = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"Second"];
[ViewController setPresentationStyleForSelfController:self presentingController:newVC];
[self.navigationController pushViewController:newVC animated:YES];
}
Edit 1:
View after popup
You should check if this VC self.navigationController is not nil. and i think you want to presentViewController: not pushViewController:...
I have three View controller A,B,C.I have navigation controller attached to A view controller.In A i have some buttons,I have attached the button segue to B view controller.Onclick of the button i go to the B view controller.On B View controller i have UITableView on click of table view item i am launching the C view controller.below is the code for that
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
{
NSLog(#"first cell");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
[self presentViewController:vc animated:YES completion:nil];
}
else if(indexPath.row==1)
{
NSLog(#"second cell");
}
else if(indexPath.row==2)
{
NSLog(#"third cell");
}
}
But on C view controller the navigation Bar is not appearing.I think C view controller is not linked to the Navigation Controller.
You use navigationController push method to display navigation bar in C viewController
try this code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
[self.navigationController pushViewController:vc animated:YES];
Use the code below
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:nil];
You need to present it using UINavigationController as modal.
Hope it helps.
use this:
[self.navigationController pushViewController:vc animated:YES];
You can use "Push" segue and embed your ViewController in Navigation Controller, and then use its Navigation Controller's functions like pushViewController and popViewControllerAnimated
Answer are all correct, but i just want to explain things..
//This line gets the storyboard with the name "Main" which contains all
//the setup you made for UI (User interface)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
//While this like gets the view inside your storyboard with
//storyboard ID/indentifier `BusinessCard `
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
//Lastly, this line is correct presenting the viewcontroller BUT this doesn't add your
//viewcontroller to the array of viewControllers inside navigationController
//
//Also, this line makes you present the viewController above the
//rootViewController of window which is in your case the navigationController
//
This is you Error
[self presentViewController:vc animated:YES completion:nil];
//This is what you are looking for, and the correct one for your implementation
//
//This will let you add the `vc`(viewController) to the array of viewController
//in navigationController, to confirm that you can check the `self.navigationController.viewControllers`
//which will return the array of viewController inside your navigationController
This is the Answer
[self.navigationController pushViewController:vc animated:YES];
Hope this helps you, and my explanation is apprehendable.. Cheers
I have two storyboards that contain one view controller each.
storyboard1 --------> viewcontrollerA
storyboard2 --------> viewcontrollerB
How do I pass a string from viewcontrollerA to viewcontrollerB?
Example:
viewcontrollerB.testString=#"The transfer of data worked!";
So far, I know how to transfer views, but I don't know how to transfer data.
This is my code to transfer views:
- (IBAction)moveToViewcontrollerB:(id)sender {
UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:#"storyboard2" bundle:nil];
UIViewController *viewcontrollerB = [storyboard2 instantiateInitialViewController];
viewcontrollerB.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:lessonOne_ViewController animated:YES completion:nil];
}
My question is, if I have a string in viewcontrollerB called "testString", how do I update that string in the viewcontrollerA IBaction method?
I only know how to transfer data using segues but since this viewcontroller is on another storyboard, a segue - prepareForSegue won't work.
Thank you in advance.
First of all set Storyboard ID to your SecondView Controller like below image.
Then add following code at your button click event.
- (IBAction)moveToViewcontrollerB:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard2"
bundle: nil];
SecondStoryboardVC * secondStoryboardVCOBJ_ = [storyboard instantiateViewControllerWithIdentifier:#"SecondClassStoryboardID"];
[secondStoryboardVCOBJ_ setStrValue:#"The transfer of data worked!"];
secondStoryboardVCOBJ_.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:secondStoryboardVCOBJ_ animated:YES completion:nil];
}
Storyboard2 is storyboard name.
SecondClassStoryboardID is Storyboard ID which you given in storyboard viewController like above image.
SecondStoryboardVC is the secondclass name which you want to present
I'm loading up a UITabBarController via a Storyboard as follows:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"EventsAdmin" bundle:nil];
[self.navigationController pushViewController:[sb instantiateInitialViewController] animated:YES];
The initial view controller is the uitabbarviewcontroller however I'd like to set a property on each of the view controllers that is loaded. How can I do this?
You can achieve this the following way:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"EventsAdmin" bundle:nil];
id vc = [sb instantiateInitialViewController];
[self.navigationController pushViewController: vc animated: YES];
if ([vc isKindOfClass: [UITabBarController class]])
{
for (UIViewController *controller in [(UITabBarController *)vc viewControllers])
{
// set your property
}
}
In the loop you should check if controller is an instance of the view controller class you want to modify the property of.
Or.. You could declare a protocol and give these modal viewControllers a property of type
`IBOutlet id <myProtocol> delegate
And hook these up to the root view controller in the storyboard. Then the modal viewControllers can query the delegate from their viewWillAppearAnimated:
Then in your rootViewController you'll have (as part of myProtocol) a
-(void)setMyStuffUp:(UIViewController *)modalViewcontroller{
if ([modalViewController isKindOfClass:[viewControllerOne class]]){
//set stuff
}elseIf ( etc == etc){
//setOtherStuff..
}
}