I'm writing an app in which I add some buttons to my view (mainView) by code. When the user taps on this buttons I want a new view (resultView) to show.
I have both of this views in my storyboard, but since there is no button on my mainView that is in storyboard I can't link the mainView and resultView to each other using storyboard. Everything must be done by code.
I want to know how I can link my two views by code and define an identifier for that link.
Thanks,
To switch the view controller programmatically, you have to set a identifier in the attribute inspector at section "View Controller". After that, you can just use the value inside your code. Here i used the identifier "Menu".
UIStoryboard *storyboard = self.storyboard;
MenuTableViewController *mtvc = [storyboard instantiateViewControllerWithIdentifier:#"Menu"];
[self presentViewController: mtvc animated:YES completion:nil];
There is also a description in the documentation, just look for
Instantiating a Storyboard’s View Controller Programmatically
Give the segue between the two an identifier in the Attribute Inspector in IB, then on your button press event call [self performSegueWithIdentifier:#"MySegueIdentifier" sender:theButton]
Related
i am using manual navigation on button click from one viewcontroller to another
nextpage *np = [[nextpage alloc]init];
[self.navigationController pushviewController:np animated:YES];
I included nextpage class on the viewcontroller in storyboard. But in this case the elements added via drag and drop are not visible while if i add manual elements via code they are visible from nextpage on navigating. Please help !
and If I add navigation from a button through a storyboard, in that case both coded elements and storyboard elements are visible. !
Try this.
Pass a identifier to your view controller in storyboard.
nextpage *np = [self.storyboard instantiateViewControllerWithIdentifier:#"your identifier"];
[self.navigationController pushviewController:np animated:YES];
Content you've added in the storyboard will only appear if you load that view controller from the storyboard. By simply alloc/initing a new view controller you're losing all of the information you've added in the storyboard.
You can create a segue from a view controller to another one (i.e. not from a specific button) if you want to manually push, just use performSegueWithIdentifier, or you can add an identifier to the view controller and use instantiateViewControllerWithIdentifier to create your VC from the storyboard.
first of all give storyboard id to viewcontroller then add following code
Nextpage *nextPage = [self.storyboard instantiateViewControllerWithIdentifier:#"NextpageIdentifier"];
[self.navigationController pushviewController:nextPage animated:YES];
I've search a lot but I can't do yet.
I've my .xib UI with some buttons, separated from main storyboard.
I need to perform an action when I press one of this button and show another view.
How can I do this directly from code such ad inside an IBAction?
You can't perform segues from XIB to Storybaord,
Instead of it you need
1) Get Storyboard's instance.
2) Instantiate a ViewController that is inside the Storyboard (By using Storyboard ID).
3) Push that ViewController to to your navigation stack.
Here is sample code
UIViewController *vc = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"yourVcIdentifier"];
[self.navigationController pushViewController:vc animated:YES];
#"MainStoryboard" - is the file name of storybaord (note. it's without .storyboard extension)
#"yourVcIdentifier" - is ViewController's Storybaord ID.
To give a Storybaord ID to some ViewController, open the storybaord, click on the ViewController and specify the "Storyboard ID" field.
The basic design of app storyboard goes like this:
Top Level#1
Navigation Controller --> View Controller (Front) contains button "Show Master"
Next Level#2
Navigation Controller --> View Controller (Master) --> push --> View Controller (Detail)
When I run the app in simulator, Front View Controller page appears.
Requirement: On "Show Master" button click, I wanted the control focused to Master. When something done here, either show Detail view Controller or swing back to Front view Controller. How to code this from Front.M and Master.M.
Setup: iOS 6.x, XCode 5.x
Note: Below code exists in Front.M but it does not work, brings up Master page in black
vwMaster *vc = [[vwMaster alloc] init];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
Any thought?
It looks like you're most of the way there, but that vwMaster has no layout information associated with it. One way to provide this would be to create a .xib file with the same name as your view controller class (i.e. vwMaster.xib) and set the xib file's owner to vwMaster. Your existing code should then work.
It seems like you're using Storyboard, however, in which case you have several options:
The easiest option. In Storyboard, ctrl-drag from the button in the Front view controller to the Master view controller. select a modal segue type.
Create a modal segue between the Front and Master view controllers in Storyboard by ctrl-dragging between them, give the segue an identifier (e.g. "vwMaster") in the attributes inspector and trigger the segue in code:
[self performSegueWithIdentifier:#"vwMaster" sender:self];
Give the vwMaster view controller itself an identifier in the identity inspector in storyboard and present that as you were before:
vwMaster *vc = (vwMaster *)[self.storyboard instantiateViewControllerWithIdentifier:#"vwMaster"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
Regardless of the route you choose, you're likely to want use the delegate pattern to dismiss your view controller. For this and more, I suggest you read the relevant apple Docs:
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html
if you use navigationcontroller y u no use push and pop instead presenting viewController?
Thanks knellr.
I have been playing with different flows. I am able to goto view by calling
[self performSegueWithIdentifier:#"name" sender:self] - provided name'm.
Also have strong code handling - in cases where data to be passed out
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//NSLog(#"prepareForSegue: %#", segue.identifier);
if ([[segue identifier] isEqualToString:#"name"])
{
}
}
I just started to use Storyboard in Xcode. I have a starting view that has some buttons inside. Is there a way to load a special view when a button is clicked?
I only found this workaround:
-(IBAction)loadRegistration:(id)sender {
// load registration controller
UIStoryboard *storyboard = self.storyboard;
RegisterController *svc = [storyboard instantiateViewControllerWithIdentifier:#"RegisterController"];
[self presentViewController:svc animated:YES completion:nil];
}
You don't need any code to load a new view. You just control-drag from the button to the controller you want to present, and choose the segue type. If the controller with the button is in a navigation controller you can choose push or modal, if it's not, then you want to choose modal.
The way you did it seems ok. You can also connect the current View Controller to the one you want to present with a segue in the Interface Builder (ctrl + drag). When the button is tapped, you would call [self performSegueWithIdentifier:#"segue identifier"];. Of course, you set the id of the segue in the IB.
Even more simpler would be to connect the button with the new View Controller in IB (also ctrl+drag). This way you won't even need the IBAction.
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.