In Xcode 4.5 you can change views from a segue, But I want to programatically change views in Mainstoryboard. I Know in Xib. we use this method:
SecondViewController *Second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Second animated:YES];
I want to change the view when the user clicks done in the textfield (This code detects if user clicks done):
-(IBAction)hidekeyboard {
[sender resignFirstResponder];
}
You create a segue in your storyboard that goes from the view that has the text field in question and goes to your SecondViewController. You have to assign an identifier to it (also in the storyboard, click on the line connecting the two view controllers), let's call it "segueToSecondViewController". Then, you can manually call the segue using:
[self.storyboard performSegueWithIdentifier:#"segueToSecondViewController" sender:self];
An alternative to segueing is to instantiate the viewController from the storyboard.
First you assign a Storyboard ID to the viewController in the storyboard.
Then you instantiate that viewController, and present it.
MBTagEditorViewController *tagEditor = [self.storyboard instantiateViewControllerWithIdentifier:#"TagEditor"];
[self presentModalViewController:tagEditor animated:YES];
Related
Lets say I am in ViewController1 and I want to load ViewController2. The only way I know to accomplish this is using a button in ViewController1, and connecting to ViewController2 in the storyboard but I do not want to do it that way. I want to do in a method in MyViewController1. For example, in my viewDidLoad method.
Any help is greatly appreciated. Thanks
Create a segue from vc1 to vc2 inside the storyboard, give it a name and call:
[self performSegueWithIdentifier: #"mySegueCustomName" sender: self];
If you are trying to embed the view managed by ViewController2 into the view managed by ViewController1, you can do this by adding a UIContainerView to ViewController1 with an embed segue pointing to ViewController2.
This is useful, for example, if you have a reusable view managed by ViewController2 and want to use that in multiple places.
You can do this in InterfaceBuilder by dragging a UIContainerView from the Object library onto the view for ViewController1. You can then change the class of the view controller it creates to ViewController2.
Do not confuse Views and ViewControllers. You post title suggests you do not have it ingrained yet fully.
If you want to present another view controller programatically, then simply create an action method for the button and do it there. ViewDidLoad is absolutely NOT fit for that purpose.
You can present a view controller included in the storyboard like this:
-(IBAction)goToDetails:(id)sender
{
//identify the correct storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboard's name" bundle:nil];
//extract a view controller instance from the storyboard
DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"details view controller's identifier"];
//present the view controller instance
[self presentViewController:detailViewController
animated:YES
completion:nil];
}
And here's an example to go a to an unrelated non-storyboard view controller.
-(IBAction)loginToSystem:(id)sender
{
LoginViewController *loginViewController = [[LoginViewController alloc] init];
[self presentViewController:loginViewController
animated:YES
completion:nil];
}
i have a button that is created programmatic and i need to perform a segue on click that will send data with prepareForSegue, but this code wont trigger it.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"AddToCalendarViewController"];
[self.navigationController pushViewController:vc animated:YES];
i have tried this:
[self performSegueWithIdentifier:#"addToCalendar" sender:self];
but since the button is created grammatically i don't have any segue identifier to call, and using the following to create it did not work
UIStoryboardSegue * segue = [[UIStoryboardSegue alloc] initWithIdentifier:#"addtoCalendar" source:self destination:vc];
UIStoryboardSegue must not be created programmatically. Create a segue by connecting your view controller to the destination view controller in the storyboard and put its identifier to #"addToCalendar" (always in the storyboard). Then call it as you are already doing.
If you don't have an actual storyboard segue to call when the button is pressed, then you need to call addTarget:action:forControlEvents, where action is a selector to the method on target to be called.
Specifically, you first need to create a method to push the destination view controller, like so:
- (void)addToCalendar {
DestinationiewController * vc = [[DestinationViewController alloc] initWithNibName:#"destinationViewController" bundle:nil;
[self.navigationController pushViewController:vc animated:YES];
}
and then in your source view controller's viewDidLoad (or wherever you're programmatically instantiating the button), you need to add
[self.button addTarget:self action:#selector(#"addToCalendar") forControlEvents: UIControlEventTouchUpInside];
which will call the addToCalendar method you defined above.
hi I have a uicollectionview controller with two buttons ,when i click the first button it directly opens uitableview controller(with 2 button) and when i click the second button i want uicollectionview controller to show? how can i do this in storyboard?
i tried this code inside one button but its throwing one exception like "Nib file not found"
table *listview=[[table alloc]initWithNibName:#"listview" bundle:nil];
[self.navigationController pushViewController:listview animated:NO];
{list view is the uitableviewcontroller name ,i assigned this name on uitableviewcontroller title property in right panel of xcode}
"Nib file not found" error makes me reckon that you haven't spelled the .xib's name correctly.
Lets say you have your controller you want to push to.
TableViewController.h
TableViewController.m
And a corresponding xib thats linked with TableViewController.h. (view, IBOutlets, etc).
TableView.xib
Now in your main ViewController we push to TableViewController
TableViewController *controller = [[TableViewController alloc] initWithNibName:#"TableView" bundle:nil];
[self.navigationController pushViewController:controller animated:NO];
Hope this helps.
BooRanger
We can use Story board identifier for moving from uicollectionviewcontroller to uitableviewcontroller
table *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"list"];
[self presentViewController:vc animated:NO completion:nil];
Its working fine
I got an app with a storyboard and one window in .xib.
From the storyboard to the .xib i move in that way:
ShowDreamNIBController *detailViewController = [[ShowDreamNIBController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:detailViewController animated:nil];
How can i move back from the .xib to the storyboard?
And one more question. Can i send some information when move to the .xib from storyboard? Like i do it throught segues in prepareForSegue method
UPD I got answer about closing .xib. Need answer about sending information.
you can get back to your storyboard by simply dismissing the modal view controller.
Like this:
- (IBAction)backPressed:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
You should put this method in your ShowDreamNIBController.
As for the second question, you can send back information to the view controller that presented the modal view controller by getting reference to it using delegation.
EDIT:
To "send information to the XIB" do you mean that you want to set information to the view controller that you want to present? If yes, than use this when you present the modal controller:
ShowDreamNIBController *detailViewController = [[ShowDreamNIBController alloc]initWithNibName:nil bundle:nil];
[detailViewController setSomething: something];
[detailViewController setAnotherThing: anotherThing];
[self presentModalViewController:detailViewController animated:nil];
I hope this helps.
I have a problem with switching views. I'm using a simulator with xcode 4.2
Storyboard contains:
NavigationController (initial view controller)
UIViewController which has relationship with the navigation controller
UIViewController (paired with my custom class: ViewEntryImageController) which hasn't got any relationship. Contains a button, a bottom toolbar with some toolbar button.
User come into the UIViewController, where he can see a ScrollView and in ScrollView some images.
Images has a gesture:
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(openEntryImage)];
[image addGestureRecognizer:recognizer];
[recognizer release];
The openEntryImage function:
(IBAction)openEntryImage
{
ViewEntryImageController *controller=[[ViewEntryImageController alloc]initWithNibName:nil bundle:nil];
controller.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:controller animated:YES];
[controller release];
}
When I try to tap the image, the openEntryImage works as well (the effect is correct), but I don't see my ViewEntryImageController view and my buttons, I'm only see a black window.
I try to put a NSLog line into the ViewEntryImageController viewDidLoad function, and it works, so what is the black window and where is my view controller?
When I try to use pushViewController, on the new view I found a navigation toolbar with a back button, but no other controls.
I tried another version, I created a UIViewController class, but now with a xib file. I used it instead of ViewEntryImageController and it works. Why?
I want to use this controller in storyboard too.
The ViewEntryImageController class by itself has no information about how to build the dialog. But you can instantiate your view controller on your own from the storyboard:
UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:#"StoryboardFileName" bundle:nil];
ViewEntryImageController *controller = (ViewEntryImageController *)[myStoryboard instantiateViewControllerWithIdentifier:#"ViewEntryImage"];
This assumes a storyboard name of StoryboardFileName and that the view entry image controller has an identifier of ViewEntryImage set in the view properties (Attributes inspector, section "View Controller").
Try it like this :
ViewEntryImageController *controller=[[ViewEntryImageController alloc]initWithNibName:#"ViewEntryImageController" bundle:nil];
If you don't use .nib names but rather use storyboards, it's a bit harder. Create a segue from the controller to the ViewEntryImageController controller by holding ctrl and dragging from one view to the other. Click this segue and give it an identifier.
Then use the [self performSegue:#"identifier"]; function to present the next view.