Create a standalone view in storyboard to use programmatically - objective-c

I'm trying to create and design a UIView using a storyboard but include it in a UIActionSheet programmatically. This is basically to avoid using CoreGraphics positioning functions with pixels
In old xcode I remember that it was possible to drag a UIView onto the nib without any controllers.
The view obviously has to be connected to the class, so it would have an IBOutlet, but not being added to the self.view
One thing that makes me feel like this should be possible is that if you drag a UIView into the controller black bar in storyboard it pops into place like so:
But its not shown on the screen itself. What is this feature for? Can I somehow open up this view and design it a bit?

Just create a new .xib file.
Right Click somewhere in the Navigator Area and select 'New File...'.
Select 'User Interface' from the list on the right and Select 'View'.
Click 'Next', 'Next', give your new view a name, and 'Create'.
A new .xib fill will be added to your project.
Double clicking on the new .xib file and it opens in Interface Builder (not Storyboard).
Edit/Design your View to your liking.
Then after you have your new view (.xib) in Interface Builder, it's a simple matter of creating a new subclass of UIView (ex. MyView), switching the class of your new view (.xib) to MyView, creating an instance of MyView in your controller, and adding it as a subview to your other view.
*And to answer your question about that little black bar at the bottom, it's called the 'Dock', and it's just a mini representation of the top-level documents of your scene. The dock is convenient for quickly dragging/dropping icons onto and making connections. See apple's storyboard description here. Ray Wenderlich has an easy to follow tutorial on storyboards here.

You cannot have a UIView outside of a UIViewController on a storyboard. I'm guessing it's because the storyboard would have no idea how to identify or instantiate with the current API. It is something I've had a use for myself. The solution is just use a XIB for the one UIView and load it up programmatically (just like used to do). I've found using a storyboard for most items and couple XIBs for re-usable views across multiple view controllers do work nicely together.
Here is some code I use to load a XIB as part of a custom object with the object gets initialized.
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[[[NSBundle mainBundle] loadNibNamed:#"BannerView" owner:self options:nil] objectAtIndex:0];
[self addSubview:self.view];
self.frame = self.view.frame;
}
return self;
}
As for dragging views down into the black bar on the storyboards. Those views are still part of the UIViewController, but they aren't a `subview' of the top level view. I think the document outline shows the hierarchy nicely.
The following view has 2.1.1 View, 2.1.2 View, etc outside of my main view hierarchy because they aren't subviews of my main view. The result is, they won't be displayed by default. I do have IBOutlets setup and I conditionally add/remove them from my main view hierarchy using the standard addSubview: and removeFromSuperview.

Related

subviews with UIButtons - how to connect the buttons to IBActions

I am trying to create a simple app which uses a main view and has a smaller subview within it. I need to have buttons in the subview and I am having trouble getting the connections for the buttons to work. I have done the following:
Create a new View-based project, which gave me the ViewController.h & .m, the MainStoryboard.storyboard and the AppDelegate.h & .m.
Create the subView using NewFile - Objective-C Class - then naming it "subView1" and making it a subclass of UIViewController and checking the with xib check box.
This gave me the subView1.h &.m files and the subView1.xib.
I then re-sized the subView in the xib, by setting it's size to "FreeForm" in the attributes inspector and then specifying the width (to 280) and height (to 300) in the size inspector. I also change the background colour to differentiate it from the main view.
I then dragged a UIButton into the subView and connected it as an IBAction (which i named "clickButton1") to the subView1.h file using touchupinside.
For testing purposes only i then used a simple NSLog to check the functionality of the button which i placed in the subView.m file as follows:
-(IBAction)clickButton1:(id)sender {
NSLog(#"It Worked!");
}
In my ViewController.m file in the viewDidLoad i then added the following code to add the subview to the main view:
subView1 *sv1 = [[subView1 alloc]init];
sv1.view.frame = CGRectMake(20,120,280,300);
[self.view addSubview: sv1.view];
This all worked fine, and when i run the app i get the main view and the subview appear as expected. The problem is when i click on the button which is located in the subview it crashes with the following error:
Thread1:EXC_BAD_ACCESS(code=1, address=0xe00000008)
From what i have read i believe this may have something to do with how I am adding the subview and the fact that I am using ARC. Something about once the subview is added it is automatically released and therefore all buttons/connections etc within the subview are lost.
So my two questions are 1) Am i missing something silly here and is there an easy fix? and 2) Is this an appropriate way to create an app which uses subviews with buttons within them or is there a better way? Thanks to anyone who takes the time to answer!
I tried to reiterate what you said.
So you first made a View-Based Application (checking the "Use Storyboards" checkbox), and you put a button in the storyboard. Then, you control-dragged the button to the "ViewController.h" file. If that is right, you should just be able to put
NSLog(#"It Worked!");
in the method implementation (at least, I could do that)

Combined UITableView with other elements - how to create and use the view?

Ive a project close to doing everything I need it to do. Its got a Main page which has four buttons that allow you to choose an option. Then a tableview page is launched from those options and displays a parsed XML feed from a website. You can then select one of the options in the table to see a detail view of the item, enquire about it, etc.
My problem is I need to add more elements to the TableViewController page, other than the tableview itself. I want a customized back button (not the navigation controller standard) plus some labels, images, etc.
In the TableViewController xib, the tableview itself fills the page. I cant resize it to add more elements above it. I can add a 'view' window seemingly above the tableview and put things in it. But it seems to add the view to the tableview. This means that when I scroll the table, the other elements like new back button, scroll away as part of the table.
So I'm led to wonder whether I need this page not to be a tableviewcontroller, but a viewcontroller, with a tableview inside it, as well as my other view with buttons, etc. Is that the right way to go? But if thats the case, then how do I instantiate the tableviewcontroller within code? Because my page will not be of that type anymore - it will be just a viewcontroller. But in code Im making a tableviewcontroller .. slightly scared by that route tbh.
Any illumination on this would be much appreciated, as various searches have left me none the wiser. Thanks.
To customize it, this is the way to go:
Change your class to be a view controller instead, which implements the TableViewDelegate and TableViewData Source protocols.
In the view didLoad of you controller, create the table view, set its delegate, data source, and any other properties you wish and add it as a subview to your view.
tableView = [[[UITableView alloc] init] autorelease];
tableView.delegate = self;
tableView.dataSource = self;
// .. Other customization
[self.view addSubview:tableView];
I suggest doing this programatically rather than IB.
Instead of a UITableViewController, you want a UIViewController that has an IBOutlet UITableView. Drag and drop a UITableView component from Storyboard and hook it up, and position it as needed on the screen. This UIViewController should implement the UITableViewDelegate and UITableViewDataSource methods.
Edit: Remember that a UITableViewController is just a subclass of UIViewController so you don't really have to get rid of all your code. :) You only need to make minor tweaks to your header and main file, and change the Storyboard to be a UIViewController instead of UITableViewController as I mentioned above.

How to design separate UIView outside of any ViewController in Storyboard?

I would like to design a UIView, which is larger than a ViewController in Storyboard (iOS 5).
The UIView should be used as the subview of a UIScrollView and hence be larger than any of my existing ViewControllers. How can I create such a UIView in Storyboard and associate it with my UIScrollView?
I would like to do this without xib files if possible.
Thank you!
I see no other option than using xibs, but it's not that annoying:
//We have file called "View.xib" in our project. It contains one SINGLE view
NSArray *xibContents = [[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:nil];
UIView *view = [xibContents lastObject]; //safer than objectAtIndex:0
[self.scrollview addSubview:view];
self.scrollview.contentSize = view.frame.size;
In order to make IB connections you can set the filesOwner class in the xib to be your viewController, and connect like usual.
You can place a UIView into your scrollview and directly design it inside the viewController of your scrollView
I've found the way to edit the view added to the scene (being at the same hierarchy level as ViewController).
Unfortunately it is from hackish types of actions.
My Xcode version is Version 4.5.2 (4G2008a). I've tested this in real project and new empty project.
The basic idea is that Xcode do have an ability to edit such external views, unfortunately this mode doesn't activate straightforwardly.
In the method that I've found you need to have 2 levels of hierarchy inside your external view:
Scene
|- VC
|- View
|- ExternalView
|- SubView1
|- SubView2
Then goto Document Outline panel
find SubView2 in the tree of your scene
double click it
The editing area will appear and its coordinates will be saved to project's user data, so you can move it to more suitable place if you want to, and next time you'll open the storyboard in IB on the machine it will be there. Although I think on other machines you'll have to do it again (I haven't tested that).

Subviews added programmatically to UIView in storyboards does not responds to action

I have a scroll view with many elements that I had to build as a separate xib because Storyboards graphic interface was clipping it and made me impossible to work with.
The xib is built with interface builder, setting the file's owner graphically to the same view controller allow me to ctrl-click and link between buttons and methods in it.
The view is added like this in the viewDidLoad method:
UIView *w = [[[NSBundle mainBundle] loadNibNamed:#"MainView" owner:self options:nil] objectAtIndex:0];
[self.myView.subviewolder addSubview:w];
where myView is the main view and subviewHolder is UIScrollView the container, both of them are linked to the controller, and the subview get added and display just fine. Self is of course the view controller.
What seems to not responds are the actions in view controller linked to the UIButtons I have in the subviews. I have put some breakpoints but the flow is just not passing there.
What am I missing ?
thanks
The answer was really simple, what I did wrong is to declare subviewholder much more smaller in height than the added subviews.
I thought that subviewholder would act as a placeholder, but instead it was causing the subviews to be displayed correctly but 'clipped' for what concerning the user interaction, therefore the IBAction was never called.

UISplitViewController: how to get toolbar if details controller is UITableView?

I checked out Apple's example on how to exchange detail views in the UISplitViewController and it seems that they put the UIToolbar in every detail controller. Then, if the device is rotated, they hide the toolbar or show it and add a popover button which will show the root controller.
I'd like to adopt this pattern to show my root controller in a popover using a button in the toolbar, but unfortunately, my detail controllers are all UITableViewControllers and they do not allow adding other UI elements than a table view. So how do I deal with that? Is there an example around?
René
I think I figured out by myself: DON'T use a ´UITableViewController´ and a UITableView as root view in your NIB, as you cannot add a UIToolbar to the table view.
Instead: In the NIB, put a standard view and drag a UIToolbar and a UITableView on it.
Connect the standard view to the controller's "view" outlet.
Add another outlet and make it a UITableView. Connect the table view to it.
In the code: Let your controller inherit from UIViewController and not from UITableViewController.
Add a property to your controller to get the TableView to make it look compatible to UITableViewController.
public UITableView TableView
{
get { return this.viewTableView; }
}
Upon the viewDidRotate event you will have to adjust the table views width and height now (UITableViewController did that job for you before):
this.TableView.Frame = new RectangleF(0, 44, this.SuperView.Frame.Width, this.SuperView.Frame.Height);
The 44 pixels com from the parent view's toolbar.
I don't miss UITableViewController. I know there are some issues like automatic scrolling when editing, but in my case this is simply not needed.
René
Check out this example and the corresponding code. If I understand your question, this should show you how to do what you're looking to accomplish.
Also, just as an FYI to everyone, another MT user MonoTouched the MultipleDetailViews example that you linked to above.