Segue to UIViewController from a xib - ios

I have a custom UIView which is a xib file and then a class that controls this view. I also have a storyboard which a lot of different view controllers inside. How do I perform a segue from the UIView to a specific UIViewController that is inside the storyboard?
Thanks

You can't do that. What you can do is give the UIViewController a storyboard ID, using the menu on the right of the interface builder.
Then call it programatically, like so:
MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self.navigationController pushViewController:vc animated:YES];

You could put a ViewController into the Storyboard, set the class to your custom ViewController. Then, if you have class files for the view in the xib, set your custom view as the VC's view (already saves some code in loadView) and then just add a segue from the custom VC to the other view controller. To trigger that segue, you have to call [self performSegueWithIdentifier:#"identifierOfSegue"] in your custom ViewController.

Related

Perform Segue from Xib programmatically

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.

Displaying a UIViewController within another UIViewController

I am developing an app in which a UIViewController (firstViewController) contains some UILabels, a UIButton, and a UIView (subView). The UIView should display the UIViewController (secondViewController) that contains some layers. I am unable to do this.
What should I do to display secondViewController within subView of firstViewController?
You should use UIViewController containment or parent/child view controllers. You can read details here.
The most basic version is:
UIViewController *parentVC = ...
UIViewController *otherVC = ... // it's view will be added as subview
[parentVC addChildViewController:otherVC];
[parentVC.containerView addSubview:otherVC.view]; // containerView is a view where your child view controller should go
[otherVC didMoveToParentViewController:parentVC];
If you only add other view controller's view as a subview, child view controller won't receive all the events. For example, if you use other methods suggested here (just add view as subview and nothing more), you won't get -viewDidAppear: message (and others) sent to your child view controller.
You can do that by adding view of another view controller as sub view in view as bellow
SecondVC *aObjSecondVC = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondVC"];
[self.view addSubview:aObjSecondVC.view]
You can add it using the following line:
[self.subView addSubView:secondViewController.view];

UITableViewController static cells not appearing when pushed to navigationController

I'm running into a rut...
I've create a UITableViewController with static cells (i've deleted all default UITableView methods). Whenever I segue to this view controller, the static cells appear, but when I push it onto the navigationcontroller the static cells do not appear...any idea to why this would be hapenning?
Here is my code:
//shows empty uitableviewcontroller
OthersUsersTableViewController *tvc = [[OthersUsersTableViewController alloc]init];
[self.navigationController pushViewController:tvc
animated:YES];
and
//works
[self performSegueWithIdentifier:#"toOtherUser" sender:self];
Thanks for the help!
If you manually alloc and init your Static table view, how should your app know that it is referencing a static table view in your storyboard?
Give your static table view an Identifier in the storyboard and initialize it like this:
OthersUsersTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:#"staticTableView"];
You can set your identifier in the menu on the right where you also set the custom class for the view controller. The field is called Storyboard ID.
when you're just calling [[UIViewController alloc] init], the UI objects you've added and configured on the storyboard,does not get called or wired to your view controller.
You should instantiate the view controller through the storyboard itself by adding an identifier to your view controller(on the storyboard) and calling instantiateViewControllerWithIdentifier method of your storyboard.
You can get a reference to your storyboard through the current view controller's storyboard property.
OthersUsersTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:#"identifier"];

UITableViewController hold by a UINavigationController

My aim is to make a UITableView with a certain level of navigation. All I will be using are table views so I really want to go with UITableViewController and UINavigationController.
I created a subclass of UITableViewController, here is the xib file:
It's automatically generated as the class is UITableViewController subclass. Is it enough to get a navigation bar, or should I add something on the xib.
And the relevant code in the .m file is the following:
- (void)viewDidLoad
{
[super viewDidLoad];
UITableViewController *tableViewController=[[UITableViewController alloc]initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:tableViewController];
self.view.window.rootViewController=navController;
//Create the modal for the UITableView
array1=[NSArray arrayWithObjects:#"Categorie 1",#"Categorie 2",#"Categorie 3",#"Categorie 4",#"Categorie 5",#"Categorie 6",#"Categorie 7",#"Categorie 8",#"Categorie 9",#"Categorie 10", nil];
}
And here is what I got:
I was waiting to see a navigation bar at the top of the view. I know I am missing something, so please bear with me and give help. Thanx.
In your MainWindow.xib, you have to drag a UINavigationController, then a UITableViewController. Set the class of the UITableViewController to your table view controller subclass. Also make sure that the table view controller is set as the root of the navigation controller.

IOS Switch view and storyboard not works

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.

Resources