Here is the scenario
Create new project Tabbed Application. Add new controller, TestViewController with XIB file, in the XIB file just add one button with text "TestViewControllerButton".
If I create a button in FirstViewController, and add an action to go to TestViewController, the button in XIB file is displaed.
TestViewController* vc = [[TestViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
The problem is when I add TestViewController to tab controller (index 2, start from 0), it does not show the XIB (I mean, the button does not displayed).
Here are my steps. I add UIViewController in Tab Controller, then change the class to TestViewController in Identity Inspector. Then add Tab Bar Item to TestViewController and link it to Tab Bar Controller, so when the last tab is clicked, it linked to TestViewController. But the problem is, the button in the XIB is not displayed (I add the button in TestViewController.xib).
Yeah, off course I can add the button directly in Main.storyboard in my TestViewController, but the idea is I want to manage the UI in TestViewController XIB, and the main.storyboard just load the view in TestViewController image.
Ok, I add some screenshot to make understand. This screen runs well when I do programmatically, run in UIViewController (code above - self presentViewController:vc animated:YES completion:nil).
When I run from Tab
The setting I think is already correct (since it runs well in UIViewController)
Already set file owner to the TestViewController
Already bind the outlet
Thanks!
I think you are using the ViewController of size Inferred , Change it to iPhone 4 inch in Attribute Inspector (Simulated Metrics -> Size and set the button frame acc. to that .
Hope this help :)
Of course it will not show the controls. See, you are using two different view controllers at these places. In the xib, you have actually added your button and hence when an instance of your testViewController is created from the xib, it has the button and its associated action with it. However, in the case when you add a viewController in your storyboard and set its class to be that of TestViewController, all you are doing is setting the class type of the new view controller to be TestViewController. But you are not providing any information by which the storyboard can know that it has to create the new instance of testViewController from the image saved in its XIB. So it just crates a new view controller of type TestViewController, using its image from inside the storyboard (without the button in it).
So to get your view controller from the xib you will have to override initWithCoder: method inside your TestViewController implementation and return an instance of your viewController from the XIB. Something like:
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
return [self initWithNibName:nil bundle:nil];
}
Related
I have a storyboard view controller with some labels and a progress bar.
In my view, I load the storyboard view with:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:[NSBundle mainBundle]];
GeodatebaseLoadingViewController *geodatabaseLoading = [storyboard instantiateViewControllerWithIdentifier:#"GeodatabaseLoading"];
[self.view addSubview:geodatabaseLoading.view];
Edit:
I linked the progress bar to the GeodatabaseLoading class, and access it with geodatabaseLoading.progressBar
Am I doing this right?
Thanks
You can't access the progressbar in the viewcontroller instance you have just created because its view won't be loaded until you display it on the screen. This means that there is no progress for you to access at that point.
If you want to update the progressbar in geodatabaseLoading, then you should define an ivar inside GeodatebaseLoadingViewController and set its value whenever you need. Then in GeodatebaseLoadingViewControllers viewWillAppear method, you can set the value of the actual progressbar (which is now guaranteed to exist) using the ivar you had created.
Your code, in its current state would work but it is a poor practice that needs to be changed. You can't randomly add a view to the screen just so you can access an outlet connected to one of its subviews.
Add a new class which inherits UIViewController. Lets call it NorCalKnockOutViewController In interface builder go to the class inspector and set the class of your loading controller to this new class.
Change you code to:
NorCalKnockOutViewController *loading = [storyboard instantiateViewControllerWithIdentifier:#"Loading"];
CTRL drag the progress bar into the header for your class to create an IBOutlet for the progress bar. Call it progressBar.
You can now access it in your code using:
UIProgressBar *pBar=loading.progressBar;
I have a functioning app, and I'd like to just add a new view and have an existing button change views with an IBAction function. I've set up the following and get no response. Thanks for any help.
MainViewController.h
- (IBAction)showSettings:(UIButton *)sender;
MainViewController.m
-(void)showSettings:(UIButton*)btn{
SettingsViewController *oView = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
[self.navigationController pushViewController:oView animated:YES];
}
SettingsViewController.h (the new view) is relatively empty.
SettingsViewController.m
boilerplate
Three things to make sure:
showSettings: gets called when the button is tapped (if not then perhaps you didn't connect the button tap event to the action at all)
SettingsViewController instance gets alloc and initialised
self.navigationController is not nil (if it's nil perhaps the current view controller is not embed in a navigation controller in the first place?).
Update:
Now we see the issue is #3. To embed your first view controller to a UINavigationController so you could push the second view controller:
Select your first view controller in storyboard
Select (menu) Editor -> Embed In -> Navigation Controller
Have you wired up your button's TouchUpInside event to your showSettings: method? This is normally done in Interface Builder with a control-drag. If you set a breakpoint on the first line of showSettings:, do you hit the breakpoint when you press the button? If not this is likely your issue.
I have a custom UIView which is a xib file and then a class that controls this view. I also have a storyboard which a lot of different view controllers inside. How do I perform a segue from the UIView to a specific UIViewController that is inside the storyboard?
Thanks
You can't do that. What you can do is give the UIViewController a storyboard ID, using the menu on the right of the interface builder.
Then call it programatically, like so:
MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self.navigationController pushViewController:vc animated:YES];
You could put a ViewController into the Storyboard, set the class to your custom ViewController. Then, if you have class files for the view in the xib, set your custom view as the VC's view (already saves some code in loadView) and then just add a segue from the custom VC to the other view controller. To trigger that segue, you have to call [self performSegueWithIdentifier:#"identifierOfSegue"] in your custom ViewController.
I make a Tabbed Application using storyboard template, two view controllers are embedded.
This is what I want to do: in the first viewController, let TabBar to select the second viewController programmatically.
The first viewController is a tableViewController, shows a list of items, and each item will push to a detailViewController. In the detailViewController, I edit some information and save the item. Then I want app to show the second ViewController, which is a tableViewController shows saved item.
Usually, we can use [TabBarController setSelectedIndex:1]; to select the second viewController.
However, since this is a storyboard template application, so many code are hidden behind. So I cannot get the TabBar instance in the first viewController, and use setSelectedIndex method.
This is what confuses me...
And now, I have found the solution for this problem. My answer is below.
I have figured out how to solve this problem.
First I add new a class: MyTabBarController.
Then, in storyboard, select the Tab Bar Controller, in identity inspector panel, set the custom class to this new class.
For the first viewController class, add a property
#property (nonatomic, weak) UITabBarController *tabBarController;
Then add - (void)viewDidAppear:(BOOL)animated in MyTabBarController class:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UINavigationController *navigationController = [self.viewControllers objectAtIndex:0];
FirstViewController *firstViewController = (FirstViewController *)navigationController.topViewController;
firstViewController.tabBarController = self;
In this way, I pass the tabBarController instance to the firstViewController, so, in the firstViewController, I can call [tabBarController setSelectedIndex:1];
Storyboard gives me a visual interface, however, it hides so many things behind.
in my iPad-app I am trying to present one of my views with a modal formsheet-style.
Here's some code:
-(void)present
{
SecondViewController *modal = [[SecondViewController alloc]init];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentModalViewController:modal animated:YES];
}
I am using Storyboard, and I have put stuff like a textView and toolbars in the view I'd like to show. I have set the right class in Identity Inspector, and in the class-files I have checked that it's the right view appearing with putting NSLog(#"Right view");
When calling the void present, a view is appearing, but only as a dark-white square. Nothing og my content from Storyboard is in it, I even tried changing the background color of the view and the textView to see if something was just outside the square, but the whole thing stayed white. It feels like it's not using the view I created in storyboard, but I have set it to the correct class, and the NSLog gets printed out when calling it. I have not connected the two views in any way in Storyboard, the SecondViewController is just floating around, so that might be the problem? The button that calls for -(void)present is created programmatically, so I can't ctrl+drag it to the button either.
Why is it showing an empty version of my class?
In the "Identity Inspector" set a "Storyboard ID" for your ViewController, and then present it like this:
-(void)present
{
SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:#"myStoryboardID"];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentModalViewController:modal animated:YES];
}
And if you're using iOS6, presentModalViewController:animated: is deprecated, so use this:
-(void)present
{
SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:#"myStoryboardID"];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentViewController:modal animated:YES completion:nil];
}
Your problem is that you're assuming the program will intrinsically know where to find the, already laid out, view for this controller when that's simply not how storyboards work. The code you list about will create a view controller, but without an associated view it will simply show as a black square.
There's a few ways to solve your dilemma:
Add the modal transition as a segue in the view controller, this would be the simplest way and is what iOS storyboards expect you to do.
Move the view from the storyboard to an external .xib and call the initWithNibName:bundle: method to load this as your view controller's view. This is the best solution if you just want to programmatically load the view.
Load the view from your storyboard programmatically with the instantiateViewControllerWithIdentifier: method, this is probably a bad idea as it goes against the design of storyboards.
I can elaborate on those if you want.