What goes in the View and what goes in the View Controller? - iphone

I am new to programming for the iPhone and this will be my first question here. I have experience with different languages like php/java/c++.
My question is about ViewControllers and views in iOS.
I have started a project which will contain several different things like a login screen, a main screen and several other screens. The goal of this project is to learn how to create everything programmatically instead of using interface builder to get more accustomed to the system. I am using the book: "Advanced iOS 4 Programming" to help me.
I have been able to create all the screens ( and stuff like logging-in is working ), but I am not sure if I did it correctly.
All of my code for creating the textfields/labels/buttons is now located in the ViewController while the main view where everything is put on is almost empty, with nothing being done in it. Shouldn't the code to create the textfields and other components be located in the view itself, or is this the correct approach?
I have looked at several different examples but most use interface builder. The book itself is also not very clear in this matter.
Thanks in advance.
Kind regards,
Jasper

In the view you have the view - on other words, literally what the human user sees with their eyeballs.
So, for example, if you are doing complicated drawing you will have your own custom drawRect: method, and that for example is in the view.
On the other hands ......
In the view controller you have things that control the view.
Generally speaking "everything" goes in the view controller.
When you first start programming for iPhone (or Mac), simply put everything in the view controller and don't worry too much. There's plenty to learn. OK?
Eventually, separate out the "actual drawing" separately in to the view.
Hope this simple explanation for beginners helps!

In simple controller code should contain methods like...
class myLoginController : NSObject
{
UIView *myView;
}
-(void) initLoginController
-(void) loadLoginViewInView :(UIView*)inView;
-(void) removeLoginView;
-(void) isViewLoaded;
-(void) submitButtonClicked : (id) button;
-(BOOL) isLoginSuccess;
and initLoginController you can create your view,
-(void) loadLoginViewInView :(UIView*)inView
{
[inView addSubview:myView];
}
and in removeLoginView you can remove "myView" from its superView .

Related

Connect action of a XIB loaded from a custom class to a storyboard

I'm trying to create a reusable component to display some photo collection.
The basic flow is the following :
First view : View. It contains my so called library, designed programmatically and loaded from storyboard by assigning a custom class
I take a photo in a modal view, openend from the 'take picture' button
Once the photo is saved on disk, I ask PhotoLib to create a new PhotoCell from the photo path
I would like my PhotoCell to be touch enabled so when I tap it, it opens the second view in a modal way, but from what I read I cannot do this from my PhotoCell or the UIImageView inside (not a controller).
So how can I do ? View is embedded in a NavigationController, even if not shown in the screenshots below.
Thank you !
If you create Photocell in photolib, then photolib should implementing delegate methods from photocell. But photolib itself is not rootviewcontroller, so it should declare delegate methods itself, and the containing view should implement it.
Basically you pass Photocell from itself to Photolib (which implements delegate method
-(void) openPhotoCell:(Photocell*)cell
{
[self.delegate openPhotocell:(Photocell*)cell];
}
, then it passes it to View, which in its turn opens it.
It may seem like pulling a tooth from an ear, but actually it's quite working and if you write good self-explanatory code, it's not a problem. I'm currently working on some big project with tens views and controllers and it works pretty good and nobody has problem with that.
If you have more layers, then maybe you should look into NSNotification.
Hope it helped, I'd be glad to explain more.
UPD:
Links:
about delegates in cocoa fundamentals guide
delegation pattern in wikipedia

Xcode 4.2 & Storyboard, how to pass data between views? Existing code error

I'm trying to learn how to pass data between views. Say set a label in the second view from text entered into a text field on the first view. I basically have tried making a string in the second view and then when switching from the first view to the second I set a string in the second view. Then when the second view loads its sets the text of a label to the same string. I NSLog right before and after the transition, before its fine, but when the second view loads it string gets erased. I'm not sure why this isn't working. Here is my project: http://www.mediafire.com/?83s88z5d06hhqb5
Thanks!
-Shredder2794
From my book (http://www.apeth.com/iOSBook/ch19.html#_storyboards):
Before a segue is performed, the source view controller is sent prepareForSegue:sender:. The view controller can work out what segue is being triggered by examining the segue’s identifier and destinationViewController properties, and the sender is the interface object that was tapped to trigger to the segue (or, if performSegueWithIdentifier:sender: was called in code, whatever object was supplied as the sender: argument). This is the moment when the source view controller and the destination view controller meet; the source view controller can thus perform configurations on the destination view controller, hand it data, and so forth.
(Of course another solution is "don't use a storyboard". Then the first view controller creates the second and can hand it data then and there.)
The reverse problem is much trickier; look at the Utility Application template for an example of how to use the delegate pattern.
StoryBoards are ready made things where you can reduce a lot of code you write.So consider controller A & B on storyboard.
Now for passing data From A to B you can connect them with a segue name its identifier and then you can use delegate methods in A as:
//This method gets called before transition from A to B.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"THE IDENTIFIER YOU NAMED"])
{
id *objectOfController_B = [segue destinationViewController];.
objectOfController_B.lblTextDisplayOfA = //Something...
}
}
Now You can Explicitly transition it by using button in controller A.
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"THE IDENTIFIER YOU NAMED" sender:sender];
}
So I guess you can try experimenting on this and you will get it how and when transition occurs with segue.
Hope this helps.
There appear to be more than a few things to explain. I think working through a few tutorials will give you the answers you need. See http://www.raywenderlich.com/tutorials
i've asked more or less the same question a few weeks/month ago. there were some very good answers, especially the one from zoul, who built a demo project that will show you how to create a factory pattern application that will provide the views with the needed objects.
my question can be found here: iOS: Initialise object at start of application for all controllers to use and have a look at the answer from 'zoul'. it got me through this problem =)
good luck trying it out =)
sebastian
I spent "countless hours" my self trying to find a way to pass data and understand delegates with no comprehension and very little success. This video did something that all the other references I checked didn't do : keep it as simple as possible while clearly showing what was needed. Thank you so very much Mr Rob Smythe. http://www.youtube.com/watch?v=XZWT0IV8FrI
Have a look at this: http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html
Delegate pattern is a common way to achieve what you are trying to do.

How to get/set the rootViewController?

Now,I gonna Develop an App ,which wants to switch from many different Views irregularly,also the views need to load large resources,AKA,it's hard to manage memory.Are there any good solustion?
PS:I created a ViewController as RootViewController,and When a button was Touch,run the code as
"ViewController=newController"
.The problem came,The new View loaded wrong way,it rotate so that couldn't show in a correct way.
I google for the solution,some one said ,I should replace the rootViewController,just like that,
[UIApplication sharedApplication].delegate.window.rootViewController=newController;
But I can't get/set the rootViewController in other class though it's a singleton.
Why not having a class that handles all the view switches ?
This article describes an architecture that might be helpfull: http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/comment-page-1/#comment-607

xCode - Changing views between 2 existing xib-files

I'm very new to xCode and objective-C so I wanted to make a simple textRPG.
I'm currently making a character creation process consisting of 4 xib-files. The only way I got switching views to work was to look at the utility-template. Problem is, now I have the first screen being the delegate for the second screen, being the delegate for the third screen etc. So by the end of the character creation process I can't dismiss the views because that just "steps back" through the views.
When I've searched around for a solution I've found a addSubview-method but it seems like that makes a new view, like, empty to arrange programmatically.
All I need is a simple way to switch from one loaded xib to another xib. Have I misunderstood addSubview or do I need something completely different?
(If it helps: I've worked with VB for several years, in case you notice that I missed some kind of concept concerning views and such)
Thanks in advance! :)
Use this code. It is really simple and works well.
View *view = [[View alloc] initWithNibName:#"xibNameGoesHere" bundle:nil];
view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:view animated:YES completion:nil];
This will switch to another xib file and the two views won't own one another. I am using it in my own game right now.
#Joakim Ok, this is the way I do it. I have a class called RootViewController : UIViewContoller, whose view I add directly to the window. Then I make a subclass of UIView i.e. MyView. All of my views then subclass MyView. I also make an enum for all my views. In RootViewController you should have a switchView:(int)view method that looks something like this:
-(void) switchView:(myView) view
{
[_currentView removeFromSuperview];
switch(view)
{
case titleView:
_currentView = [[TitleView alloc] initWithRoot:self];
break;
case homeView:
_currentView = [[HomeView alloc] initWithRoot:self];
break;
default: break;
}
[self.view addSubview:_currentView];
[_currentView release];
}
in #interface RootViewContoller define MyView *_currentView;
TitleView and HomeView both subclass MyView and have a common method -(id)initWithRoot:(RootViewController*) rvc.
To switch between views use [_rvc switchView:homeView];
Hope this helps :)
It is called UINavigationController. Idea is
1) You push corresponding 'next' controller into navigation controller each time user submits current screen. Bonus - you'll get 'back' button for free on each step of character creation.
2) After character is created you pop all character creation controllers from stack.
Please read View Controller Programming Guide for iOS before trying to 'switch views' and such. You'll save tons of time and nerves.
another idea is not to use interface builder at all. i have been working with iphone apps for two years now and found that interface builder really prolongs the time to actually make something. make your own root controller and think about the logic you need to navigate through the views.

How to actually implement the paging UIScrollView from Apple's example?

I found an example of a paging UIScrollView in Apple's developer docs and it's just what I want for my application. I have a main view with a "Help" button that I want to present a view that users can page through to see all the help topics. The sample is at:
https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollViewPagingMode/ScrollViewPagingMode.html%23//apple_ref/doc/uid/TP40008179-CH3-SW1
And it works as advertised, but, as with so many of Apple's examples, they do all kinds of code right in the AppDelegate. I have my own AppDelegate, but then I have a NavigationController with a RootView that has this "Help" button on it. I can't seem to get the example code integrated into my own and have it work. I'm pretty sure I can't put the code they have in their AppDelegate in my own, but how to set it up eludes me.
Can someone point me to an example where they do what I'm talking about?
EDIT: I was able to create a new project and get it to work like Apple's by moving all the AppDelegate methods into the UIViewController that the template supplied and creating a new ContentPage UIViewController to hold the content. It can scroll from page to page, so I think I can insert this code into my other project ok.
I replaced the applicationDidFinishLaunching with an equivalent viewDidLoad and got rid of the AppDelegate stuff dealing with window and such. Then I changed Apple's initWithPageNumber: method to refer to my help pages rather than just creating instances of their generic views.
- (id)initWithPageNumber:(int)page {
NSLog(#"--initWithPageNumber:ContentPage");
if (self = [super initWithNibName:[NSString stringWithFormat:#"HelpPage%d", page] bundle:nil]) {
pageNumber = page;
}
return self;
}
Thanks for the help - sometimes it's good just to have someone tell you it can be done to keep going!
In the sample code, the applicationDidFinishLaunching: method sets up the UIScrollView (and a UIPageControl, but let's ignore that for now) and then "loads" the first two pages. "Loading" a page consists of loading that page's view controller into memory and caching it. It then adds that controller's view as a subview to the UIScrollView offset with an appropriate x based on what "page number" it is. You should be able to do all that in your viewDidLoad.
If you have a blank view when your view controller's view is shown, then you haven't added the subview correctly to your UIScrollView. Depending on what exactly you changed, that could be some combination of (a) not getting the x offset correct, (b) not setting the frame correctly, or (c) not adding it as a subview correctly.
If you post some of your own code we might be able to shed some light on the problem.