How do you transfer data between UIViewControllers? - iphone

Currently I am making a game for iPhone, and want each level to be on a different uiviewcontroller (I have tried putting them all on the same view controller, but this just makes it jumpy). However, I need a way to get a high score in the level's view controller and send it back to the menu view controller. I am using the code:
SecondLevelViewController *screen = [[SecondLevelViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
to get to the viewcontroller and
[self dismissModalViewControllerAnimated:YES];
to get back. I am familiar with NSUserDefaults, which are what I am using currently to get high scores. However, I know this code resets the level's view controller, that is still fine and even great because i want the level to reset-- but if just i could get data back that would be helpful.
Please put things in simple words, because I am very new to programming.
Thanks in advance!
Steve Becker
PS I am using xcode 4.0...
--------------------------------------------ALSO!!!-------------------------------------
The code I am using, I can only figure out how to get transitions "FlipHorizontal", "CoverVertical", and "CrossDissolve"...But I have seen many other cool transitions on the iPhone--like the page corner flipping like a page in a book. If you know how to do these other transitions, please tell me!!!! Greatly appreciated!!!!!

It's much more standard to have all of the levels on the same UIViewController unless the logic is so different that it's like playing different games. However, you could use a whole slew of different methods to achieve this. For example, you could use the NSNotificationCenter, NSUserDefaults, or a plist.

You will want to use delegation or NSNotifications for this.

Think about the different view controllers like the mafia: every view controller is operating on a need-to-know basis.
Look at this answer I gave before.

you could also use a singleton class to modify and access the sharable data in any class of your projects,
Here is the good SO post on singleton class in objective - C
Singleton shared data source in Objective-C
Edited:
What should my Objective-C singleton look like?
Edited: for Curl page animation : below is the link to blog tutorial and the source code.
http://blog.steventroughtonsmith.com/2010/02/apples-ibooks-dynamic-page-curl.html

Related

UINavigationController suggestion

I have read that UINavigationController are best option when you want to jump from n number of screen to first screen. It takes following code to do so:
NSMutableArray *array=[[NSMutableArray alloc]initWithArray:self.navigationController.viewController];
[array removeObjectAtIndex:1];
[array removeObjectAtIndex:1];
[array removeObjectAtIndex:1];
self.navigationController.ViewController=array;
[self.navigationController popViewController:YES];
by using this code I can go directly from fourth screen to first screen directly .
If I don't using navigation controller then also by making the object of firstSCreen in fourth screen I can achieve the same thing within couple of lines.Then why one should go for navigation controller? If answer is for memory optimization then we are autoreleasing the the object of firstViewController and now we are using auto referencing.
first thing this is wrong approach to pop. true one is
[self.navigationController popToRootViewController:YES];
and second thing is that if you are at 4 screen, then by poping to a specific viewcontroller will no pop all screens. here is memory issue.
another issue is that some times you don't want to lose parent screens data. if you use method of pop to specific viewcontroller, the data will be lost as the object having data is released. you made a new one.
point is that this depends on your conditions what is suit able in your scenario. but the normal and usual approach is that don't use specific popout techniqu as it may cause problems
You can use
[self.navigationController popToViewController:(UIViewController *) animated:(BOOL)];
You just need to pass ViewController's object on which you want to move directly... And between view controller's will pop-out automatically
Hope this helps you...
agreed with 'The Saad'
one more thing is that if you have various screens with the data coming from server.
it will be quite difficult to render(user interection) screen second while loding data on first.
same with all other sibling views.

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

Encapsulation of logic and interface on iPhone SDK

I assume this is going to be a very basic question, but maybe somebody can help me. I've come to iPhone SDK from a C# .NET background.
I would like to know if there is some mechanism similar to what is called in ASP.NET "UserControl" which encapsulates logic and interface. It would be desirable to launch "events" too.
I'm trying to design something like a common header for the entire application, which shows different types of buttons depending of the view where it is placed.
Normally Objective C will support only single Inheritance.We can achieve multiple inheritance
through Delegates called as protocols(user defined).i think it will help you when you google based on this(user defined protocols in iphone sdk).
First of all thanks for your contribution. I realized that I was looking for something different: The views (yeah, I know there were here since the beginning).
I've realized that when I allocate one of my controllers by default it tries to load the xib with the same name. Therefore, I can design the view on the interface builder and then load it on another view as a subview (that's similar to usercontrol concept).
How to make the view adjust to the desired size?:
Controller *controller = [[Controller alloc] initWithNibName:nil bundle:nil];
controller.view.frame = CGRectMake(100, 100, 200, 200);
[self.view addSubview:controller.view];
The only missing here is how to comunicate the view with the subview, but I supose that can be made with a reference to the parent.
Thanks a lot
(PS: I'm going to check the answer as correct just for close the thread. I don't know if this procedure is the best or has enough quality)

Loading new Views

Ok so I'm sure I'm missing this somewhere but I think I need someone else to bounce this off as I've been going around in circles on this for some time.
My situation is quite straight forward really.
I've got two files
TapView.m
TapView.h
which are loaded when the AppController.m calls the showTapView method.
-(void) showTapView {
[tapViewController resetAllStates:self];
[navigationController pushViewController animated:YES];
[tapViewController prepareTapView];
[[UIAccelerometer sharedAccelerometer] setDelegate:tapViewController];
}
Now this all works swell and loads TapView in happily enough. It's not really an issue, but I don't like building interfaces progmatically, what I'd love to be able to do is to change this method so that instead of loading a progmatically created view it calls on a XIB based view, let's call it LazyView for the sake of arguments.
Now if it's a big thing to do this I'm happy as is and can leave it progmatically inclined, it's just that having it working through Interface Builder will help me significantly in the future as I expand the application further :)
If anyone's wondering by the way, I've built the app using some samples from open source projects, the app doesn't bear much resemblance these days but it was from Remotepad - many thanks to the guys for the release of the source, it helped me get my head around Bonjour no end.
Sounds like you are looking for the initWithNibName method to load a view controller from a Nib.
UIViewController* controller = [[LazyViewController alloc] initWithNibName:#"LazyViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

How do I hide an infoButton in a utility app from the MainViewController when the infoButton is in the RootViewController?

How would I go about updating an object declared in the RootViewController from my MainViewController?
I'm attempting to hide my info button when my iAd is tapped, I have all the relevant pieces of code for the iAd in place, but can't figure out how to code the action. I saw an example of a similar situation online that was like this:
((MainViewController *)parentViewController).infoButton.hidden = #"";
I haven't been able to get that to work though, I just need this one value modifiable from the MVC, can anyone give me a simple suggestion?
P.S. I'm a total n00b and a snippet of code would help a great deal, I'm kind of learning as I go, thanks!
I figured this out through use of the NSNotificationCenter