I'm trying to figure out how to preform transitions between different views just with code, without storyboard. For example, on a button press.
- (IBAction)nextClass
{
// insert transition between views
}
I know this is possible, I was just wondering what the actual code would be to make this happen.
Not 100% sure if I am understanding your question correctly, but I think you can just say:
ViewController *vc = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
The name "ViewController" after initWithNibName should be the name of the .xib file that contains the UI for ViewController. Let me know if this is what you are looking for!
EDIT:
Mungbeans brings up a good point. If you are using a navigation controller, you should say:
ViewController *vc = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
Related
My iOS application using Objective C. It received a remote notification, when a user click it, it will open a particular view.
This view has a back button in the navigation tab. In normal scenarios can back to root view. However, when I open this view from a notification, cannot back to any view nor leaving the application.
Here is my spruce code:
1. from AppDelegate.m to open a particular view:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewAnnouncement =[storyboard instantiateViewControllerWithIdentifier:#"Announcement"];
self.window.rootViewController = viewAnnouncement;
[self.window makeKeyAndVisible];
Action function to back to previous view (in viewClass.m):
- (IBAction)onBtnBackClicked:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
I believe the problem is, when I open this view from a notification, no parent view to let it back to. Can anyone help me, when user open this view from a notification, whether it goes to a root view or back to the previous opening app
EDIT : The previous answer didn't take in count the fact that you were in AppDelegate. Maybe this answer is better.
Turn your [self dismissViewControllerAnimated:YES completion:nil]
into :
UINavigationController *navigationController = [[UINavigationController alloc] init];
UIViewController *yourPreviousVC = [[UIViewController alloc] init];
[navigationController pushViewController:yourPreviousVC animated:YES];
If I'm wrong or if I missunderstood the question, do not hesitate to correct my answer.
Try this
Announcement *annouce = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"Announcement"];
HomeVC *home = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"HomeVC"];
[((UINavigationController *)self.window.rootViewController) setViewControllers:#[home,annouce] animated:YES];
And If app already open and you want to go previous page
Announcement *annouce = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"Announcement"];
UIViewController *visibleView = ((UINavigationController *)window.rootViewController).visibleViewController;
[((UINavigationController *)self.window.rootViewController) setViewControllers:#[visibleView,annouce] animated:YES];
I found the solution. My app has multiple views which I can go from a navigation list. When I want to back to main view, I just dismiss the current view.
Cuurently, a view is opened when I click a notification and I can't simply dismiss this view, Coz it considered as root view.
What I did to solve it, I connect my view (Announcement) by segue to main view, and set an identifier (for example: "leaveAnnouncementPage". Then, in your class just call this function in back button's fnction to back to the main view:
[self performSegueWithIdentifier:#"leaveAnnouncementPage" sender:self];
Here is also some useful links:
First
Second
Thank you guys for your help, and all the best
Using:
UIViewController *vc = [[UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil] instantiateInitialViewController];
[self presentViewController:vc animated:YES completion:^{}];
But I need to set a BOOL in the initial viewController for that story board.
I have BOOL declared in the .h and an if statement is dependent on it:
if (_boolIsTrue) {
FirstView *firstView = (FirstView *)[storyboard instantiateViewControllerWithIdentifier:#"first_view"];
[self.navigationController setViewControllers:#[firstView] animated:NO];
}
else {
SecondView *secondView = (SecondView *)[storyboard instantiateViewControllerWithIdentifier:#"second_view"];
[self.navigationController setViewControllers:#[secondView] animated:NO];
}
When I try to access it with something like:
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
UIViewController *vc =[storybord instantiateInitialViewController];
vc does not give me access to the BOOL.
Not sure what it is that I am not understanding about ViewControllers and Storyboards, but I would really appreciate the help.
Thanks
EDIT AFTER SOLUTION:
Wain was exactly right!
Here's the exact working code since my gaps spanned a couple different concepts. In case it helps someone else:
UINavigationController *nav = [[UIStoryboard storyboardWithName:#“MyStoryBoard” bundle:nil] instantiateInitialViewController];
InitialLockSetupViewController *vc = nav.viewControllers.lastObject;
vc.isBoolTrue = YES;
[self presentViewController:nav animated:YES completion:^{}];
So you have to get the View Controller with the correct type in order to access the var. BUT if it's not set as the initial view controller in the storyboard you have to get it as the root (0-th) view controller on the stack. THEN you have to remember to push the nav controller NOT the view controller.
Thanks again Wain. This was a good lesson!
You're simply not understanding that the compiler needs to know the class type. Currently you're telling the compiler that the class is UIViewController and it obviously doesn't have your custom property. You need to set the class to your custom one, like:
MyViewController *vc = [storybord instantiateInitialViewController];
Also, make sure that you don't call instantiateInitialViewController more than once or you'll have multiple copies of the same class and might use the wrong one...
I am trying to implement UIPopoverController. I have seen some sample code but still confused on how to connect the popover view controller to storyboard. Help please.
if (!patientPopover) {
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
MyViewController * addPacientController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
UINavigationController * myNavController = [[UINavigationController alloc] initWithRootViewController:MyViewController];
patientPopover = [[UIPopoverController alloc] initWithContentViewController:myNavController];
patientPopover.delegate = self; // optional
}
[patientPopover presentPopoverFromBarButtonItem:yourInstanceBarButtonItemOrMethodArgumentAttachedToTheBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
It's good to have your popover in an instance variable so that you avoid allocating it each time you press the button. I am using here presenting the popover from a UIBarButtonItem, but you can also present it from any view, like :
[patientPopover presentPopoverFromRect:yourView.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
You don't need to visually see the actual popover in the storyboard or nib file, because it is only a container. You just need to see what goes inside it, and that is the view controller, and obviously, you can do that.
I am using storyboards, I have one viewcontroller and on click I need to show another view controller modally. I am trying using this code
[self presentViewController:zoomV animated:YES completion:NULL];
I am coming up with a blank screen.
This is how I create
zViewController *zoomV = [[zViewController alloc] init];
[self presentViewController:zoomV animated:YES completion:NULL];
I tried researching this and some answers revolve around using storyboards and not having a rootviewcontroller associated. So what I have is in the initial scene I have a navigationController, and from there I drag to another Viewcontroller a relationship which defines it as a rootViewcontroller. Is that sufficient ? or is this irrelevant?
Since you have your zViewController in your storyboard, you should instantiate your zViewController using UIStoryboard instantiateViewControllerWithIdentifier:.
In your first view controller, instead of creating the zViewController using alloc/init do this, of course setting an identifier for your zViewController in your storyboard.
zViewController *zoomV = [self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
[self presentViewController:zoomV
animated:YES
completion:NULL];
Also you could accomplish the same using a segue and executing it directly, without the need of instantiating the zViewController, but is up to you.
As a second(small) comment, do not name classes starting with lowercase in ObjC :).
You may refer below snippet for story board:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
AddNameViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"AddNameViewController.m"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:sfvc animated:YES completion:nil];
I'm trying to make an app using Interface Builder (instead of Storyboard) for the first time, but I'm stuck. Is it possible to change view using a ViewController in the same XIB?
I'd like that when I tap a button, the view changes to the other ViewController present in the same XIB file. Must I call back an action? If yes, which?
If you have 2 view controllers with 2 .xib files, like this
You can present the NextViewControler from your ViewController's button press method,
-(IBAction) yourButtonPressed:(id)sender;{
NextViewController *nextViewController = [[NextViewController alloc] initWithNibName:#"NextViewController" bundle:nil];
[self presentViewController:nextViewController animated:YES completion:nil];
}
EDIT :
You can present the same viewcontroller like this.
-(IBAction) yourButtonPressed:(id)sender;{
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:viewController animated:YES completion:nil];
}