Unable to switch views programmatically using a storyboard? - objective-c

I am developing a storyboard-based application, using XCode 4.2. I'm new to storyboards, before that I used to switch views by creating a new instance of a class like this:
if (x==1)
{
theClass *theView ;
theView= [[theClass alloc] initWithNibName:nil bundle:nil];
[theView setText:theText];
[reader presentModalViewController:theClass animated:YES]; //where reader is an instance of the ZBar library
}
With storyboards, here is the code I am trying:
if (x==1)
{
[self performSegueWithIdentifier: #"theNewView" sender: self];
}
then:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"theNewView"])
{
[[segue destinationViewController] setText:theText];
[reader presentModalViewController:[segue destinationViewController] animated:YES];
}
}
I made sure that the links are well made and that the prepareforsegue method is called, but even with this code, the new view is not being loaded?

Don't call presentModalViewController in your prepareForSeque method - this method is for passing information to the new view controller.
See post.
Useful learning weblog post, for getting started with a storyboard.

Related

Performwithsegue and programatically switching views - Objective-C

I am having a problem, I want to change to another viewcontroller when a timer expires and this works with this code:
- (IBAction)Akkoord:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"Innameformulier"];
[self presentViewController:vc animated:YES completion:nil];
[self performseguewithidentifier:#"nextcontroller"]
}
But when I use this, my variables in my prepareforsegue method are not passing. How can I do this? I already tried [self performseguewithidentifier] but this is not working.
my preparforsegue code is:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
Innameformulier *IForm = [segue destinationViewController];
IForm.SignatureTransport = _drawImage.image;
IForm.KlantnaamInname = _Klantnaamtransport;
}
How can I call this function to happen on the timer?
In Interface Builder, select the View Controller from which you want to fire a segue, CTRL-drag from it to the destination View Controller and create a segue.
Select the segue, open the Attributes Inspector and create an Identifier for it.
Go back to your View Controller Class that fires the segue and if you don't already have it commented, add the method prepareForSegue:
In your prepareForSegue: method you should have something like the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue {
if ([segue.identifier isEqualToString:#"XXXX"]) {
DestinationVC *dVC = segue.destinationViewController;
dVC.attribute1 = self.anAttribute;
dVC.attribute2 = self.anotherAttribute;
}
}
And now you can fire the segue by calling [self performSegueWithIdentifier:#"XXXX"].
Note: you can also fire an action segue from an UIControl subclass like UIButton
OK, what you are doing here is bypassing the segue completely. You need to do something like this...
- (IBAction)Akkoord:(id)sender {
[self performseguewithidentifier:#"nextcontroller"]
}
This is all you need to do. The segue will then create the nextController for you and then the code inside prepareForSegue will pass the variables across.
If you want it on a timer then you just need to set up a timer and when it expires you can run...
[self performseguewithidentifier:#"nextcontroller"]
As long as you have a segue with this identifier then it will work anywhere. Ever after a timer.

Add new view controller and push on that in cocoa application

I am new to OS X application. In iOS there is methods like :
1. self.window.rootViewController = self.viewController;
2. [self.window addSubview:self.viewController.view];
to add view controller in window or
3. DefaultViewController *objDefault = [[DefaultViewController alloc] initWithNibName:#"DefaultViewController" bundle:nil];
[self.navigationController pushViewController:objDefault animated:TRUE];
4. DefaultViewController *objDefault = [[DefaultViewController alloc] initWithNibName:#"DefaultViewController" bundle:nil];
[self presentViewController: objDefault animated:TRUE completion:nil];
to push on next view controller.
My question is that in OS X is there any method like above to add new view controller to window or push on next view controller..?
Cocoa and Cocoa-touch have a little different ways to change views on the window.
To change views you need to programmatically remove old subview and add new one.
- (void)addNewSubview:(NSView *)view // NSWindowController subclass implementation file
{
[_subview removeFromSuperview];
NSView *contentView = (NSView *)self.contentView;
[contentView addSubview:view];
_subview = view;
}
or simple [contentView replaceSubview:_subview with:view];
Since OS X 10.10 there's an opportunity to use storyboards something like in iOS.
UPD
To awake window from the application delegate class create your NSWindowController-subclass instance and show the window (it's mostly like in iOS' before-storyboards era).
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CustomWindowController *controller = [[CustomWindowController alloc] initWithWindowNibName:#"CustomWindowController"];
[controller showWindow:nil];
[controller.window makeKeyAndOrderFront:nil];
}
If you are using storyboards and segues to show a new NSViewController, then you might need to close the old view controller. Here is how I do it:
- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender
{
[self.view.window close];
}
Each view controller itself creates a window, so you need to close the old one before showing the new one.

Storyboard UIViewController not appear

I recently started using storyboard.
I have the main viewcontroller, which by code must call another viewcontroller.
in storyboard, I created a "segue" that goes from the first to the second controller, with the identifier "segueToSignSelection."
in the main viewcontroller, when I need to call up the second viewcontroller, insert this code:
[self performSegueWithIdentifier:#"segueToSignSelection" sender:self];
that calls this method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"segueToSignSelection"])
{
signSelectionViewController = (SignSelectionViewController*)[segue destinationViewController];
[self presentModalViewController:segue.destinationViewController animated:YES];
[segue.destinationViewController test];
}
}
in the second controller, the test method is invoked. But the view does not appear. What could be the problem?
Thanks

iOS 5, Storyboards, ARC: Navigation Controller doesn't work in custom delegate

The prequel for this question is here. Now I've got another problem. I cannot navigate despite of the didSelectRowAtIndexPath getting invoked while tapping on cell
KMList *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"KMs"];
detailViewController.fromPeriod=self.fromPeriod;
detailViewController.period1=self.entityID;
[self.navigationController pushViewController:detailViewController animated:YES];
What the problem. Is it because I am kind of off the storyboard?
P.S No warnings or errors. Just nothing happens.
Thanks in advance.
You should not push the new view controller in code when using a storyboard. This is done automatically. Instead make a push-Segue in Interface Builder and give it an identifier. Then in your view controller you overwrite prepareForSegue:sender: and check for the identifier like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"<your segue identifier>"]) {
KMList *detailViewController = [segue destinationViewController];
detailViewController.fromPeriod=self.fromPeriod;
detailViewController.period1=self.entityID;
}
}

How to pass data between modal view controller

This is a noob question.what i want is to pass data from my QuizViewcontroller to QuizModalViewController.i was successful in creating a normal modal view controller but having problem when passing data between the two..below is my code.
QuizViewController
-(IBAction)button3Clicked:(id)sender
{
QuizModalViewController *mvid=[[QuizModalViewController alloc]initWithNibName:#"QuizModalViewController" bundle:nil];
mvid.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:mvid animated:YES];
}
QuizModalViewController
- (IBAction)goBackToView
{
[self dismissModalViewControllerAnimated:YES];
//[controller loadQuestion:currentQuestionIndex+1];
}
-(IBAction)button3Clicked:(id)sender
{
QuizModalViewController *mvid=[[QuizModalViewController alloc]initWithNibName:#"QuizModalViewController" bundle:nil];
mvid.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mvid.theDataYouWantToPass = theData; // this is how you do it
[self presentModalViewController:mvid animated:YES];
}
Note that theDataYouWantToPass must be a property declared in the QuizModalViewController.h file.
You can also use the -(void)prepareForSegue:... - Method to share data between controllers.
For Example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"Your_Identifier"]) {
TestViewController *controller = [segue destinationViewController];
NSArray *array = [[NSArray alloc] initWithObjects:#"",nil];
controller.objectInTestViewController = array;
}
}
Hope this helps someone....
Make your QuizModalViewController available at class scope instead of using a local variable.
Then in the goBackToView: you can access the controller's content prior to dismissing it.
By the way: in your code you're leaking mvid. Release it after presenting it modally.