I have a storyboard that handles all of my applications UI, apart from the header on each page which is handled by its own .xib file following this guide:
http://patientprogrammer.wordpress.com/2012/03/12/re-usable-subviews-in-ios/
Within my header .xib I have a button that I want to have when clicked load a view that is part of my story board.
I have tried:
- (IBAction)clicked:(id)sender {
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
someViewController *storyViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:#"someViewController"];
[self.navigationController pushViewController:storyViewController animated:YES];
}
However as my class uses the interface UIView controller navigationController is not found, what can I do to launch a view within the story board from my UIView.
Or if there another interface I can use that will still let me have this as a subview.
what can I do to launch a view within the story board from my UIView
This is bad. A view is a view. Its purpose is to show something to the user, not controlling the app.
UIViewController is where you will need to do this. The navigationController is a property on this class, not on UIView. Read UIViewController class reference for more info.
Related
I've search a lot but I can't do yet.
I've my .xib UI with some buttons, separated from main storyboard.
I need to perform an action when I press one of this button and show another view.
How can I do this directly from code such ad inside an IBAction?
You can't perform segues from XIB to Storybaord,
Instead of it you need
1) Get Storyboard's instance.
2) Instantiate a ViewController that is inside the Storyboard (By using Storyboard ID).
3) Push that ViewController to to your navigation stack.
Here is sample code
UIViewController *vc = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"yourVcIdentifier"];
[self.navigationController pushViewController:vc animated:YES];
#"MainStoryboard" - is the file name of storybaord (note. it's without .storyboard extension)
#"yourVcIdentifier" - is ViewController's Storybaord ID.
To give a Storybaord ID to some ViewController, open the storybaord, click on the ViewController and specify the "Storyboard ID" field.
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 created empty IOS project and want to create UI. I searched for information about connecting storyboard to code but there is nothing about it, just about xib files. So is it possible to use storyboard instead xib in empty project? And how?
You can get the storyboard object like this assuming you have a Storyboard file already in the Project
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:[NSBundle mainBundle]];
YourViewController *yourVC = [storyBoard instantiateViewControllerWithIdentifier:#"identifier"];
//Do whatever with the viewcontroller object
[self.navigationController pushViewController:yourVC animated:YES ];
Note:
Check the Storyboard name
Check your viewcontroller identifier in the Storyboard
First create an empty project with some class name
then create an storyboard from file->userInterface->storyboard
then give a name for the storyboard after that press storyboard in the left menu and place a viewcontroller and hold viewcontroller give class name as already created Uiviewcontrollerclass
finally press project choose main interface as ViewController.storyboard
if you wanna code suppose segue use [self performSegueWithIdentifier: #"nameofcontroller" sender: self];
if don want segue go like this
[self performSegueWithIdentifier: #"TheSequeName" sender: self];
The #"TheSequeName" is defined when you ctrl drag a button to open a new view controller on the storyboard
If you don't use segue, there are many variants to open a new view controller. The basic one is using a separate NIB (without storyboard)
SecondViewController *view = [[SecondViewController allow] initWithNibName:#"NibName" bundle:nil];
If you declare your view controller on Storyboard, you can use
viewController *view = [self.storyboard instantiateViewControllerWithIdentifier:#"viewStoryboardId"];
Then you show your viewController using navigation controller
[self.navigationController pushViewController:view animated:YES];
Hope it works
From the 'File' menu, create a new Storyboard.
Call it Main.storyboard.
In your project settings, set it as the main interface file under the Deployment Info.
New File - > Objective-C Class (of type View Controller) -> without Xib file
now on on storyboard, go to that particular xib file
in property window -> Show Identity Inspector -> change class to above added ViewController.
That's it, now you can control your storyboard xib files from code.
I am using a split view controller and wanting to add multiple table views to the master view controller.
So I can take advantage of prototype cells that (I believe) you can only get from UITableViewController I have put the UITableViewController into a second storyboard for the app. I am then substantiating it with:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
MyTableViewController *myTableViewController = [storyboard instantiateInitialViewController];
I then add the view of this UITableViewController as a subview of my UIViewController.
[self.view addSubview:myTableViewController.view];
When I run this, I can select and highlight rows just fine. But didSelectRowAtIndexPath is not being called for some reason?
Thanks for any help,
Richard
Add this line
myTableViewController.view.delegate = myTableViewController;
or Make sure you have done the same in XIB(Interface Builder).
Turns out that I needed to add the uitableviewcontroller as a child controller of the uiviewcontroller as well. Just adding the view alone was not enough.
I have a problem with switching views. I'm using a simulator with xcode 4.2
Storyboard contains:
NavigationController (initial view controller)
UIViewController which has relationship with the navigation controller
UIViewController (paired with my custom class: ViewEntryImageController) which hasn't got any relationship. Contains a button, a bottom toolbar with some toolbar button.
User come into the UIViewController, where he can see a ScrollView and in ScrollView some images.
Images has a gesture:
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(openEntryImage)];
[image addGestureRecognizer:recognizer];
[recognizer release];
The openEntryImage function:
(IBAction)openEntryImage
{
ViewEntryImageController *controller=[[ViewEntryImageController alloc]initWithNibName:nil bundle:nil];
controller.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:controller animated:YES];
[controller release];
}
When I try to tap the image, the openEntryImage works as well (the effect is correct), but I don't see my ViewEntryImageController view and my buttons, I'm only see a black window.
I try to put a NSLog line into the ViewEntryImageController viewDidLoad function, and it works, so what is the black window and where is my view controller?
When I try to use pushViewController, on the new view I found a navigation toolbar with a back button, but no other controls.
I tried another version, I created a UIViewController class, but now with a xib file. I used it instead of ViewEntryImageController and it works. Why?
I want to use this controller in storyboard too.
The ViewEntryImageController class by itself has no information about how to build the dialog. But you can instantiate your view controller on your own from the storyboard:
UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:#"StoryboardFileName" bundle:nil];
ViewEntryImageController *controller = (ViewEntryImageController *)[myStoryboard instantiateViewControllerWithIdentifier:#"ViewEntryImage"];
This assumes a storyboard name of StoryboardFileName and that the view entry image controller has an identifier of ViewEntryImage set in the view properties (Attributes inspector, section "View Controller").
Try it like this :
ViewEntryImageController *controller=[[ViewEntryImageController alloc]initWithNibName:#"ViewEntryImageController" bundle:nil];
If you don't use .nib names but rather use storyboards, it's a bit harder. Create a segue from the controller to the ViewEntryImageController controller by holding ctrl and dragging from one view to the other. Click this segue and give it an identifier.
Then use the [self performSegue:#"identifier"]; function to present the next view.