I have a main view where I initialize severals arrays and released them in viewDidUnload. So when I change to other view where I have the instruccions and I come back to the main view all the array are nil again.
What I what to do is: when the app is lunched, the arrays are initialized, y use them, I could go to others views and when come back to the main view that the arrays keep the values, and only when the app is close then release all the arrays.
How do I have to do it?
Just release them in -dealloc and initialize them in viewDidLoad. That way theyre only released or nullified when the view shuts down and when it is reloaded, they are reinitialized.
Hope this helps.
You can use different approaches for your problem. I can tell you some ways.
You can write methods for view initialization which will take NSArray as parameter. Like:
- (id)initWithArray:(NSArray *)myArray {
[super init];
//here you can store an NSArray inside this ViewController in #property. For ex:
currentArray = myArray;
return self;
}
You can use global storage like CoreData and use NSManagedObject class to define instances for necessary object in every view.
You can use delegate approach. For example you can delegate methods from second view in first view.
Related
I have a UITableViewController that can display content from any of three different arrays. In Objective-C, what I would typically do is set a property on the TableVC that is a reference to the array to be displayed. Then the TableVC would not only be able to show the contents of that array, but also handle user-directed editing of the array, say, if they deleted or re-ordered elements.
The way it seems to work in Swift when I set a property on my TableVC to an array is that it is a copy of my model's array, not a reference. And while this is fine in certain contexts, in my application it seems bad for two reasons: 1) my arrays are enormous, thousands of elements, and copying huge arrays over and over again seems wasteful; 2) small edits are tougher to handle: I have to communicate back to the model about element deletion or re-ordering and make sure my view and model arrays stay in sync.
Setting a property that's a reference to an array is what I want to do, but there's no such thing as an inout object property in Swift.
What is the correct way for me to handle this?
Thanks.
Swift Arrays are not actually copied every time you assign them; instead, they are copy-on-write. What this means is that the array is only copied when something mutates it; until that point, you only copy a reference, like with Objective-C.
With that said, if you need reference semantics, the NSArray class from Objective-C is still available in Swift. You can also create your own class type that wraps an Array if you prefer not to use the Objective-C bridge.
I'm using eloquent relationships.
When I call $carcollection = $owner->cars()->get(); I have a collection to work with. So let's say that I have, for this particular owner, retrieved three cars. The collection is a collection of three arrays. Each array describes the car.
This is all working fine.
Now I want to add more attributes to the array, without breaking the collection. The additional attributes will come from a different source, in fact another model (e.g. servicehistory)
Either I retrieve the other model and then try merge() them, or I try manipulate the arrays within the collection without breaking the collection.
All this activity is taking place in my controller.
Is one way better than another, or is there a totally different approach I could use.... perhaps this logic belongs in the model themselves? Looking for some pointers :).
Just to be specific, if you do $owner->cars()->get(); you have a collection of Car Models, not array.
That have been said, you can totally load another relation on you Car model, using
$carcollection = $owner->cars()->with('servicehistory')->get();
$carcollection->first()->servicehistory;
You can try to use the transform method of the collection.
$cars = $owner->cars()->get();
$allServiceHistory = $this->getAllService();
$cars->transform(function($car) use($allServiceHistory) {
// you can do whatever you want here
$car->someAttribute = $allServiceHistory->find(...):
// or
$car->otherAttribute = ServiceHistoryModel::whereCarId($car->getKey())->get();
});
And this way, the $cars collection will be mutated to whatever you want.
Of course, it would be wiser to lazy load the data instead of falling into an n+1 queries situation.
I would like to release memory in arrays with photos when transitioning from a viewController. I have tried autoreleasepool like the following, but it does not work. Will you teach me how to release memory? Thanks.
autoreleasepool{
imageArray = []
}
An object will only be retained if another object has a strong reference to it. As soon as your view controller disappears, it will most likely be deallocated as well, which automatically removes its strong references to other objects. Thus, if imageArray is strongly referenced only by your disappearing view controller, the memory will automatically be released. You do not need to use an autoreleasepool.
Can anybody point me in the right direction to how I could use UICollectionViewLayout to create an interface similar to the Pinterest column layout?
I tried searching online, but it looks like there are not many examples out there yet.
The 1000memories "Quilt" view is pinterest-like and open source: http://blog.1000memories.com/168-opensourcing-quilt, and you can dig through that to see how it works.
If you're looking for a more conceptual overview, here's the basic idea of what you're going to want to do. The easiest thing by far, if you just need a Pinterest-style layout, is to subclass UICollectionViewFlowLayout. You get a lot of layout help from this class, and Pinterest style is within its capabilities. You only need to override one method.
Set up a normal UICollectionView using UICollectionViewFlow layout. A quick way to do this is:
Drag a UIViewController onto a storyboard, drop a UICollectionView on that. Set the classes to match your custom classes, etc. You can use a delegate and create a delegate class here but strictly speaking that is not necessary to achieve JUST the Pinterest flow layout (you will almost definitely want to break the selection responsibility stuff into a delegate class in reality though).
Stub out a data source. Implementing the data source protocol for UICollectionView (http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewDataSource_protocol/Reference/Reference.html) is trivially simple. Make sure you set a reuse identifier on your UICollectionViewCell. You need:
(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
just return 1 for now;
(NSInteteger)collectionView:numberOfItemsInSection:
hardcode a number for now, make it 20.
– (UICollectionViewCell *)collectionView:cellForItemAtIndexPath:
This is one of the places where subclassing the flow layout's gonna do you a favor. All you really need to do here is call dequeueReusableCellWithReuseIdentifier:forIndexPath: with the index path. If you added a UIImageView or some labels to the cell, this would be a great place to actually assign the image, text, etc.
In the viewController's viewDidLoad instantiate a UICollectionViewFlowLayout and set the UICollectionView's datasource to yours and layout to flowlayout. Remember, this class is a subclass of UICollectionViewViewController.
self.collectionView.dataSource = [[YourDataSource alloc] init];
self.collectionView.collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
Ok. At this point you should be able to run your app and see some stuff on the screen. This is a whirlwind overview. If you need more details about how to set up ViewControllers and so on there's tons of stuff available about that.
Now comes the important part, Pinterest-izing the flow layout.
First, add a new class that is a subclass of UIViewControllerFlowLayout. Change your ViewController's viewDidLoad to instantiate this class and assign as the UICollectionView's collectionViewLayout.
The method you are going to need to implement is - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect.
Here's the thing: The superclass is going to do almost all the work for you. Your code is going to look something like this:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
[attributes enumerateObjectsUsingBlock:^(id attr, NSUInteger idx, BOOL *stop) {
float newYCoord = [calculationMethodYouHaveToWriteFor:attr.frame];
attr.frame = CGRectMake(attr.frame.origin.x, newYCoord, attr.size.width, attr.size.height];
}];
}
Pinterest uses fixed-width columns, all you need to do in your calculation method is figure out what column you are in (`attr.origin.x / _columnWidth), and look up the total height in that column from the ivar you've been saving it in. Don't forget to add it to the new object's height and save it back for the next pass.
The flow layout superclass handles: making cells, determining which cells are visible, figuring out the contents size, figuring out the arrangement of the rows in the x direction, assigning index paths to cells. Lots of junk. And overriding that one method lets you fiddle with the y-pos to your heart's desire.
Heres two from github
https://github.com/jayslu/JSPintDemo
https://github.com/chiahsien/UICollectionViewWaterfallLayout
I've used a modified version of Waterfall in a project now, and I'm investigating JSPint now.
I have created a custom uicollectionviewlayout which is used in my personal project. Here is the link. Hope it helps.
https://github.com/johnny0614/YJZAlbumCollectionViewLayout
You can get anything you want from here:
https://github.com/ParsifalC/CPCollectionViewKit
For example(Both these two layouts are custom UICollectionViewLayout):
I have read all of the posts about passing data from one view controller to another but I am still at a loss as to how that is done. Simply put, I have an array that is built in my MainViewController and need to use that array in my DisplayViewController.
I am simply doing the following
//DisplayViewController
self.items = mainViewController.items
I would think that this notation would work but I keep getting the following error:
Request for member 'mainViewController' in something not a structure or union.
Since I am a real newbie I cannot really understand some of the answers that I read. So if someone can put it in laymans terms, I would really appreciate it!!!
Usually it's done the other way around. Just before you push your DisplayViewController on the stack (or however you make it visible), you set its items property.
detailViewController.items = self.items;
[self.navigationController pushViewController:detailViewController animated:YES];