Saving data iphone - cocoa

In my app people can read newsmessages and add a reaction etcetra. The articels are loaded from JSON/XML into a UITableview.
I want to make a new UITableviewController that's called 'favorites' the idea is to put under each article an button to save the message into their 'favorites' in the app.
Does anyone know how to do that? I just need some tips! Do i need to save data in a .plist or do i need to use userdefaults?
Thanks

You can definitely do it either way.
For me, plist saving/loading is at most times easier, but that's just a matter of taste :
NSMutableArray* favorites = [[NSMutableArray alloc] initWithContentsOfFile:filename];

Related

What is the best way to develop applications using the auto layout in Xcode?

So I guess this is going to be closed for being too subjective and too opinion based but if anyone can help me I would appreciate it.
I got a question. If I have a few controllers that all have almost the same thing For example they have the same background, have a menu going around the edge but the actual content is different. I had a couple of ideas. 1) Just have one view controller and just kill the objects for that current view if the user chooses a different option on the menu and spawn the new objects for that menu. My issue with this way was that I could't find a way to use the auto layout with this.
Second way would to be have a function in a .swift file that I can call and it creates an image view and sets up the menu an everything like that. I have the opposite issue here though, now the auto layout won't work.
App devs must have a way of doing this, I'm just probably thinking of this completely the wrong way.
Is there a better way to be doing this - I am sure there is? I would appreciate it if someone could point me in the correct direction.
Thanks
EDIT:
I should make it clear that the language I am using is swift.
You can create custom container view controller and swap the view controllers for the part that change according to the user selection.
--Adding Example--
e.g iPad's Settings app. The left side is a table view and right side is detail view which changes on user selection. So Tableview can be wrapped in a view controller let's say ListViewController. This will not change. The right side will be DetailViewController which would be swapped according to user selection. Your ContainerViewController will have 2 view controllers at all times.
Here is how to add view controllers as child and set their views in objective-c.
- (void) setupContentViewControllerWith: (DetailViewController*) detailViewController andListViewController:(ListViewController*)listViewController {
[self addChildViewController:listViewController];
[self addChildViewController:detailViewController];
listViewController.view.frame = CGRectMake(kListView_X, kListView_Y, kListView_Width, kListView_Height);
detailViewController.view.frame = CGRectMake(kListView_Width, kDetailView_Y, self.view.bounds.size.width, self.view.bounds.size.height-kDetailView_Y);
[self.scrollContainer addSubview:listViewController.view];
[self.scrollContainer addSubview:detailViewController.view];
[self.scrollContainer setContentSize:CGSizeMake(kListView_Width+self.view.bounds.size.width, self.view.bounds.size.height)];
}
When user selects new item from the list, you can swap DetailViewControllers as below
- (void) replaceEpisodeControllerWith:(DetailViewController *)detailViewController {
detailViewController.view.frame = CGRectMake(kListView_Width, kDetailView_Y, self.view.bounds.size.width, self.view.bounds.size.height-kDetailView_Y);
[UIView transitionFromView:currentDetailViewController.view
toView:detailViewController.view
duration:0.0
options:UIViewAnimationOptionTransitionNone
completion:^(BOOL finished)
{
[currentDetailViewController.view removeFromSuperview];
[currentDetailViewController removeFromParentViewController];
[currentDetailViewController release];
currentDetailViewController = detailViewController;
}];
}
I don't have swift version of this.

Xcode UIRefreshControl on UIView

I am building an App that needs to be able to refresh by pulling, and then re-run the ViewDidLoad.
So I know i could use a UIRefreshControl, but I can only find code that is used in a UITableViewController.
Has anyone got an idea on how to use a UIRefreshControl in a UIView instead of in a UITableView.
Thanks in advance.
try this…
[myView setNeedsDisplay];
this would reload your view controller. you can put this in a method and call it during pull to refresh…Hope this helps you.
Happy coding ;)
Very easily:
Keep the control as amember or property:
UIRefreshControl *_refreshControl;
Add this to your viewDidLoad method: (simply adding the control to your tableView. make sure the tableView is not nil ofcourse).
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventValueChanged];
[_tableView insertSubview:_refreshControl atIndex:0];
implement the refresh method:
- (void)refresh:(UIRefreshControl *)refreshControl {
[self reloadData];
//Don't forget to stop the refreshing animation after data reloads.
[_refreshControl endRefreshing];
}
UIRefreshControl isn't meant to be used without a table. This note in the documentation specifically warns against such use:
Note: Because the refresh control is specifically designed for use in a table view that's managed by a table view controller, using it in
a different context can result in undefined behavior.
So although it may be possible to get the control to work, you really shouldn't use it without a table. Rolling your own refresh control is a better solution.
Better still, try to engineer your app such that the user doesn't need to refresh the view. The app should know when new data is available. The only excuse for making the user refresh is if doing so automatically would somehow confuse the user or otherwise make the app more difficult to use.

NSArray to display photos inside a UIImageView

I need a little help. I am new to programming and I am trying to create an app that shows photos, like they were a magazine.
I can say I am on the half path.
I am trying to create a NSArray with the name of the photos, so I can display them in a UIImageView.
But, how can I send this message to the UIImageView when the information comes from the NSArray?
Actually, let me explain it more. The user will be able to swipe between the photos. The effect is gonna be the curl one, like iBooks.
The problem is that I do not know how to change the UIImageView to display the photos.
Can I create a viewcontroller to each photo and then add the UIViewAnimationTransitionCurlDown to change between them? What's the easiest option?
Well, hope you guys can help me.
Thanks!
You should look into UIPageViewController. Essentially, you would have a main view controller that contains the array of photos. This would have a child view controller that is a page view controller. The page view controller would refer back to the main view controller for view controllers to display, and the main view controller would provide it with view controllers that contain image views initialized however the main view controller wants it. Take a look at Apple's tutorial, as well as this tutorial, for more information.
Hope this helps!

Select row in UITableView

I'm pretty new to xcode development and I cannot figure out how to select a row and give label/action I want. I'm using something i found on the internet, sample code with a customized uitableview with 3 rows:
opLabel1.text = [NSString stringWithFormat:#"HeliosMedica",[indexPath section]];
bottomLabel1.text = [NSString stringWithFormat:#"Centrumedical.", [indexPath section]];
I want each row to have different content and also give them a hyperlink. Can anyone tell me where how I can do that.
Appreciate very much your help.
Read the Apple Documents for UITableViewDelegate Protocol Reference. You want to understand some of the basic methods there, including tableView:didSelectRowAtIndexPath:
You probably already have implemented some of the UITableView class methods, in order to get a table to just show up on our screen, including numberOfSections, numberOfSections:and insertRowsAtIndexPaths:withRowAnimation:. These are called by the tableview object and your code executes to meet the tableView's needs.
Here is some sample code you can start with to open a url when the tableView:didSelectRowAtIndexPath:method is executed.
myURL = [arrayOfURLs objectAtIndex:indexPath.row];
[[UIApplication sharedApplication] openURL:myURL];
You will have to initialize the array, first.

iOS layout; I'm not getting it

Well, "not getting it" is too harsh; I've got it working in for what for me is a logical setup, but it does not seem to be what iOS deems logical. So I'm not getting something.
Suppose I've got an app that shows two pieces of information; a date and a table. According to the MVC approach I've got three MVC at work here, one for the date, one for the table and one that takes both these MCVs and makes it into a screen, wiring them up.
The master MVC knows how/where it wants to layout the two sub MVC's. Each detail MVC only takes care of its own childeren within the bounds that were specified by the master MVC. Something like:
- (void)loadView {
MVC* mvc1 = [[MVC1 alloc] initwithFrame:...]
[self.view addSubview:mvc1.view];
MVC* mvc2 = [[MVC2 alloc] initwithFrame:...]
[self.view addSubview:mvc2.view];
}
If the above is logical (which is it for me) then I would expect any MVC class to have a constructor "initWithFrame". But an MVC does not, only view have this.
Why?
How would one correctly layout nested MVCs? (Naturally I do not have just these two, but the detail MVCs have sub MVCs again.)
Thanks all for replying. I will study the links that were provided.
Let me try to explain my issue one more time, hopefully to making it more clear. Do note that I already figured out that my view does not match iOS's, since I do not like where my code is going.
Yes, I'm calling a UIViewController an "MVC", since it for me at the moment implements all aspects of a MVC; it has controller code and an embedded view, plus the controller usually also holds and provides the data (all TableView examples implement it like this).
MVC can be present on many levels; basically a UITextField could (should?) be a MVC; there is a view, but also controller logic involved that you do not want to mix with other code. Encapsulation. For example: Java's Swing JTextField has a MVC. So does a JTable, JList, ... Multiple MVC patterns nested in other MVC's to build a whole screen.
This what I expect when some platform says it uses the MVC pattern. So When I coded the table, I created a MVC and only send the loadData message with a date as the parameter to it. It needs to take care of the rest itself. I have a Detail MVC that can slide in; I then tell it the object it needs to show and it needs to take care of the rest itself. Encapsulation.
So I have a lot of UIViewControllers with embedded UIViews. And that is not the way to do it...
One more potential link is the great talk from WWDC 2010 on MVC.
http://developer.apple.com/videos/wwdc/2010/
It is Session 116 - Model-View-Controllr for iPhone OS
The session is chock full of practical advice on how MVC really works, what makes it tick, why it's good. But it also has a lot of intro stuff to help folks new to the concept to wrap their heads around it.
If I understand your sentence on Java's Swing classes above are you talking about the anonymous classes that respond to events? If so those are not "MVC's", they are what is termed 'Observers', when they observe an event from the view they take some action (usually send a message to a controller). Cocoa Touch uses the Target/Action paradigm (and delegation) to achieve this.
I'd also strongly suggest you take Matthew and Stephen's advice and write a bunch of code. If you don't build that base of intuition, asking the right question (which is most of what is needed to get a good answer) is very difficult.
I really think the WWDC 2010 talk will help.
Good Luck!
If I understand your question -- and I may not, see my comments on it -- I think you're applying the MVC design pattern far too granularly. Most commonly in the setup you describe you'll have a single Model, a single Controller, and multiple Views that are grouped/combined, as in a .xib file.
In Cocoa Touch terms you'd have one UIView that contains a UILabel with the date and a UITableView for your table. These are your Views.
You'll certainly have a Model for the table data, likely an array of data. Your date data might be from its own model if it's a date retrieved from something or calculated or whatever, something entirely separate from the array of data. If it's instead associated with the array data -- they're both pulling from a database, or the date is calculated from the array data, or what have you -- then you have a single Model.
If the data is all coming from a single Model then a single Controller is likely fine. Even if the data is coming from more than one source/Model you likely only need/want one controller in this setup. The UITableView will have a UITableViewController, and that same controller can take care of providing your date as well.
To sum, the Model View Controller design pattern doesn't call for having a bunch of nested sets of models, views, and controllers. They could be, and sufficiently complex projects may call for it. Broadly, though, you'll have a controller that's associated with a model and one or more views, and that set of objects works together to provide a piece of functionality.
Tbee,
I'll post a tiny code example here, since it seems you're not really getting it.
#interface MyView : UIView
#property (retain) IBOutlet UIButton *button1;
#property (retain) IBOutlet UIButton *button2;
#property (assign) bool myData;
-(IBAction) doButton1:(id)sender;
-(IBAction) doButton2:(id)sender;
#end;
#implementation MyView
#synthesize button1 = _button1;
#synthesize button2 = _button2;
#synthesize myData = _myData;
// I'm leaving out the initWithNib, viewDidLoad, etc.
- (IBAction) doButton1:(id)sender
{
// do something as a result of clicking button1
_myData = YES;
}
- (IBAction) doButton2:(id)sender
{
// do something as a result of clicking button2
_myData = NO;
}
#end
Connect those up in InterfaceBuilder, and you've got a working "MVC." You don't need a completely new UIViewController for each button. The one for the View takes care of it.
UITableView and it's associated Views are more complex, and may require an additional UIViewController to help encapsulate. I really don't suggest starting out by using them, but this is a good tutorial here. It's got a lot of images which will show you how to connect things up in IB and the like. It's old, so your XCode may not look like the images, but it helps.
Thanks for the links, I'll look into them.
So far I've rewritten most of my application to using views instead of viewcontrollers (except the toplevel one) and it starts to match up with the API calls that are available like layoutSubviews. What I find disturbing that I need to do this now:
[tableDataSource loadData:date];
[tableView reloadData];
Where in my previous setup all I did was:
[tableViewController loadData:date];
But apparently that is the way to do it. One thing is unclear to me ATM. Since I construct and layout the view in loadView in my AppViewController, how do they get relayouted if the orientation changes. The VC does not have a layoutSubviews, so I should use the didRotateFromInterfaceOrientation and reposition the subviews from there?
BTW, I'm not mixing registering anonymous inner classes as listeners (observers). I'm very experienced with writing Swing components and JavaFX controls. And that probably is the culprit, in Java(FX) every component has a view and a controller (not always a model).

Resources