some days ago I wrote a method to load a view controller using presentViewController:
-(void)passaGC:(NSString *)user
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"generaC"];
[self presentViewController:viewController animated:YES completion:nil];
}
But today I need to pass the variable user from this method to the loaded viewController.
How can I modify my method to do this?
I found other question on stack overflow but nothing is really similar to my request
add a property to your destination viewController (in the .h):
#property (strong, nonatomic) NSString *user;
and finally your method will look like
-(void)passaGC:(NSString *)user
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"generaC"];
viewController.user = user;
[self presentViewController:viewController animated:YES completion:nil];
}
Swift Solution
Except #IBOutlets, you can simply assing data to destination view controller properties.
DestinationVC.swift
var name: String?
SourceVC.swift
let storyboard = UIStoryboard(name: "Helper", bundle: nil)
let destinationVC = storyboard.instantiateViewControllerWithIdentifier("DestinationSID") as! DestinationVC
destinationVC.name = "Mustafa"
presentViewController(destinationVC, animated: true, completion: nil)
Related
I have the following viewControllers:
When I click the "Go to gray" it loads the gray viewController using SWRevealViewController.
Here is my code:
#import "SWRevealViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)gotoGray:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SWRevealViewController *swrController = [storyboard instantiateViewControllerWithIdentifier:#"SWRevealViewController"];
[self presentViewController:swrController animated:YES completion:nil];
}
My Question to you guys is how can pass data from the blue view controller to the gray (front SWRevealViewController) ?
I'll really appreciate your help.
You have a reference to it with SWRevealViewController *swrController, you can set data on the reference by using information from the presenting view controller.
- (IBAction)gotoGray:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SWRevealViewController *swrController = [storyboard instantiateViewControllerWithIdentifier:#"SWRevealViewController"];
swrController.aProperty = self.myInformation;
swrContoller.anArray = self.arrayOfInfo
[self presentViewController:swrController animated:YES completion:nil];
}
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 have implement UISearchController in my HomeController.By this way
SearchTableViewController *searchResults = (SearchTableViewController *)self.controller.searchResultsController;
[self addObserver:searchResults forKeyPath:#"myResult" options:NSKeyValueObservingOptionNew context:nil];
searchResults.rootHomeController=_homeController;
[self presentViewController:self.controller animated:YES completion:nil];
My Search value showing well in SearchTableViewController.Now I need to go details after SearchTableViewController UITableView cell click i tried by this way but not working.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
#"Main" bundle:[NSBundle mainBundle]];
AlbumSongListController *detailSongList=[storyboard instantiateViewControllerWithIdentifier:#"AlbumSongListControllerID"];
[self presentViewController:detailSongList animated:YES completion:nil];
I need to pass navigation controller so that i can back to search controller
pass your result one view controller to another by declaring a varialble like:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("DestViewController") as! DestViewController
resultViewController.result = self.result
self.presentViewController(resultViewController, animated:true, completion:nil)
You need to present a navigation controller with SearchTableViewController as root in your HomeController. Thus you can perform navigations.
i.e. on click of table cell in SearchTableViewController (didSelectRowAtIndexPath delegate), push the detail view controller using [self.navigationcontroller pushviewcontroller] , you will get back navigation to SearchTableViewController by doing so.
plz use this code in app delegate
-(BOOL) application: (UIApplication * ) application didFinishLaunchingWithOptions: (NSDictionary * ) launchOptions {
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:
# "Main"
bundle: [NSBundle mainBundle]
];
HomeVcController * HomeVcControllerOb = [storyboard instantiateViewControllerWithIdentifier: # "HomeVcControllerID"];
UINavigationController * navController = [
[UINavigationController alloc] initWithRootViewController: HomeVcControllerOb
];
self.window.rootViewController = navController;
}
and hidden NavigationBar on homeVWcontroller
[self.navigationController setNavigationBarHidden:YES animated:YES];
then after just use this
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:
# "Main"
bundle: [NSBundle mainBundle]
];
AlbumSongListController * detailSongList = [storyboard instantiateViewControllerWithIdentifier: # "AlbumSongListControllerID"];
[self presentViewController: detailSongList animated: YES completion: nil];
and in AlbumSongListController show navigation bar
[self.navigationController setNavigationBarHidden:No animated:YES];
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];
Helo all,
I am moving from one view controller to another using below code.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
[self.navigationController pushViewController:vc animated:YES];
Now i want to pass some data from one view controller to another like some variable.I have got this code to do this task but i am not making instance of another view controller.I am starting another view controller using storyboard.
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.str1 = str;
[self.navigationController pushViewController:secondViewController animated:YES];
Please tell me how can i pass data between view controllers in my way?
Solution does not work
decalre NSString in .h file.
#property (nonatomic, strong) NSString *UserId;
Synthesize in .m file
#synthesize UserId;
Code for navigation
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
vc.UserId=#"abc";
[self.navigationController pushViewController:vc animated:YES];
But vc.UserId does not recognized.
You can pass data between two ViewControler with the help of properties.
First in RoloBusinessCard.h ViewControler make a property like this
#property (nonatomic, strong) NSString *str1;
in .m file of this class synthesize it like this
#synthesize str2;
Now like this you can pass value
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
vc.str2=str2
[self.navigationController pushViewController:vc animated:YES];
If you are not able to get str2 in RoloBusinessCard
First Replace UIViewController with Your Viewcontroller Name like this
RoloBusinessCard *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
or
typecase UIViewController like this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
(RoloBusinessCard *)vc1=vc
vc1.str2=str2
[self.navigationController pushViewController:vc1 animated:YES];
Hope it help.
Try Like this, Hope it will work.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
vc.str1 = str;
[self.navigationController pushViewController:vc animated:YES];
Here we are just using Navigation stack for passing data to the next controller
create a property in 2nd view controller
#property (nonatomic, strong) NSString *yourString;
in firstView controller action method
object of view controller which you will create on push method do
-(void)btnAtion{
object.yourstring = txtyourfirstvcObject.text
}
In swift, we can do the same
let's suppose we want to pass a Bool Value to the next controller so we can do :
Class ToViewController : UIViewController {
public var isSelected : Bool?
}
Class FromViewController : UIViewController {
#IBAction func onClickFlyingFrom(_ sender: UIButton) {
let tvc = self.storyboard?.instantiateViewController(withIdentifier: "ToViewController") as! ToViewController
tvc.isSelected = true
self.navigationController?.present(vc, animated: true, completion: nil)
}
}