So I have two UICollectionViews that have different types of layouts, different types of cells, and different data in the cells, hence I have decided to make two separate views rather than dealing with keeping track of all of their data.
I made almost all the UI in the Interface editor, so I have .xib files for both my UICollectionViews (lets call them NewsCollectionView and ExploreCollectionView) and all of my cells. The two .xib files are connected to their controllers through the FilesOwner property in the Interface Builder.
I also have a main view (lets call it HomeViewController) with a tabBar which I want to use to switch between my two UICollectionViews. My first question is simple, how do I initialize one of my UICollectionViews from the NIB?
Here is how I imagined it (in my HomeViewController)
#property (strong, nonatomic) NewsCollectionView *newsCollectionView;
#property (strong, nonatomic) ExploreCollectionView *exploreCollectionView;
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:#"NewsCollectionView" owner:self options:nil];
self.newsCollectionView = [arrayOfViews objectAtIndex:0];
[self.view addSubview:self.newsCollectionView];
The result of that code is a message stating "Can't add self as subview".
My second question is about switching between the two collectionViews. I imagine it will go something like this:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item == newsEventsTab)
{
[UIView transitionFromView:self.exploreCollectionView
toView:self.newsCollectionView
duration:0.25
options:UIViewAnimationTransitionFlipFromRight
completion:nil];
}
if(item == exploreTab)
{
[UIView transitionFromView:self.newsCollectionView
toView:self.exploreCollectionView
duration:0.25
options:UIViewAnimationTransitionFlipFromRight
completion:nil];
}
}
However I have no idea if that would work because I can't even get the views to show up yet. Please help me out, thanks!!
I ended up accomplishing my task by creating a containerView inside my HomeViewController, and connecting it via IBOutlet.
In order to set up my collection views I did this:
self.newsCollectionView = [[NewsCollectionView alloc] initWithNibName:#"NewsCollectionView" bundle:nil];
self.newsCollectionView.view.frame = self.collectionContainer.bounds;
self.exploreCollectionView = [[ExploreCollectionView alloc] initWithNibName:#"ExploreCollectionView" bundle:nil];
self.exploreCollectionView.view.frame = self.collectionContainer.bounds;
[self addChildViewController:self.newsCollectionView];
[self addChildViewController:self.exploreCollectionView];
[self.collectionContainer addSubview:self.newsCollectionView.view];
[self.newsCollectionView didMoveToParentViewController:self];
[self.exploreCollectionView didMoveToParentViewController:self];
The view transition is as I described in my original post, except you have to reference the actual view instead of the controller (so self.newsCollectionView.view instead of just self.newsCollectionView).
Works like a charm.
Related
I will start off by describing what I'm trying to accomplish and then follow by describing what I've tried already. I am pretty sure what I've tried is NOT the best approach so please correct my approach as needed!
I have a ViewController.m and a Custom View which is laid out in CustomView.xib. The custom view has UIButtons and UILabels which are populated from an Array of Custom Objects.
The user flow should go as such: ViewController starts off showing CustomView with its labels populated by
CustomObjectArray[0] -> User presses button -> Another "copy" of CustomView slides into the view, over the previous version.
It's labels and buttons are populated by CustomObjectArray[1] -> User presses button -> repeat until end of Array.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So far I made a ViewController.m/h, a CustomView.m/h AND a CustomView.xib file. I used interface builder to do the layout.
On the "Custom Class" tab for the top-level View in XIB file, I type in "CustomView". I also drag IBOutlets from the XIB file to the CustomView HEADER (.h) file.
In the ViewController, under -(instancetype) init method, I create a custom view using the normal initWithNib method. And then I do:
self.view = CustomViewVariableName;
When I run the program, the view show's up fine. However, when I try to selector's, nothing's getting recognized by the buttons:
[currentCustomView.continueButton addTarget:self action:#selector(continueButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
What did I do wrong here?
More importantly, given my described goals up top, am I even doing this right? Do I need to have CustomView.m/h files? Or can I do the same thing with ONLY the XIB and the ViewController file. Remember that I need to have "multiple copies" and slide them on top of each other until the end of my custom objects array.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Some more code as requested.
In ViewController:
- (instancetype)init
{
currentCustomView = [[[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:nil] objectAtIndex:0];
self.view = currentQuizQuestionView;
.......
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[currentCustomView.continueButton addTarget:self action:#selector(continuePressed:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)continuePressed:(id)sender{
NSLog(#"Current position");
//[self moveInQuestion];
}
In CustomView.h:
#import <UIKit/UIKit.h>
#interface CustomView : UIView
#property (weak, nonatomic) IBOutlet UIButton *continueButton;
#end
In CustomView.m:
this is just the default page, I added nothing in this file.
CustomView.xib:
I'm not sure if I fully understand your requirements. Let's assume you would like to have several CustomViews as subviews of your ViewController and those subviews can be display each by each after pressing its own button.
First thing about adding subview:
self.view = CustomViewVariableName;
Since CustomsViews will be subviews according to my assumption, above line is an error. You probably would like to have something like this.
for (NSInteger i = 0; i < 5; i++) {
CustomView *v = [[CustomView alloc] initWithNib];
[v.button addTarget:self
action:#selector(continuePressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:v];
}
Second, I noticed you would like to have an array as a handler to refer all the subviews. We can add above CustomView into a mutable array by insert one more line into above for-loop.
[_customObjectArray addObject:v.button];
Finally, I created a simple project and tried to implement things you mentioned. Maybe you can take it for a reference. https://db.tt/Och2tzyG
I am having what seems like a typical Container View problem in iOS. I have a ViewController with two subviews: a UISegmentedControl and a Container View. Now having placed my Container View, in the storyboard, I am not sure how to proceed. Naturally I thought my next step was to subclass UIContainerView to do all the stuff that I read in the iOS Documentation. But there is no such class as UIContainerView. So now, beyond what I was able to place in the storyboard, I am stuck. Hoping someone can help me I will posit what seems like a simple scenario.
Imagine:
One ViewController with two buttons (Cat, Dog) and a ContainerView.
When user clicks on catButton, then the ContainerView should show the CatViewController (and do similarly for dogButton)
Image that already I have the storyboard setup.
For simplicity, let CatViewController contain a single UILabel with the word CAT (and similarly for DogViewController).
also, in the storyboard, I have already created CatViewController and DogViewController as two stand-alone, unreachable, View Controllers.
So at this point, how do I proceed? Since I cannot subclass such a class as UIContainerView, what do I do?
I believe this scenario is simple enough for someone to provide an example, but if you deem it too complicated, please provide an example to yet a simpler scenario. I just want to see how a simple one is done.
P.S. I have already taken a tour here on StackOverflow, such as:
Swapping child views in a container view
and I have already read the docs at https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html#//apple_ref/doc/uid/TP40007457-CH18-SW6
I think is better use an UISegmentedControl instead two UIButtons.
The container view subviews (_vwContainer.subviews) contains initially the CatViewController's view, automatically instantiated.
// ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIView *vwContainer;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_vwContainer.clipsToBounds = YES;
}
- (IBAction)onSegmentValueChanged:(UISegmentedControl *)sender {
NSLog(#"Value changed to: %zd",sender.selectedSegmentIndex);
NSLog(#"BEFORE: self.childViewControllers: %#",self.childViewControllers);
NSLog(#"BEFORE: _vwContainer.subviews: %#",_vwContainer.subviews);
// set oldVC & newVC
UIViewController *oldVC = self.childViewControllers.firstObject;
NSString *strIdNewVC;
switch (sender.selectedSegmentIndex) {
case 0: strIdNewVC = #"catVC"; break;
default: strIdNewVC = #"dogVC";
}
UIViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:strIdNewVC];
//
[oldVC willMoveToParentViewController:nil];
[self addChildViewController:newVC];
// Prepare animation transition, for example left to right
newVC.view.frame = oldVC.view.frame;
CGPoint pntEnd = oldVC.view.center;
CGPoint pntInit = pntEnd;
pntInit.x += oldVC.view.frame.size.width;
newVC.view.center = pntInit;
[self transitionFromViewController:oldVC toViewController:newVC
duration:0.25 options:0
animations:^{
newVC.view.center = pntEnd;
} completion:^(BOOL finished) {
[oldVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
NSLog(#"AFTER: self.childViewControllers: %#",self.childViewControllers);
NSLog(#"AFTER: _vwContainer.subviews: %#",_vwContainer.subviews);
}];
}
#end
I have a UINavigationController/UITableView and I want to present a UIView over top of it when the table is empty to give the user a prompt on how to add items to the table.
I've never make a UIView (as opposed to a UIViewController before) so I'll step through what I did to make it:
Make a new UIView Class - MakeSentenceHelperView
Make a nib called MakeSentenceHelperView.xib
Set File's owner to MakeSentenceHelperView
Load the nib in the MakeSentenceHelperView:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(#"makesentencehelperview init");
[[NSBundle mainBundle] loadNibNamed:#"MakeSentenceHelperView" owner:self options:nil];
}
return self;
}
and present the MakeSentenceHelperView in the UITableViewController:
//present the placeholder view for sentences
MakeSentenceHelperView *makeSentenceHelperView = [[MakeSentenceHelperView alloc] init];
NSLog(#"present placeholder: self.navigationcontroller.view: %#", self.navigationController.view);
//Something like this:
[self.navigationController.view addSubview:makeSentenceHelperView];
[self.navigationController.view bringSubviewToFront:makeSentenceHelperView];
The class loads and logs ok, but nothing appears in front of the UITableView - where have I gone wrong?
UPDATE: if I add [self.tableView setHidden:YES]; then the tableview disappears and the space is black and empty. I'm assuming this means I'm setting up the View wrong, somewhere.
You can use https://github.com/ecstasy2/toast-notifications-ios/ for showing Toast view liek Android. Check array size and if table view is not showing then called this one and show any custom method.
Thanks to #Aadhira for their link which led me to the problem.
I needed to add awakeFromNib
I was missing [self addSubview:self.view]; at the end of initWithFrame.
I have a main view with 3 buttons. Clicking on any of the buttons adds a SubView.
The buttons have different titles and are all linked to IBAction "switchView"
The "switchView" code is below.
- (IBAction)switchView:(id)sender{
secondView *myViewController = [[secondView alloc] initWithNibName:#"secondView" bundle:nil];
[self.view addSubview:myViewController.view];
}
The "secondView" loads up correctly and everything works well.
The problem is I want to be able to know which button was the Sender.
I don't want to create 3 subviews, one for each button. The code and XIB would be absolutely the same>
The only difference would be a variable that I would like to set up in the second view (viewDidLoad method) depending on who is the Sender (which button was clicked)
Is this possible? Or I would need to create 3 subViews - one for each button?
Your help is greatly appreciated!
You can identify different buttons with the tag property.
e.g. with your method:
-(IBAction)switchView:(id)sender {
UIButton *button = (UIButton*)sender;
if (button.tag == 1) {
//TODO: Code here...
} else if (button.tag == 2) {
//TODO: Code here...
} else {
//TODO: Code here...
}
}
The tag property can be set via the InterfaceBuilder.
Hope this helps.
I think you can solve in 2 ways:
Create a property like:
#property (nonatomic, strong) IBOutlet UIButton *button1, *button2, *button3;
in your viewcontroller and link the buttons to them as referencing outlet on the XIB.
Give a different tag to each button on your xib and ask for the tag of the sender with UIButton *b=(UIButton*)sender; b.tag; like Markus posted in detail.
Solving my problem it all came down to transferring data between the mainView and subView.
In my mainView.h I declared an NSString and its #property
...
NSString *btnPressed;
}
#property(nonatomic, retain) NSString *btnPressed;
...
then in my mainView.m inside the switchView method I did this:
- (IBAction)switchView:(id)sender{
secondView *myViewController = [[secondView alloc] initWithNibName:#"secondView" bundle:nil];
btnPressed = [NSString stringWithFormat:#"%i", [sender tag]];
[myViewController setBtnPressed:self.btnPressed];
[self.view addSubview:myViewController.view];
}
This line in the code above actually takes care of transferring the data to the newly created subView:
[myViewController setBtnPressed:self.btnPressed];
Then in my secondView.h I declare exactly the same NSString *btnPressed and its #property (though this a completely different object than the one declared in main)
Then in my secondView.m I get the value of the button pressed I'm interested in.
- (void)viewDidLoad {
[super viewDidLoad];
int theValueOfTheButtonPressed = [self.btnPressed intValue];
}
This works well.
Don't forget to #synthesize btnPressed; as well as [btnPressed release]; in both mainView.m and secondView.m
Ok, here's another question.
I am creating a UIView called ProgressView that is a semi-transparent view with an activity indicator and a progress bar.
I want to be able to use this view throughout different view controllers in my app, when required.
I know of 3 different ways of doing this (but I am only interested in one):
1) Create the entire view programatically, instantiate and configure as required. No worries I get that one.
2) Create the UIView in interface builder, add the required objects and load it using a method like the below. Problem with this is that we are basically guessing that the view is the objectAtIndex:0 because nowhere in the documentation I found a reference to the order of the elements returned from the [[NSBundle mainBundle] loadNibName: function.
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"yournib"
owner:self
options:nil];
UIView *myView = [nibContents objectAtIndex:0];
myView.frame = CGRectMake(0,0,300,400); //or whatever coordinates you need
[scrollview addSubview:myView];
3) Subclass UIViewController and let it manage the view as per normal. In this case I would never be actually pushing the view controller onto the stack, but only its main view:
ProgressViewController *vc = [[ProgressViewController alloc] initWithNibName:#"ProgressView" bundle:nil];
[vc.view setCenter:CGPointMake(self.view.center.x, self.view.center.y)];
[self.view addSubview:vc.view];
[vc release];
As far as I can tell, #3 is the the correct way of doing this (apart from programatically) but I am not entirely sure if it is safe to release the ProgressView's view controller whilst another controller's view is retaining its main view (gut feel says it is going to leak?)?
What do I do in terms of memory management in this case, where and when should I release the ProgressView's view controller?
Thanks in advance for your thoughts.
Cheers,
Rog
I think that your solution #3 adds unnecessary complexity by introducing a UIViewController instance just as a container for your ProgressView so that you can setup nib bindings. While I do think that it is nice to be able to work with an IBOutlet bound property rather than iterating through the nib contents you can do so without introducing a UIViewController whose behavior you neither need nor want. This should avoid your confusion around how and when to release the view controller and what, if any, side effects it might have on the responder chain or other behaviors of the loaded view.
Instead please reconsider using NSBundle and taking advantage of the power of that owner argument.
#interface ProgressViewContainer : NSObject {
}
#property (nonatomic, retain) IBOutlet ProgressView *progressView;
#end
#implementation ProgressViewContainer
#synthesize progressView = progressView;
- (void) dealloc {
[progressView release];
[super dealloc];
}
#end
#interface ProgressView : UIView {
}
+ (ProgressView *) newProgressView;
#end
#implementation ProgressView
+ (ProgressView *) newProgressView {
ProgressViewContainer *container = [[ProgressViewContainer alloc] init];
[[NSBundle mainBundle] loadNibNamed:#"ProgressView" owner:container options:nil];
ProgressView *progressView = [container.progressView retain];
[container release];
return progressView;
}
#end
Create a nib named "ProgressView" containing a ProgressView and set it's File's Owner class to ProgressViewContainer. Now you can create ProgressViews loaded from your nib.
ProgressView *progressView = [ProgressView newProgressView];
[scrollView addSubview:progressView];
[progressView release];
If you have multiple configurations of your progress view then maybe you'll want to implement a -initWithNibNamed: method on ProgressView instead of +newProgressView so you can specify which nib to use to create each ProgressView instance.
I vote for option #2. The return value from -[NSBundle loadNibNamed] is an array of the top-level objects. So as long as you have just one top level object in your nib, then the index 0 will be correct. The other views are subviews and not top level objects.
Another option of course is to do something like create a superclass for all of your view controllers that includes an outlet called something like 'progressView' and then connect your view to that outlet on file's owner in the nib. Seems like overkill for this, though.
I also prefer alternative #2. If the "0" is bothering you, you could:
Create a subclass of UIView called ProgressView
Create a nib-file called ProgressView.xib describing your progress view.
Select the topmost view in your nib, and set its Class to ProgressView in interface builder
then do
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"ProgressView" owner:self options:nil];
ProgressView *progressView = nil;
for (UIView *view in nibContents) {
if ([view isKindOfClass:[ProgressView class]]) {
progressView = (ProgressView *) view;
break;
}
}
if (progressView != nil) {
//Use progressView here
}
I ended up adding a category to UIView for this:
#import "UIViewNibLoading.h"
#implementation UIView (UIViewNibLoading)
+ (id) loadNibNamed:(NSString *) nibName {
return [UIView loadNibNamed:nibName fromBundle:[NSBundle mainBundle] retainingObjectWithTag:1];
}
+ (id) loadNibNamed:(NSString *) nibName fromBundle:(NSBundle *) bundle retainingObjectWithTag:(NSUInteger) tag {
NSArray * nib = [bundle loadNibNamed:nibName owner:nil options:nil];
if(!nib) return nil;
UIView * target = nil;
for(UIView * view in nib) {
if(view.tag == tag) {
target = [view retain];
break;
}
}
if(target && [target respondsToSelector:#selector(viewDidLoad)]) {
[target performSelector:#selector(viewDidLoad)];
}
return [target autorelease];
}
#end
explanation here: http://gngrwzrd.com/blog-view-controller-less-view-loading-ios-mac.html