How to replace a UITableView by UICollectionView in a same UIViewController? - ios

I have a UIViewController and this controller has a Searchbar. If I write something the search result will be shown on UITableView. when I will select one of the rows of UITableView , the UITableView will be vanished and UICollectionView will be appeared on same UIViewController.
Example: If I write f in Searchbar ,UITableView will show
feni
florida
france
If I press in france, details will be shown on a UICollectionView in same UIViewController.
one think I have thought to hide UITableView in DidSelectRowAtindexPath and will appear UICollectionview Programmatically ... but to design a cell of UICollectionview in Programmatically is not easy to me.
Please provide me any suggestion or tutorials that I can proceed.

You can create a storyboard with tableview and collection view and give storyboard identifier and load the storyboard according to the condition
if (some_condition)
{
//UITableview storyboard
UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:#"storyboardName" bundle: nil];
ViewController* vc = [storyBoard instantiateViewControllerWithIdentifier: #"Storyboard identifier"];
}
else
{
//UICollectionview storyboard
UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:#"storyboardName For Collection" bundle: nil];
ViewController* vc = [storyBoard instantiateViewControllerWithIdentifier: #"Storyboard identifier for collection view"];
}
Hope it helps !

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. :)

IOS 8 Objective-C Referencing storyboard from a UITableViewCell

I have a use case where essentially I have a image in a customized table view cell. When the user selects the image we want to send them to a Profile page.
Normally, I'd do this by
- (IBAction)userClickedProfilePic:(id)sender {
ProfileViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Profile"];
vc.postInfo = self.newsfeedData.newsFeedPosts[indexPath.row];
[[self navigationController] pushViewController:vc animated:YES];
}
But self != have a storyboard reference in this case since it's a UITableViewCell class. Is there a way to pop or push a new viewController from a tableviewcell action?
You have plenty of options. What I usually do is that I will create IBAction in viewcontroller which "contains" tableview with its cells. You can create IBOutlets or IBActions in all parents viewcontrollers through IB in a same way you would do it for cell..you might didn't know that.
But you want to click event on uiimageview which is not so straitforward. You have to do it programatically but it's not that hard just do something like this in cellForRowAtIndexPath and implement actionMethod:
UITapGestureRecognizer *actionMethod = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionMethod:)];
[cell.yourImageView addGestureRecognizer:actionMethod];
By the way instead of self.storyboard you can use this in case your storyboard is named Main.storyboard.
[UIStoryboard storyboardWithName:#"Main" bundle:nil]

Storyboard UITabBaar, initiate two different views from button

I've created a storyboard that has a UITabbarController all is working well but now I want to add some logic that determines which viewcontroller a particular tabbar button will display.
Example... if a customer has a valid subscription display viewcontroller one, if no subscription display viewcontroller two.
Is this possible using storyboards, I've looked at UITabBarDelegate and prepareForSegue but struggling to piece this together?
Are there any examples of how to do this sort of thing using StoryBoards?
Many thanks
You can set it like this:
if(hasSubscription)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
ViewController1* subsection = [storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
ViewController2* subsection1 = [storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
[(UITabBarController*)self.window.rootViewController setViewControllers:[NSArray arrayWithObjects:subsection,subsection1, nil]];
}
If you want to add rootviewController according to the subscription then above answer given by soryngod is good one.
But if you want to open viewControllers after rootviewcontroller loaded, then at tabBarButton press perform following code:-
before this code, add yours viewControllerONE and viewControllerTWO to the rootViewController by segues as shown: . And give each segue an identifier in AttributeInspector, example "one" for viewControllerONE and "two" for viewControllerTWO.
then at tabBarButton action do the following:-
if(subscription)
[self performSegueWithIdentifier:#"one" sender:self];
else
[self performSegueWithIdentifier:#"two" sender:self];

load view form StoryBoard that does not have a Segue

I am really liking having all of my view's laid out in the Storyboard but there are times when I will have a view that is shown based on a button that is generated by code so there will be no Segue reference - it will be totally disconnected in the Storyboard. I would still like to design it in the storyboard though so I can have a nice overview of all my screens.
Is it possible for me to load the XIB (or whatever it is in a storyboard) designed in the storyboard when a UIViewControler is loaded?
I do this in my projects:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *myController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self.navigationController pushViewController: myController animated:YES];
Just make sure you have set your controller's identifier correctly. You can do this by:
selecting the controller in Storyboard
in the Utilities panel, click the Attributes inspector. Whatever you decide to put in the Identifier field is what goes in the 'instantiateViewControllerWithIdentifier' statement.
I hope this helps.
You must assign a Storyboard ID to your view controller.
IB > Show the Identity inspector > Identity > Storyboard ID
Swift
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewcontroller = storyboard.instantiateViewControllerWithIdentifier("viewController")
as? UIViewController
if let navigationController = self.navigationController {
navigationController.pushViewController(viewcontroller!, animated: true)
}

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