Xcode moving from view to view - iphone

I'm coding in Xcode, and i have a problem moving from view to view (xib to xib) My xib-files all have a personal .h and .m file.
this is the code i have:
The code to go from the first view to the quiz view in this case:
-(IBAction)switchviewMainToQuiz:(id)sender {
ViewControllerQuiz *quizView = [[ViewControllerQuiz alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:quizView animated:YES];
}
The code to go back from a view to my first view:
-(IBAction)switchviewQuizToMain:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
the problem is that the second code just goes back and that gives sometimes an error or a fail in the app. So when i go to other views and then want to go back it often gets stuck. So does someone have an other code to switch the views, or maybe an other solution? Thank You!

I wouldn't create new viewController in IBAction. For me i would do this in this way:
1 Create property with viewController:
#property(nonatomic, strong) UIViewController *quizView ;
2 Synthesize it:
#synthesize quizView = _quizView;
3 Create method:
- (UIViewController *) quizView {
if (_quizView == nil) {
_quizView = [[ViewControllerQuiz alloc] initWithNibName:nil bundle:nil];
}
return _quizView;
}
4 In your IBAction:
-(IBAction)switchviewMainToQuiz:(id)sender {
[self presentModalViewController:self.quizView animated:YES];
}

Related

iphone - change view with button programmatically

I read this tutorial about storyboards on iphone dev. The guy changes views through a button. But the connection of the button is made through the interface builder. How can i do this programmatically, so i can make some checks (username/password for example) before the view is changed?
Thanks in advance.
Connect one view with other (not the button with the view).
Change the segue to custom and give it an identifier (in IB). Ex: Login
Then create an action and assign it to the button.
In the button action use:
[self performSegueWithIdentifier:#"Login" sender:self];
Create a Segue Class object and in the .m use something like:
UIViewController *dst = [self destinationViewController];
UIViewController *nav = [self sourceViewController];
//All your custom code
[nav presentModalViewController:dst animated:YES];
Edit: Forgot something important!
In IB, in the Segue configuratiĆ³n, put in Segue Class, the name of the Segue Class created. Ex: LoginSegue is the name of the segue created, in Segue Class yo has to write "LoginSegue"
Edit: To Create the Class:
1.- Create a New File extending UIStoryBoardSegue, the .h gonna be something like:
#import <Foundation/Foundation.h>
#interface LoginSegue : UIStoryboardSegue
#end
2.- In the implementation, use the code above inside an method called perform:
-(void)perform
{
UIViewController *dst = [self destinationViewController];
UIViewController *nav = [self sourceViewController];
//Custom Code
[nav presentModalViewController:dst animated:YES];
}
If you need to access the property of the source viewController, you need to change:
UIViewController *nav = [self sourceViewController];
to
eYourClass *nav = [self sourceViewController];
Hope this help!
You can either create the button and drag the touchUpInside action to an IBAction in interface builder
or in code somewhere in viewDidLoad
[self.login addTarget:self action:#selector(loginTapped) forControlEvents:UIControlEventTouchUpInside];
Then the loginTapped method could look something like
- (void)loginTapped;
{
if ([self authenticateWithUsername:self.username.text password:self.password.text]) {
[self performSegueWithIdentifier:#"loginSuccessful" sender:self];
} else {
// Warn user about invalid username/password
}
}
This relies on you having created the segue in the storyboard with a name that matches the identifier argument

Load View after applicationDidBecomeActive

I want to present a ViewController every time the app starts even from background, I have this code:
mainViewController *mainController = [[mainViewController alloc] init];
UIViewController *src = (UIViewController *) self.window.rootViewController;
UIViewController *dst = (UIViewController *) mainController;
[src presentModalViewController:dst animated:YES];
The ViewController loads becuase the keyboard pops up but the screen is black. Anyway thank you!
It's ok I figured it out, I took the View I wanted to load out of the mainstoryboard and put it in a nib and the just used initWithNib.

Cocoa-Touch: Check if UIViewController exists before init it

Is there a way to check if a UIViewController is loaded in memory/visible on screen?
Something like this:
if([ContentRvController exists]){
contentView *ContentRvController = [[contentView alloc]
initWithNibName:#"contentView" bundle:nil]; //ContentView is a custom UIViewController
....
//Code to set the UIViewController
....
}
else{
[ContentRvController release];
}
That should happen when a button (that right now initializes the ViewControllers) is tapped. Right now, when tapped it opens n ViewControllers, it is supposed to display only one at a time.
Thats pretty much it, greetings and hope you can help me out.
Is this based on existing code?
Classes should start upper case, and instances should be camel case, e.g.
if([contentRvController exists]){
ContentView *contentRvController = [[ContentView alloc]
initWithNibName:#"contentView" bundle:nil]; //ContentView is a custom UIViewController
....
//Code to set the UIViewController
....
}
else{
[contentRvController release];
}
it is probably worth declaring it in the header, i.e.
#interface SomeClass : NSObject {
}
#property(non-atomic, retain) ContentView *contentRvController;
#end
and then in code you could do
if(contentRvController!=nil){
ContentView *aView=[[[ContentView alloc] init] autorelease];
self.contentRvController=aView;
}
Also, don't do the else{[contentRv release];} bit, if you have autoreleased it anywhere, this will leak at some point.
If you are using UINavigationController check for the property topViewController.

self.view insertSubView .... not supposed to be self.view, but what?

I have a UIViewController which shows different UIViewController sub-classes. In the main UIViewController.m, I have a sub-class called 'Home' load on app start.
Now, which the Home view loaded, I have a button which I want to use to switch to another view called 'PreGameInfo'. I'm trying to use the code below:
- (IBAction)showPreGameInfo:(id)sender {
[self.view insertSubview:preGameInfo.view atIndex:0];
}
It doesn't work, and I know it's because the 'self.view' needs to refer to the main UIViewController rather than the self of the 'Home' view. Does anyone know how to insertSubView to the main UIViewController when using a UIButton whilst in a SubView???
Thank you!
You can use a delagate. It very easy
So implement this in your information view controller;
In the InformationViewController.h
#protocol InformationViewControllerDelegate;
#interface InformationViewController : UIViewController {
id <InformationViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <InformationViewControllerDelegate> delegate;
- (IBAction)returnBack:(id)sender;
#end
#protocol InformationViewControllerDelegate
- (void)InformationViewControllerDidFinish:(InformationViewController *)controller;
#end
in the InformationViewController.m
- (IBAction)returnBack:(id)sender {
[self.delegate InformationViewControllerDidFinish:self];
}
And use the delegate in any view controller you need it like this :
In the HomeViewController.h
#import "InformationViewController.h"
#interface HomeViewController : UIViewController <InformationViewControllerDelegate> {
}
Write the method to change the view from Home view to Information view
- (IBAction)goToInformationView:(id)sender;
In the HomeViewController.m
- (IBAction)goToInformationView:(id)sender {
InformationViewController *controller = [[InformationViewController alloc] initWithNibName:nil bundle:nil];
controller.delegate = self;
// You can chose the transition you want here (they are 4 see UIModalTransitionStyle)
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
And the last but not least the delegate method it inform the HomeViewController when the InformationViewController had finished
- (void)InformationViewControllerDidFinish:(InformationViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
I hope it helps
- (IBAction)showPreGameInfo:(id)sender {
[superview insertSubview:preGameInfo.view atIndex:0];
}
Does this code work?
[self.parentViewController.view addSubview:myView];
Just do it in the modalView:
[self presentModalViewController:preGameInfo animated:YES]
or you can do something like this...
This line will add your new view to windows rootViewControllers view
[self.view.window.rootViewController.view addSubview:preGameInfo.view];

Switching views - iPhone development

I need help figuring out how to change out the view in my application. I have a wonderfully working view that I have finished and now I'd like to be able to switch the view to a brand new, blank white screen to display.
I have these files:
HelloAppDelegate.h,
HelloAppDelegate.m,
HelloViewController.h, and
HelloViewController.m
Then, I added a new View Controller so now I have two more files:
SecondViewController.h and
SecondViewController.m
In my first view (HelloViewController), I have a button. When the user presses this button, I'd like SecondViewController to show up. So, in my HelloViewController.m, I have an action method
-(IBAction)switchToSecondView:(id)sender {
}
In this method, how can I go about initializing my second view and displaying it?
Thanks in advance!
If you want to do something like flipping view, making it modal and then returning back to the main view do following:
Define a delegate to indicate that secondary view finished its work
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish;
#end
In main view do following:
- (void)flipsideViewControllerDidFinish {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
In secondary view do following:
- (IBAction)done {
[self.delegate flipsideViewControllerDidFinish];
}