I'm confused something fierce over having multiple views. I simply want to have a button on my main view that activates a new view, which in turn would have an (x) button which goes back to main view. For the life of me, I can't figure out how to do this with two separate .xib files. How might this be done?
Thanks!
It might be easiest to just use the utility application as a template.
From there, you can see how you would load view controllers and nibs in order to bring up the new view followed by how you would exit it.
I actually solved it by doing it this way:
NewViewController *new = [[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil];
new.delegate = self;
new.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:new animated:YES];
[new release];
You are using a navigation controller, right?
On the IBAction associated with a button tap on the main view, put
NewViewController *newView = ... // [alloc - init your view here if you haven't already]
[self.navigationController pushViewController:newView animated:YES];
And on the button on newView,
[self.navigationController popViewControllerAnimated:YES]
Related
I have a UIViewController class and a second which I want to push modally preferably. However I can't seem to call [self pushModalViewController:...], how come?
What requirements do I need to meet to be able to do so?
I am doing this and getting a black view pushed:
vc = [[ViewController alloc] init];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
I have made my view controller in my storyboard and given it a custom class. I am trying to present this view modally via this class as seen in my code.
Any help much appreciated, thanks.
'push' and 'modal' don't belong together in the same thought. You can:
present a modal view controller, preferably using -presentViewController:animated:completion:, which is the modern replacement for -presentModalViewController:animated:
push a view controller onto the navigation stack, assuming that you're using a UINavigationController. To do that from a view controller, use:
[self.navigationController pushViewController:foo animated:YES];
You're actually looking for [self presentModalViewController:myViewController.view animated:YES]
How are you initing this viewcontroller. From the posted code, I assume it is being initialized to a blank view.
Maybe you can init it from a nibname or something?
vc = [[ViewController alloc] initWithNibName:#"NibName" bundle:nil];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
Also another thing to note, if this is a custom class and no nib file, is there any code in initWithCoder? How does the viewDidLoad looks like?
I'm having a little predicament switching between views, here.
Alright, so, I have this view controller class in my iPhone project called "BaseViewController," which is the default, which has a button called "GoToNextView." I added another ViewController to the storyboard called "NextViewController," and then I created another custom view controller class called "NextViewController." Under the inspector window for NextViewController on the storyboard I changed its custom class to "NextViewController;" I'm assuming everything should be hooked up, now. When I click on the "GoToNextView" button, though, the application stops with a SIGABRT message.
Here's the code for my button click action in the BaseViewController class.
- (IBAction)Transition_Next:(id)sender
{
nextViewController = [[NextViewController alloc]
initWithNibName:#"SecondView"
bundle:[NSBundle mainBundle]];
[self.view addSubview:nextViewController.view];
}
What might I be doing wrong, here?
Thanks in advance...
You can use:
- (IBAction)Transition_Next:(id)sender
nextViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nextViewController animated:YES];
}
Instead of your code and it should work! Hope that helps!
You can use [self.navigationController pushViewController:nextViewController animated:NO]; or if you don't have a navigation controller, you may use
[self presentModalViewController:nextViewController animated:NO]; instead of [self.view addSubview:nextViewController.view];
I have an iPad app that uses the storyboard board feature and then I have a separate .xib file for another view. I can switch to the separate view and its fine:
-(IBAction)SecondView:(id)sender{
SecondView *Second = [[SecondView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Second animated:NO];
}
But when I am in the Second View and try going back I do everything the same just with the first view controller, but It just goes to a black screen:
-(IBAction)FirstView:(id)sender{
FirstView *First = [[FirstView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:First animated:NO];
}
What do you guys think? Am I doing something wrong? What is the best way to switch views?
initWithNibName:bundle is for loading nib or xib files.
If you are loading from a storyboard, you need to use
FirstView *First= [self.storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
IDENTIFIER is defined in your storyboard, it's in the Utilities, Attributes Inspector on the right side.
HOWEVER your real problem is that you shouldn't be loading from the storyboard at all. you should just be calling
[self dismissModalViewControllerAnimated:YES];
That call will clean up the presentModalViewController:animated: that you used to put the modal view controller up in the first place.
You presented SecondView using presentModalViewController:animated:, so you need to dismiss it using dismissModalViewControllerAnimated:.
- (IBAction)FirstView:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
In my app there is requirement that..I have 6 buttons in a nib,
when I press any button a new nib will be loaded into the window according to the button pressed. problem is after loading the new nib If I want to come back to the previous nib (which is having all the buttons) how to add navigation controller?
what I am doing now is while loading the new nib when I pressed the button
objNewViewController = [[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil];
[self.navigationController pushViewController:objNewViewController animated:YES];
but by this way im not able to load the nib, it's not performing any operation?
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil]];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
And in NewViewController:
USe this to dismiss and get back to previous view.
[[self navigationController] dismissModalViewControllerAnimated:YES];
There is a template in Xcode for a navigation based app. It does everything you describe. Well, very close at least, only the AnotherViewController in -tableView:didSelectRowAtIndexPath: is commented out.
I am creating a Navigation based iPhone application.
In that I have called a UiViewController using presentModalViewController. After that, the ViewController becomes visible. From that ViewController I need to call another ViewController using the sample presentModalViewController. Is this possible or not?
What do you mean by "call another uiviewcontroller"? (It really helps if you can be more detailed in your question.) If you mean, "slide in another view controller", then:
MyNewViewController *myNewViewController = [[MyNewViewController alloc] initWithNibName:#"MyNewViewController" bundle:nil];
[navigationController pushViewController:myNewViewController animated:YES];
[myNewViewController release];
...where:
MyNewViewController is the new view controller class that you want to slide in (the above code assumes you have an XIB file for the view controller class).
navigationController points to the current navigation controller. You'll have to replace it with something like [self navigationController], depending where you are in the view hierarchy.
U might be using following line to present a view controller.
//assume name of viewController which u want to present is "myViewController"
[self.navigationController presentModalViewController:myViewController animated:YES]
If you want to push an other ViewController or present an other ViewController then u will need to replace above line with following lines.
//[self.navigationController presentModalViewController:myViewController animated:YES];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navigationController.navigationBarHidden = YES; //if u want to show navigation bar then remove this line
[self presentModalViewController:navigationController animated:YES];
After using above code you can present or push other view controllers within presented view controller.
Hope it will solve your problem :)