Loading new Views - iphone

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];

Related

objective-c: help me understand views and modal transition

I'm lacking a serious understanding of how modal transitions work, so please do not leave the simplest answer out of this.
I have two views with their own view controllers set up in storyboard. A button on the main-menu leads to other-view. I set up this transition purely via ctrl-click and selecting the modal transition. I also have a button leading from the other-view back to the main-menu, set up similarly.
To further my understanding of these transitions I decided that I want the main menu to play a sound when it loads up, but only the first time i hit run, and not again when i go hit the button the other-view to go back to menu-view.
in my menu-view I have a private property BOOL
#interface MainMenuViewController ()
#property BOOL menuSoundPlayed;
#end
and my viewDidLoad...
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.menuSoundPlayed){
//sound setup code omitted for clarity
AudioServicesPlaySystemSound(mySound);
self.menuSoundPlayed = YES;
}
}
can someone help me understand why the menu sound plays every time main-menu view loads? I do acknowledge that the menuSoundPlayed is never really initialized, but I dont know where I would even do that.
side-note: I love that apple gives us all these conveniences like story-board, but I almost wish it was easier to see all the code behind these things, so i could actually understand what was going on when i 'segue' between views.
Anyways, thank you!
I did a bit more researching and answered my own question.
In the situation I had before (described in my question) I had the code
[self performSegueWithIdentifier:#"mainMenuSegue" sender:sender];
being executed, which as commenters described, starts a new instance of my main-menu.
What i wanted was to return to the main-menu instance already created.
To do this the following code is necessary (with your own completion block obviously):
[self dismissViewControllerAnimated:YES
completion:^{
NSLog("view dismissed");
}];

how to use pushViewController in ReaderSample of ZBarReader project

I am working on the ReaderSample of ZBarReader project,instead of use
[self presentModalViewController: controller animated: YES];
I am trying to use
[self.navigationController pushViewController:controller animated:YES];
and when I run the application, there is nothing happens at all
I am wondering why I cant use pushViewController
Please advice me on this issue.
Thanks
EDIT:
The underlying reason is that apple does not allow stacking of navigation bars and UIImagePickerController has one of its own so than you will have two and will be confused..
The ZBarReader uses UIImagePickerController as its base class (not actually but internally somewhere ) and as you might be knowing you can't push a UIImagePickerController object on a navigation controller... that's why you have to use presentModalViewController
(Modal view controllers are used to tell the user that something important is happening apart from the normal app usage and clicking a picture is a part of that.. . its in the HIG)...
hoping this helps..

How do you transfer data between UIViewControllers?

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

I would like to flip my app's splash screen - how can I mimic the flipside controller's animation?

I've finally gotten a working "alpha" version of my first app installed and (mostly) working on my iPhone 3G. So excited I came into the house and danced a little jig while my wife rolled her eyes at me. Don't care - totally stoked that I figured it out on my own (with lots of help here - thanks again, guys).
I've never really dabbled with or cared about animation; I'm more into utility-type apps. However, I've decided that I'd like to animate my app's opening image / default.png / splash screen similar to the flipside view controller animation - where the image spins from a view on the front to a different view on the back. I've found code for animating between views using the flipside animation, but how would I go about animating from a static *.png image to my navigation-based table view? I'm just not even sure where to start with this one - literally the first time I've ever even searched for anything graphics-related in the documentation.
Any help will be appreciated. As usual, thanks in advance!
You can't do anything with your Default.png, and just for form I'll point out that that HIG guidelines say that you shouldn't use it as a splash screen :-).
I would suggest that you use your initial view controller to duplicate the Default.png, and copy the flip animation code from a basic Utility app template - you probably want to use [NSObject performSelector:#selector(...) afterDelay:0] to get it to flip, called from your initial viewDidLoad:.
You can just present a modal view controller when you first launch using the flip transition instead of the default slide. Have your initial view controller loaded from your xib just display the same image you are using for your Default.png. Once you get the -viewDidLoad call in your initial view controller, push the modal view specifying the transition you want. Something like this:
- (void)showMainView;
{
MainViewController *controller = [[MainViewController alloc] init];
[controller setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:controller animated:YES];
[controller release], controller = nil;
}
As Paul suggested, you should call this using performSelector:withObject:afterDelay:
[self performSelector:#selector(showMainView) withObject:nil afterDelay:0.15];
Hope that helps.

Iphone changing to another view [BEGINNER]

Hi I have a small doubt, I have 3 Nib Files:
ConfigureContacts.xib
CallContactsViewController.xib
MainWindow.xib
When Application starts I do:
[window addSubview:callContactsViewController.view];
[window makeKeyAndVisible];
So that the CallContactsViewController.xib is loaded.
Inside CallContactsViewController.xib there is a button, that when presses jumps to:
-(IBAction)configureContacts:(id)sender
{
configureContacts = [[ConfigureContacts alloc] initWithNibName:#"ConfigureContacts" bundle:nil];
[self.view addSubview:configureContacts.view];
}
The idea is that when the button is pressed it goes to the "next window" which is the other .xib file. Is this the correct way of changing through xibs ? Am I wasting memory here ?
Thanks in advance
is ConfigureContacts a ViewController subclass? (It looks like it is)
if so you would actually do something like this
-(IBAction)configureContacts:(id)sender
{
configureContacts = [[ConfigureContacts alloc] initWithNibName:#"ConfigureContacts" bundle:nil];
[self pushviewController:configureContacts animated:YES];
}
My Guess this is what you want to do.
[self.view addSubview:configureContacts.view] should work as well however it will change only part of the view and keep you on the same 'Window' so to speak.
pushViewController is a method from UINavigationController, which is used for managing hierarchical viewControllers. It sounds like that might be what you're trying to do here, in which case adding a UINavigationController to your code might be the way to go.
Alternatively, you could implement a third UIViewController that owns both CallContrats and ConfigureContacts and is responsible for switching between them.
Check out the sample code from Beginning iPhone Development 3. Specifically, check out the programs "06 View Switcher" and "09 Nav". I think they'll have the code that you're looking for.