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];
}
Related
I have a button inside my nib file. How can I programmatically present another view controller when the user taps on this button?
As I understood , you want to push new viewController on Button click . Please try this
- (IBAction)buttonClicked {
MyViewController *myViewController = [[MyViewController alloc]init];
[self presentViewController: myViewController animated:YES completion:nil];
}
First you need to create an action for your button, normally via Ctrl+drag. On that action implementation you should alloc the destination ViewController. The code would be something like this:
- (IBAction)buttonTapped {
MyViewController *modalViewController = [MyViewController new];
[self presentViewController:modalViewController animated:YES completion:nil];
}
Since you said that your file is a nib, I'm assuming that you aren't on a storyboard with both ViewControllers on it and the ViewController to be presented is code based. If this isn't the case, please elaborate your question.
As requested: Loading a VC from a storyboard
- (IBAction)buttonTapped {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
RSSViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"RSSVC"];
// OR If is initial
// RSSViewController *viewController = [storyboard instantiateInitialViewController];
viewController.feedURL = #"...";
[self presentViewController:viewController animated:YES completion:nil];
}
If you need, ViewController identifier should be set in the Identity Inspector in the Storyboard.
Refer the below screenshots
First : control+drag the button
Second: Once you release the mouse,it shows you the box
Third:Click Connection Dropdown box.It shows you 3 option
Fourth:Choose or Select Action from the drop down.
Five : Finally give action name
now in ViewContoller.h the action method is
- (IBAction)actionGoNextPage:(id)sender;
Then in ViewContoller.m
#import "SecondViewController.h"
- (IBAction)actionGoNextPage:(id)sender
{
SecondViewController *secondVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self presentViewController:secondVC animated:YES completion:nil];
}
I have added subview in a UIViewController and I want a new UIViewController Should be loaded on click of button.
Challenge is I am not able to use button in new UIViewController.
Code which I have write:
DetailsOfDayViewController *aViewController =[[DetailsOfDayViewController alloc] initWithNibName:#"DetailsOfDayViewController" bundle:nil];
aViewController.dateParsing = dateparsing;
[self addSubview:aViewController.view];
This will fix your issue. This is happening because your view controller is being deallocated when you don't have any strong pointers on it.
DetailsOfDayViewController *aViewController =[[DetailsOfDayViewController alloc] initWithNibName:#"DetailsOfDayViewController" bundle:nil];
aViewController.dateParsing = dateparsing;
[self addChildViewController:aViewController];
[self addSubview:aViewController.view];
Another fix will be connecting this view controller to a property:
#property(nonatomic,strong) DetailsOfDayViewController *aViewController;
And change your code like this:
self.aViewController = [[DetailsOfDayViewController alloc] initWithNibName:#"DetailsOfDayViewController" bundle:nil];
self.aViewController.dateParsing = dateparsing;
[self addSubview:self.aViewController.view];
For showing another controller you can following options
1. Add SubView
newViewControllerObj.view.frame = oldViewControllerObj(self).view.frame
[oldViewControllerObj(self).view.newViewControllerObj.view];
2. Present
[oldViewControllerObj(self) presentViewController: newViewControllerObj animated:YES completion:nil];
3. Push using Navigation (For this you already have to set navigation controller as root controller)
[oldViewControllerObj(self).navigationController pushViewController:newViewControllerObj animated:YES];
How do you switch between view controllers programatically in an effective manner?
I am currently utilising:
ViewController *viewController = [[ViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
However this does not display the items that I've inserted using the interface builder. Only the items I've inserted programatically.
I wish to do this, as I want to display a home button on each page.
Give your viewController identity in StoryBord
then into your code
UIViewController *documetController = [storyboard instantiateViewControllerWithIdentifier:#"MXDocumentViewController"];
[self.navigationController pushViewController:reviewListController animated:YES];
I'm using ECSliding and I have this problem!
In my project there are this files:
InitViewController (ECSlidingController)
FirstViewController (UIViewController)
SecondViewController (UIViewController)
LeftMenuViewController (UIViewController)
ThirdViewController (UIViewController)
I use InitView to instatiate my FirstView as the topview, sliding to right opens the LeftMenu.
In the LeftMenu there are 2 buttons, one button loads as topview the FirstView, the second one loads the SecondView.
In my First and SecondView there is an identical button that loads the ThirdView not as topview controller but as a new view:
ThirdViewController *third = [self.storyboard
instantiateViewControllerWithIdentifier:#"Third"];
[self presentViewController:third animated:YES completion:nil];
In my ThirdView are 2 buttons, one button loads the FirstView, the second one loads the SecondView.
As the ThirdView is not a topview but another view I have to recall ECSliding to open my FirstView or SecondView.
I succeeded loading the FirstView from my ThirdView by using my InitView
InitialSlidingViewController *home = [self.storyboard
instantiateViewControllerWithIdentifier:#"Init"];
[self presentViewController:home animated:YES completion:nil];
But how can I load the SecondView from my ThirdView?
My question basically is how can I load something that use ECSliding after a normal View.
From Third we want to go back to the sliding view controller and change the top view controller. What you're currently doing with this code:
InitialSlidingViewController *home = [self.storyboard instantiateViewControllerWithIdentifier:#"Init"];
[self presentViewController:home animated:YES completion:nil];
Is creating an entirely new sliding view controller to show. Eventually, by doing this, you'll run out of memory because you'll have hundreds of presented views allocated and never visible.
So, what we want to do is give Third a property:
#property (weak, nonatomic) ECSlidingViewController *slideController;
And we want to set that property before presenting Third:
ThirdViewController *third = [self.storyboard instantiateViewControllerWithIdentifier:#"Third"];
third.slideController = self.slidingViewController;
[self presentViewController:third animated:YES completion:nil];
Now, when one of the buttons is pressed on third we can say: "what's on display? Can we just dismiss or do we need to change and dismiss?":
- (void)oneButtonPressed {
if ([self.slideController.topViewController isKindOfClass:[SecondViewController class]]) {
FirstViewController *first = [self.storyboard instantiateViewControllerWithIdentifier:#"First"];
self.slideController.topViewController = first;
}
[self dismissViewControllerAnimated:YES];
}
You need to write the corresponding method for twoButtonPressed and you're done.
For your new comment, rather than presenting the third, instead you should put it into a navigation controller and present that. Then, when you need to present fourth and fifth you just push them into the nav controller and pop them off again. If required you can also give them a reference to the slideController.
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController :third];
[self presentViewController:nav animated:YES completion:nil];
How can i go from a uitabbarcontroller to a viewcontroller in fullscreen, retaining all the data and views/tabs on the uitabbarcontroller?
tks
Did you want to hide the TabBar or show a new view, but once you're done with the new view have the TabBar be unaffected?
In the case of the latter try something like:
ViewController *vc = [[ViewController alloc] initWith...];
[self presentModalViewController:vc];
Add ViewController on TabBar in fullScreen then:
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController " bundle:nil];
[self presentViewController:vc animated:YES completion:NULL];
Now dissmiss that Modal Controller like this in any event( of button may be)
[self removeFromParentViewController];
After this your tabbar will as it is: