Problem with info view on iPhone app - iphone

I am currently using this code to bring up an info view for an iPhone app.
-(IBAction)showInfo:(id)sender {
InfoView *info = [[InfoView alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
info.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:info animated:YES];
[info release];
}
And this code go back to my main view
-(IBAction) exitInfo:(id)sender {
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
When I run it on the simulator, it goes fine, however, on the actual iPhone, when I press the button that triggers the exitInfo method, it carries out its animation, but once it is completely off the screen and only my main view is visible, the screen will flicker, quickly showing the info view again. How can I fix this?

In my opinion, your viewController shouldn't even know it is modally viewed.
Try putting the code that send the dismissModalViewController message in your main controller and alert your main controller that the user clicked the dismiss button using a delegate.
something like this in your "parent" viewController
-(IBAction)showInfo:(id)sender {
InfoView *info = [[InfoView alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
info.modalTransitionStyle = UIModalTransitionStylePartialCurl;
info.delegate = self;
[self presentModalViewController:info animated:YES];
[info release];
}
-(IBAction)closedButtonClicked {
[self dismissModalViewControllerAnimated: YES];
}
and in your "Modal" viewController
-(IBAction) exitInfo:(id)sender {
[delegate closedButtonClicked];
}
Of course you will need a protocol like
#protocol MyCustomDelegate
-(IBAction)closedButtonClicked;
#end
and something like this in your "modal" interface
-id<MyCustomDelegate> delegate;

Related

error when i am calling next view controller

i am getting an error when i am calling next view controller, i want to call a view controller that a action continue .this screen will come appear first time only when we run first time.
controller is not going next view.
-(void)viewDidLoad
{
welcomePage *temp = [[welcomePage alloc]initWithNibName:#"welcomePage" bundle:nil];
[self presentViewController:temp animated:YES completion:^(void){}];
}
Warning: Attempt to present on whose view is not in the window hierarchy!
Try this one..
You change the code in Appdelegate.m didFinishLaunchingWithOptions method like this
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
And in your ViewDidLoad method like this..
welcomePage *temp = [[welcomePage alloc]initWithNibName:#"welcomePage" bundle:nil];
[self.navigationController presentViewController:temp animated:YES completion:nil];
For dismissing viewcontroller you can write following code in welcomepage button action
-(IBAction)dismiss:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}
use this code
-(void)viewDidLoad
{
welcomePage *temp = [[welcomePage alloc]initWithNibName:#"welcomePage" bundle:nil];
[self presentViewController:temp animated:YES completion:nil];
}
That error means you are trying to present a modal view controller from a view controller that is not currently in the screen.
If You are using Storyboard : Use this
instantiateViewControllerWithIdentifier
UIViewController objController = [self.storyboard instantiateViewControllerWithIdentifier:#"StoryBoard Id"];
[self presentViewController:objController animated:YES completion:nil];
To Set Storybaord ID , Check Here .
This error means that you are trying to present 'welcome page' from a view that is not on the screen yet.
Knowing that simply moving the code to the viewDidAppear method doesn't work for you, what you can try to do is this:
-(void)viewDidAppear
{
[self presentWelcome];
}
- presentWelcome {
welcomePage *temp = [[welcomePage alloc]initWithNibName:#"welcomePage" bundle:nil];
[self presentViewController:temp animated:YES completion:^(void){}];
}
What happens here is that when the view appears (so when it is in the view hierarchy), the controller starts a function that calls the other viewController. hope this solves your problem.
Hope this helps you!

can't dismiss presentModalViewController

i have two view controllers in iphone application.
1.) FirstVC
2.) SecondVC
In my FirstVC i have one button. By tapping on that button i opened SecondVC in presentModalViewController. look bellow code.
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil];
[self.navigationController presentModalViewController:secondVC animated:YES];
}
now moved to SecondVC. On SecondVC i have create navigation bar as well as "cancel" button as a leftBarButtonItem. i set a button clicked event on cancel button. in that method i want to dismiss SecondVC. bellow method is in SecondVC. look bellow code.
- (void)cancelButtonClicked
{
[self dismissModalViewControllerAnimated:YES];
}
This code dosen't work. i can't dismiss SecondVC by this code. Please suggest another tricks.
Change the button code to this..
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil];
[self presentViewController:secondVC animated:YES completion:NULL];
}
on cancelButtonClick
-(void)cancelButtonClicked {
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}
You are sending the dismissModalViewControllerAnimated: message to the wrong object.
Since you presented the modal view controller through the navigation controller, then you should call:
[self.presentingViewController dismissModalViewControllerAnimated:YES];
This will work on iOS 5 and newer. In case you are targeting only iOS 5 and newer, you could also think of using the newer methods that are available on it to manage modal view controllers:
– presentViewController:animated:completion:
– dismissViewControllerAnimated:completion:
but I don't think this is mandatory.
If you want to support iOS 4 and older, you should add a property to your modal view controller:
#interface SecondVC : UIViewController
#property (nonatomic, weak/assign) UIViewController* presentingController;
...
#end
and set it before displaying the controller modally:
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil];
secondVC.presentingController = self.navigationController;
[self.navigationController presentModalViewController:secondVC animated:YES];
}
then you would use:
[self.presentingController dismissModalViewControllerAnimated:YES];
try this for present secondVC...
[self presentModalViewController:secondVC animated:YES];
Please Replace your code with the following... code
- (IBAction)buttonClicked:(id)sender
{
SecondVC *secondVC = [SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil];
[self presentModalViewController:secondVC animated:YES];
}
- (void)cancelButtonClicked
{
[self dismissModalViewControllerAnimated:YES];
}

How can open viewcontroller immediately on click on button

I am developing an e-com app for iPhone in which i need to open a view immediately when the user clicks on a button for the upcoming view, the data loads large images from server , to load images I am using a background thread .
Thank You
Simple, here's how to create a UIViewController and present it from within an IBAction.
- (IBAction)goToNextView:(id)sender
{
//if you are using xibs use this line
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"myXib" bundle:[NSBundle mainBundle]];
//if you are using storyboards use this line
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"myViewControllersID"];
//to present the controller modally use this
[self presentViewController:controller animated:NO completion:nil];
//or if you are pushing to this controller using a navigation controller use this
[self.navigationController pushViewController:controller animated:NO];
}
Be sure to pass animated NO so that the view is displayed immediately.
You need to make a button and then add its action like
-(IBAction)ButtonAction:(id)sender{
CalController *calculateView=[[CalController alloc] initWithNibName:#"CalController" bundle:nil];
[self.navigationController presentModalViewController:calculateView animated:NO];
}
You can use modal view controller for this
- (IBAction)showController:(id)sender {
SampleViewController *sampleView = [[SampleViewController alloc] init];
[self presentModalViewController:sampleView animated:YES];
}
Here it is the tutorial http://timneill.net/2010/09/modal-view-controller-example-part-1/

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

presentModalViewController not working

here's my code:
ViewController *vc = [[ViewController alloc] initWithNibName:#"TableView" bundle:nil];
[self.navigationController presentModalViewController:vc animated:YES];
//[self setView:[vc view]];
If I call it, nothing happens. However, if I change it to:
ViewController *vc = [[ViewController alloc] initWithNibName:#"TableView" bundle:nil];
//[self.navigationController presentModalViewController:vc animated:YES];
[self setView:[vc view]];
The view appears just fine (without the transition, of course).
What am I doing wrong? Is there anything special you have to take care of when initializing the view controller? I tried to copy as much as possible from Apple's examples, but I can't get this to work...
Thanks for any input!
-- Ry
You can only present modal view controllers from controllers that have already been shown onscreen (usually through a UINavigationController or UITabBarController). Try creating a UINavigationController, pushing a viewController to it, and then presenting your modal controller. There's a starter project in Xcode that shows how to create a UINavigationController-based flow if you're unfamiliar with it.
One other thing to note: if you haven't pushed the view controller onto a UINavigationController, the .navigationController property will be nil, and messaging it will have no effect.
I encountered the same problem while attempting to show a modal view over another modal view. Ben's answer is correct, and can be implemented like so:
#interface FirstView: UIViewController {
UIViewController *firstView;
}
- (IBAction)showOptionsView:(id)sender;
#end
In the main view class:
- (void)viewDidLoad {
[super viewDidLoad];
firstView = [[UIViewController alloc]init];
[firstView setView:self.view];
[firstView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
}
- (IBAction)showOptionsView:(id)sender {
OptionsView *optView = [[OptionsView alloc]initWithNibName:#"OptionsView" bundle:nil];
if(firstView != nil) {
[firstView presentModalViewController:optView animated:YES];
[optView release];
}