I'm doing a testing stuff. I'm making a pop up table (PopUpViewController, UIViewController) that trigger by UI button through below code. It work fine.
ThirdViewController.m
-(IBAction)onClickPopUpView:(id)sender{
popUpViewController=[[PopUpViewController alloc]initWithNibName:#"PopUpViewController" bundle:nil];
[self.view addSubview:controller.view];
[self addChildViewController:controller];
[controller didMoveToParentViewController:self];
}
PopUpViewController.m
- (IBAction)close:(id)sender
{
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
}
That code work for ios5 or later. I wonder how can do some effect that is compatible with ios4. Does anyone give me hint?
Related
I have used below code for IO6 and below to force rotate:
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIApplication* application = [UIApplication sharedApplication];
if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
{
UIViewController *c = [[UIViewController alloc]init];
[c.view setBackgroundColor:[UIColor redColor]];
[self.navigationController presentViewController:c animated:NO completion:^{
[self.navigationController dismissViewControllerAnimated:NO completion:^{
}];
}];
}
}
But it is not working correctly on IOS7, it rotate view controller but again set blank view on screen...
Can you help me to solve this issue in IOS 7...
Change this line:
[self.navigationController dismissViewControllerAnimated:NO completion:^{
}];
To this:
[self.navigationController dismissViewControllerAnimated:YES completion:^{
}];
I have the following problem. I want to go from a view to another view, not with a button, but writing code. I have a view in which there are, among other things, some buttons. When I press one of them runs an if - else. From else I want to go to another view.
I have tried this
GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:#"GameOverViewController" bundle:nil];
[self.view removeFromSuperview];
gOver.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:gOver animated:YES];
this
GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:#"GameOverViewController" bundle:nil];
[self.view removeFromSuperview];
[self.view insertSubview:gOver.view atIndex:0];
and this
GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:#"GameOverViewController" bundle:nil];
[self.view removeFromSuperview];
[self.view addSubview:gOver.view];
but nothing worked. Any ideas ?
i also tried this
GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:#"GameOverViewController" bundle:nil];
[self.view setHidden:YES];
[self.navigationController pushViewController:gOver animated:NO];
throws exeption on
[self presentModalViewController:gOver animated:YES];
[self.view insertSubview:gOver.view atIndex:0];
[self.view addSubview:gOver.view];
Thanks in advance
DonĀ“t call [self.view removeFromSuperview]; which may cause your viewController to be deallocated, when using presentModalViewController it covers your view:
GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:#"GameOverViewController" bundle:nil];
// gOver.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:gOver animated:YES];
If you need to hide your current view then use: [self.view setHidden: YES];
"I want to go from a view to another view, not with a button, but writing code." - did you refer this to push in storyboard or did you mean that you din't want it in a button action?
The above code works fine :
-(IBAction)buttonAction:(id)sender
{
GameViewController *game = [[GameViewController alloc] initWithNibName:#"GameViewController" bundle:nil];
game.modalTransitionStyle = UIModalTransitionStylePartialCurl ;
[self presentModalViewController:game animated:YES];
}
And hope that you doing n iPhone app and not an iPad one, in which case you need to use popOver instead of modalView
How to load multiple views one after the another once this view is displayed
- (void)displayviewsAction:(id)sender
{
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
}
Now on top of this viewcontroller how i can load multiple viewcontrollers one after the other.
Please advice.
Thanks for help.
It depends on how you want to display it. If you're using a UINavigationController, you can do something like:
[self.navigationController pushViewController:viewController animated:YES];
or, if you'd like to present it as a modal view, you can do this to support older versions of iOS:
[self presentModalViewController:viewController animated:YES];
or, if you're only targeting iOS 5, do this:
[self presentViewController:viewController animated:YES completion:NULL];
Then to dismiss the view controller, on older versions of iOS:
[self dismissModalViewControllerAnimated:YES];
or if you're only targeting iOS 5:
[self dismissViewControllerAnimated:YES completion:NULL];
I think what you are looking for is a UINavigationController. That is a kind of super-controller that can load a stack of other view controllers one after another.
I'm working on an iPhone/iPad application and trying to switch views through a function call, not by a button. I've seen lots of sources switching views triggered by a button, but there's nothing that explains how to switch views triggered by a function. I tried to do this on my own, but it failed. Here's the code that I tried:
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.webview == nil) {
self.webview = [[MainViewController alloc] init];
/* webview initialized with storyboard */
if (self.view.superview == nil) {
[self.view insertSubview:self.webview.view atIndex:0];
}
[storyboard release];
}
}
This is viewDidLoad of the viewController.m. First, it shows WebView.
- (void)changeView {
self.normview = [[AlternateViewController alloc] init];
/* normview initialized with storyboard */
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.webview viewWillDisappear:YES];
[self.webview viewDidDisappear:YES];
[self.webview.view removeFromSuperview];
[self.view insertSubview:self.normview.view atIndex:0];
[self.normview viewWillAppear:YES];
[self.normview viewDidAppear:YES];
[UIView commitAnimations];
}
And when this changeView function (in a viewController.m) is called, It should change the view from webview to normview (actually, it works fine when the same code is triggered by a button). But when I call this function in other file (not viewController.m), such as
ViewController *viewcontroller = [[ViewController alloc] init];
[viewcontroller changeView];
It doesn't work. Anyone can give a clue to solve this or an alternative way? (ps. I'm testing on iPad.)
You can switch views with this function:
YourView *screen = [[YourView alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
You can set the modaltransitionstyle to your willings.
Don't forget to import your headerfile at the top of your class.
#import "YourView.h"
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[[self model] setTransitioning:NO];
[[[[self view]subviews] objectAtIndex:0] removeFromSuperview];
}
How can I tell what kind of viewController controls the subview at index 0?
If it's a QuizViewController I need to call a function on it.
Thanks in advance.
Good answers. I don't believe I explained enough. This is a all in a view controller that is a view stack. The view controller adds and deletes views manually with an animated transition. I am not using a navigationController and cannot in this particular instance for other reasons.
Sometimes the views I add are simple UIImageViews. Sometimes they are QuizViews. The QuizViews have a QuizViewController because they need internal functionality. Here are the two functions I use to add the views.
- (void)loadQuiz:(NSInteger )quizNum
{
if([self quizViewController] != nil)
{
[self setQuizViewController:nil];
}
QuizViewController *quiz = [[QuizViewController alloc] initWithNibName:#"QuizViewController" bundle:nil];
[quiz setUp:quizNum];
[self setQuizViewController:quiz];
[quiz release];
[[self view] addSubview:[[self quizViewController]view]];
[self setSlide1:[[[self view] subviews] objectAtIndex:0]];
[self setSlide2:[[[self view] subviews] objectAtIndex:1]];
[[self slide1] setHidden:NO];
[[self slide2] setHidden:YES];
[self performTransition];
}
- (void)loadImage:(NSString *)slideImage
{
UIImage *tempImg = [UIImage imageWithContentsOfFile:[Utilities localPathForFileName:slideImage]];
UIImageView *temp = [[UIImageView alloc] initWithImage:tempImg];
[[self view] addSubview:temp];
[temp release];
//[topView release];
if ([[[self view]subviews] count] > 2) {
//add the 2nd subview
//[[[[self view]subviews] objectAtIndex:0] removeFromSuperview];
}
[self setSlide1:[[[self view] subviews] objectAtIndex:0]];
[self setSlide2:[[[self view] subviews] objectAtIndex:1]];
[[self slide1] setHidden:NO];
[[self slide2] setHidden:YES];
NSLog(#"%s %d",__FUNCTION__,[[[self view]subviews] count]);
[self performTransition];
}
So my question is still, in the animationDidStop function how do I detect if it's a quiz?
if ([[self view]subviews] objectAtIndex:0] isKindOfClass:CLASS(class))
...
or isMemberofClass
From memory so you'll have to test it out...
You can also set a tag on the view when you create it, then look for the view when you retrieve it.
someUIView.tag=99;
then
if ( [[self view]subviews] objectAtIndex:0].tag == 99 )
...
Cheers
You maintain one UIViewController (for e.g. UIViewController *currentViewController) object for keeping the track of current view controller.
Now before pushing to the viewcontroller which according to my understanding gets pushed by more than one view controller,you set the currentViewController with the viewController from which you are pushing.
So if you are in QuizViewController and pushing to viewController which controlls subviews,you first set currentController of it and then push it.
subViewController.currentController = self;
[self.navigationController pushViewController:subViewController animated:YES];
Views don't inherently have view controllers attached to them, so the way your question is worded doesn't quite make sense. If what you're doing is having a UIView subclass, say QuizView, as a subview and need to know when that is being removed and act on it, then the code would look like this;
-(void) animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)flag
{
[[self model] setTransitioning:NO];
UIView *subview = [self.view.subviews objectAtIndex:0];
if([subview isKindOfClass:[QuizView class]])
{
[(QuizView*)subview yourFunction];
}
[subview removeFromSuperview];
}
If you mean something different and you can provide some code I might be able to help more, but like I said your original question isn't quite clear to me if this isn't what you mean ;)