Did receive memory warning when alloc UIButton and UILabel - ios

This is the situation:
I've a UIScrollView (called A) that scrolls horizontally. For this scroll view the property pagingEnabled is setted to YES. On the right of the screen I've a UIButton, that's attached to self.view and doesn't scroll with scroll view.
When tapped, this button shows a UIView with another UIScrollView (called B) inside that scrolls vertically. This scroll view contains a list of UIButton and UILabel. Every UIButton as an UIImage as backgroundImage. I used UIButton instead UIImageView because in this way is easy to manage the touch event on the image. The UILabel contains a description for every image.
The problem:
For every page in A scroll view, I need to change the UIButton contained in the B scroll view, because every page has a different image list. So, in the scrollView:didEndScrollingAnimation scroll view delegate method I remove the old UIButton and the old UILabel from the view and I remove also from the NSArray in which they're contained.
The I create a new list of UIButton and UILabel, I add to self.view and insert in the array, with [array addObject:].
After a variable number of scroll the app receive a memory warning message and the will killed from the system.
I use ARC, so is almost impossible to understand if the old object are deallecated (and in any case would be very difficult because they are system objects).
Any ideas ?

What I would do in your case, is the same I recommend to others:
Use profile and analyse the difference between heaps. This way you are able to check the memory footprint of your application between each swipe.
For a basic tutorial of how to do it, you can check this. I know this is a bit vague and you would want a objective answer, but the thing is that, what you think is that the problem right now, might not correspond to the real issue in your application. If you need further help after analysing your heap, just edit your question and let us know.
And this is the original article I used, from Mr. bbum.

Related

UIButton not receiving IBAction

I have what should be a very simple thing to do. I'm working on someone else's code, and I want to enlarge a UIButton because it's too small for users. I made it bigger in the storyboard, but when I run the app, the associated IBAction only gets hit when touching where the original rectangle was before I changed it. The button is still visibly larger, but only a portion of it receives touch events. Does anyone know what else might be at play here?
Note: there are no views on top of the new area that the button occupies, so I don't think the touches get picked up by a view on top.
Something to check is whether the UIButton has an ancestor view (i.e. a view in its superview chain) that is the smaller size. Hit-tests only pass down the view hierarchy if the touch is contained within the view so a smaller superview will stop the touches outside its bounds, even if the touch is inside the button.
Is the IBAction hooked up to "touch up inside" in Interface Builder/Storyboard? I've made mistakes where I hook it up with a different kind of event, which exhibits behaviours like you're experiencing.
Found the issue. There was a view being programmatically added on top of the button. It's origin.x was being hardcoded to where the buttons width use to end.

Trying to add UIImage as background in storyboard (without overlapping my other ViewController elements)

I am trying to add an UIImage to my ViewController in the Interface Builder without overlapping all my content. Is there a way to do so? Whenever I place it you can kind of see through it, but I just wanted to hide it if possible (without deleting it) because it becomes bothersome to rearrange the position of the other elements (labels, buttons, views, etc). I know I can do it programmatically (which I've done so) but I just wanted to know if there was an IB alternative.
As an aside question I would like to know how does iOS handles the size of the images (resolution), because the image I am assigning as my VC background (programmatically) it's high resolution, and UIImage does not have a property .contentMode, therefore I cannot assign it as aspect fit.
Thank you for your help in advance!
Cheers!
Drag the image view behind other views in the left panel for the view controller in IB.

Elusive UIView Subclass Name

This one is really bugging me! What is the name of this UIView subclass? I'm not talking about the compass itself, but the two dots at the bottom of the view. I know it's not a private API because I have seen it before. Or am I confused and this is not a UIView at all, but a UIViewController. Which UIView / UIViewController subclass is shown here. It acts like a UIScrollView, but has distinct pages, and has the dots at the bottom of the screen that show the users relative progress through the pages. I have checked this link about UIView subclasses, but became lost after about the 45th one. http://www.themusingsofalostprogrammer.com/2010/09/list-of-every-uiview-subclass.html
(source: tqn.com)
Thankyou for your time.
It is a UIPageControl. It corresponds (or is supposed to correspond) to the number of "pages" the user can scroll to, sideways. Normally, it indicates how many pages there are, and which one we are on, plus it typically provides a way to scroll sideways (by tapping to its left or right).
If I may add to what matt said...
To use a UIPageControl effectively, you also need a UIScrollView that contains the content. An update to the page control should result in a change to the contentOffset of the scrollView as shown in the code below. UIScrollView has a pagingEnabled property that should be set to YES to complete the illusion of paging.
- (IBAction)pageValueChanged:(UIPageControl *)sender
{
// self.pagedView is an IBOutlet to a UIScrollView
[self.pagedView setContentOffset:CGPointMake( sender.currentPage * 320, 0 ) animated:YES];
}

Using interface builder to layout a scrolling view with images and text

I have a requirement in my app to display a bunch of information that includes both text and images. It will be quite long, so it will need to be scrollable to access all the content.
I know that I can achive this by programmatically adding different UILabels, UIImages etc to a UIScrollView. But this is a proof of concept, so I'm looking for something a little quicker than having to work out all the positioning and code required. The information is static anyway, and does not need to interact with code.
Is there a way to do this using the interface builder (storyboard or xib is fine)?
you definitely can do that if you simply want a quick interface
1.> you might need to know how long is your scroll view, for example in my case, i set it to 1568
2.> Then i drag all the controls that will fit for the first 568 pixel view onto the scroll view and position them.
3.> Then change the Y value for that scroll view to something like - 500, so you can see the rest of the scroll view, and put everything you need there.
4.> After you have all your controls, and remember to set the frame back to 0,0,320,568
5.> last step, in your code, set SCROLLVIEW.contentSize = CGSizeMake(320, 1568);
I would still suggestion don't hard code all those values, but if you are looking for a quick way to do your interface, hope that gives you some ideas.
Just start a new project with a single view, it will come with a xib or storyboard for its single ViewController.
Create a UIView by dragging it into the workspace and place as many Labels, Images and UI Elements as you want.
Open the xib / storyboard and drag a UIScrollView in as your root VC's root view. Drag the view containing your layout into the scrollview, making it the scrollviews only subview.
Done (almost)!
If you launch your app at this point, you'll notice you can't scroll. That is because the scrollview is "too stupid" to adjust the size of its contentSize property on its own.
You'll need some code here, but it is only a tiny snippet and you won't need to touch it again:
Create a new Category on UIScrollView.
In your category's implementation, do:
#implementation UIScrollView (MyHandyCategory)
-(void)awakeFromNib {
NSArray *subViews = [self subviews];
UIView *contentView = [subViews objectAtIndex:0];
[self setContentSize:contentView.frame.size];
}
#end
Done (for real this time)! This will check the size of the view your scrollview contains and ajust the contentSize after it has been initialized. You can change the size of your content view as you like, no need to play around with hardcoded values or even Interface Builder values!
If it’s just proof of concept I’d have a WebView and a local HTML page you load. Easy-peasy.
I would suggest UICollectionView. It's fairly straightforward. There's a good tutorial here:
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

When moving a subview around, how do I keep a subview of it in the same place in its superview?

That's a very convoluted title. Sorry.
Basically, I have a UIView that I've attached to a UITableViewCell so I can slide the cell around, or at least create the tric-- illusion of it. Within that UIView I have a UIImageView. I want to keep this UIImageView static relative to the view controller. So basically when I slide the cell around, I want everything about the cell to slide around with my finger (I have this done) EXCEPT the UIImageView. I want it to seem to the user like it's staying in one place -- which means to the cell's superview (the view controller's view) it's staying in one place.
What would be the easiest way to build this? I'm sorry I don't have code to share, I'm just trying to build this from a conceptual level first. My UIView on the UITableViewCell is slid with a UIPanGestureRecognizer and moving the UIView's frame if that helps.
Would I have to move it in the opposite direction of the UIView when it moves (say it moves one forward in x, move it one back in x?) or is there a way where I can just say "lock your position relative to your super-superview"?
I've had the same problem before.. what I did was simply not add the UIImageView as a subview to the UITableViewCell, and instead attach it to the parent of the UITableViewCell.. that way whatever happens to the UITableViewCell frame properties (ie size or origin) won't impact your UIImageView..
you may wonder: well one of the reasons I attach the UIImageView as a subview to the tableCell is b/c it's related to it in many different ways.. The response to that is that you can associate an object with another through objc_setAssociatedObject
If you come from web dev background, you can think of this discussion as the difference between position:absolute and position:relative discussion.
Initially I tried to do what you had in mind, basically moving them in opposite directions etc.. I just think my solution is a lot simpler to do.
abood was close, but close, but if you simply make the imageview a subview of the cell itself instead of the UIView I added over the cell, they operate independently. So contrary to the first line, adding it as a subview of UITableViewCell worked perfectly and didn't have any of the issues outlined. Works perfectly now.

Resources