Add Storyboard as subview in UIViewController - ios

I am creating a reusable view for which I have created a separate Storyboard. This reusable view will be added in different UIViewController is Main.StoryBoard. For this I followed storyboard reference. But I am not sure how to add Storyboard as a subview inside a UIViewController having some definite frame.
Please let me know if you want to have a screenshot as to how I am designing the screen.

first you need to get the reference of the storyboard from the different storyboard.
UIViewController *viewControllerToAdd =
[[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil]instantiateViewControllerWithIdentifier:#"viewForPopover"];
//Instead of MainStoryboard you write the name of the Storyboard which you have created separately
the you write these lines
[self addChildViewController:viewControllerToAdd];
[self.view addSubview:viewControllerToAdd.view];
[viewControllerToAdd didMoveToParentViewController:self];
This much should probably do, i havent tried it yet

make reusable UIViewController and then addChildViewController wherever you want. Below i made RemarksViewController which i used in many views of UIViewController
RemarksViewController *vC=[[UIStoryboard storyboardWithName:#"yourStoryboardName" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"showRemarks"];
[self addChildViewController:vC];
vC.view.bounds=self.view.bounds;
[self.view addSubview:vC.view];
[vC didMoveToParentViewController:self];

Related

UICollectionView segue from Sign-up showing a black screen

I have a social app that requires login/sign-up. This is done with the ParseUI library that present a custom window for user validation.
When a user signs up, I want to display a CollectionView where users choose some preferences (each as a cell) and proceed to the app.
In the Sign-Up View Controller I have this:
- (void)signUpViewController:(PFSignUpViewController *)signUpController didSignUpUser:(PFUser *)user {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ChooseLeagueCollectionViewController *clvc = [storyboard instantiateViewControllerWithIdentifier:#"ChooseLeagueViewController"];
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:clvc animated:NO completion:NULL];
}
Do I need to alloc and init a flowLayout object even though it is already present in my storyboard?
In my storyboard I have a CollectionViewController containing one cell linked to a subclass of UICollectionViewCell with an ImageView and a Label. This storyboard file has as custom class 'ChooseLeagueViewController' which is a subclass of CollectionViewController, and is also data source and delegate.
In the .h file I have:
#interface ChooseLeagueCollectionViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
In the .m file I implement all the required methods that the datasource and delegate protocols require. In cellForItemAtIndexPath I populate the cell (subclass of CollectionviewCell), change the label text and text color, the image, and than return it.
Also I am not sure why I am asked to register my cell like below, even though my cell has a reusable identifier in the storyboard.
[self.collectionView registerClass:[LeagueCollectionViewCell class]forCellWithReuseIdentifier:#"leagueCell"];
Anyway when I run the app, after signing up, i am brought to a completly black screen but the battery. the app does not crash and all the 'data source' methods are called correctly.
I think that there is something wrong in the way I am trying to show this View.
Any guess on how I could improve my segue or what the mistake is?
thanks in advance
Simply alloc,init will give you blank controller with no view associated with it. You have to initialise it through storyboard like this, then you will not have to provide any custom init/alloc,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ChooseLeagueCollectionViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"yourViewControllerIdentifier"];
[self presentViewController:vc animated:NO completion:NULL];
You specify viewController identifier in storyboard by selecting your viewController and in identity Inspector in storyboard id. :)

Xcode 6 subclasses

I want to put a view controller inside a scroll view.
I think I can do it with something like:
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[scrollView addSubview:vc];
What I don't understand is how to specify my nib name correctly, because I have an interface built which I want to use but it is one of multiple views appearing in my main storyboard.
You cannot add a viewController as a subview. You can add a childViewController and the viewController's view as a subview. But you should probably read up more on how Objective-C and iOS works.

Property 'navigationController' not found. UIView Interface

I have a storyboard that handles all of my applications UI, apart from the header on each page which is handled by its own .xib file following this guide:
http://patientprogrammer.wordpress.com/2012/03/12/re-usable-subviews-in-ios/
Within my header .xib I have a button that I want to have when clicked load a view that is part of my story board.
I have tried:
- (IBAction)clicked:(id)sender {
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
someViewController *storyViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:#"someViewController"];
[self.navigationController pushViewController:storyViewController animated:YES];
}
However as my class uses the interface UIView controller navigationController is not found, what can I do to launch a view within the story board from my UIView.
Or if there another interface I can use that will still let me have this as a subview.
what can I do to launch a view within the story board from my UIView
This is bad. A view is a view. Its purpose is to show something to the user, not controlling the app.
UIViewController is where you will need to do this. The navigationController is a property on this class, not on UIView. Read UIViewController class reference for more info.

Segue to UIViewController from a xib

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.

didDeselectRowAtIndexPath not called from UITableViewController added to UIViewController from story board

I am using a split view controller and wanting to add multiple table views to the master view controller.
So I can take advantage of prototype cells that (I believe) you can only get from UITableViewController I have put the UITableViewController into a second storyboard for the app. I am then substantiating it with:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
MyTableViewController *myTableViewController = [storyboard instantiateInitialViewController];
I then add the view of this UITableViewController as a subview of my UIViewController.
[self.view addSubview:myTableViewController.view];
When I run this, I can select and highlight rows just fine. But didSelectRowAtIndexPath is not being called for some reason?
Thanks for any help,
Richard
Add this line
myTableViewController.view.delegate = myTableViewController;
or Make sure you have done the same in XIB(Interface Builder).
Turns out that I needed to add the uitableviewcontroller as a child controller of the uiviewcontroller as well. Just adding the view alone was not enough.

Resources