Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i try this code but it didn't work for me.
Thank You
-(void)passvar:(NSString *)user
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"signupvc"];
viewController.user = user;
[self presentViewController:viewController animated:YES completion:nil];
}
UIViewController does not have an user property.
You should use your custom subclass instead of UIVIewController
-(void)passvar:(NSString *)user
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MyCustomViewController *viewController = (MyCustomViewController *)[storyboard instantiateViewControllerWithIdentifier:#"signupvc"];
viewController.user = user;
[self presentViewController:viewController animated:YES completion:nil];
}
Note that in this code, you instantiate a new MyCustomViewController from the storyboard and present it to the user. It has nothing to do with any other View Controller that you opened before calling those line of code.
If you call this function 3 times, it will open 3 different MyCustomViewController.
Related
I added one ViewController to my project and created one class.
After I bind this class to my ViewController.
In another controller I have method:
- (IBAction)login:(id)sender {
// How here I can do redirect to controllerViewController
}
There are two ways to push view controllers in the application.
1) by Segue
2) By View controller identifier
1) By Segue :
[self performSegueWithIdentifier:"SegueIdentifier" sender:self];
2) By View controller identifier :
Yourclassname *gotoYourClass = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:gotoYourClass animated:YES];
- (IBAction)login:(id)sender {
ViewController *vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil]; }
In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code, to bring that view forward.
IF YOU WANT TO USE PUSH THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self.navigationController pushViewController:vc animeted:YES];
IF YOU WANT TO PRESENT THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
Note: In UIViewController please enter your view controller name which you want to push into another view.
If you want to comeback to the current ViewController later then use Navigation ViewController or else just use the presentedViewController of self(your current viewController)- no going back business, like they have previously illustrated.
For a simple block of execution(demo or practice) it's all the same but for a project application it completely matters to make the correct choice.
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 recently started a new project and after I successfully set up a login page, I came into a problem.
If I type in valid credentials in the text fields, my app should do the following:
if (user) {
[self.delegate loginViewControllerDidLogin:self];
...
The "if user" part means that everything was okay. After a few hours of searching I got to know that my [self.delegate loginViewControllerDidLogin:self]; should be defined in the AppDelegate.m file. Here I wrote the following code:
- (void)loginViewControllerDidLogin:(LoginViewController *)controller {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController * ViewController = [[UIViewController alloc] init];
[self.window addSubview:ViewController.view];
[self.window.rootViewController presentViewController:ViewController animated:YES completion:nil];
}
My problem is that I cannot define the ViewController on my story board, so I cannot connect the code with the ViewController itself.
So basically I would like the app to open a new ViewController after logging in.
On your storyboard select the view controller you want to display and fill out the "Storyboard ID":
Then instantiate it like this:
UIViewController * ViewController = [storyboard instantiateViewControllerWithIdentifier:#"YourVCStoryboardID"];
Using:
UIViewController *vc = [[UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil] instantiateInitialViewController];
[self presentViewController:vc animated:YES completion:^{}];
But I need to set a BOOL in the initial viewController for that story board.
I have BOOL declared in the .h and an if statement is dependent on it:
if (_boolIsTrue) {
FirstView *firstView = (FirstView *)[storyboard instantiateViewControllerWithIdentifier:#"first_view"];
[self.navigationController setViewControllers:#[firstView] animated:NO];
}
else {
SecondView *secondView = (SecondView *)[storyboard instantiateViewControllerWithIdentifier:#"second_view"];
[self.navigationController setViewControllers:#[secondView] animated:NO];
}
When I try to access it with something like:
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
UIViewController *vc =[storybord instantiateInitialViewController];
vc does not give me access to the BOOL.
Not sure what it is that I am not understanding about ViewControllers and Storyboards, but I would really appreciate the help.
Thanks
EDIT AFTER SOLUTION:
Wain was exactly right!
Here's the exact working code since my gaps spanned a couple different concepts. In case it helps someone else:
UINavigationController *nav = [[UIStoryboard storyboardWithName:#“MyStoryBoard” bundle:nil] instantiateInitialViewController];
InitialLockSetupViewController *vc = nav.viewControllers.lastObject;
vc.isBoolTrue = YES;
[self presentViewController:nav animated:YES completion:^{}];
So you have to get the View Controller with the correct type in order to access the var. BUT if it's not set as the initial view controller in the storyboard you have to get it as the root (0-th) view controller on the stack. THEN you have to remember to push the nav controller NOT the view controller.
Thanks again Wain. This was a good lesson!
You're simply not understanding that the compiler needs to know the class type. Currently you're telling the compiler that the class is UIViewController and it obviously doesn't have your custom property. You need to set the class to your custom one, like:
MyViewController *vc = [storybord instantiateInitialViewController];
Also, make sure that you don't call instantiateInitialViewController more than once or you'll have multiple copies of the same class and might use the wrong one...
I wanted to be able to enter a view controller only if an entered password is correct.
i used the following code -
if(([input isEqual:#"12"]))
{
NSLog(#"change view controller");
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"secondViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
but when i enter , Xcode gave me a sigbrt error, so i added an all exception breakpoint, but it goes to main.m and says 'thread 1:break point 1.1'.
thank you for reading and if you know the answer please answer
SecondViewController *second=[self.storyboard instantiateViewControllerWithIdentifier:#"storyboardid"];
[self.navigationController presentViewController:second animated:YES completion:nil];