add xib UIView to storyboard view controller without using code - ios

I am trying to reuse a specific view. Say it's a voting system with five buttons. Since it appears in different scenes/screen, I just wanted to have one view and reuse it where needed. So I create the view in a xib file named MyStarSystem.xib. Now I want to simply drop my view into a storyboard view controller through the storyboard, not in code.
If all goes well, I know I should be able to do that by just dragging a UIView into the view controller and then set the class as UIVotingView. But where do I tell UIVotingView to draw the content of the xib? And of course, the buttons have to be interactive so I want to have IBOutlets and IBActions. So I was thinking of using -(void)drawRect:(CGRec)rect but that makes no sense as a solution. Does anyone know how I might create this view once and use it all over -- by dropping it into the storyboard?

I think you can accomplish what you want by making a base view controller in the storyboard, and connecting its IBActions and outlets. You can then copy and paste this controller in the storyboard -- depending on how you do it, the copy may appear directly over the old one, so you need to move it over to see both. Create as many of these as you want, and create new subclasses for them that all inherit from your base view controller. They should all have access to the outlets and actions that you made in the base controller (the outlets need to be in the .h file, not the class extension in the .m). You can then add any specific code in the subclasses, and any extra views in the storyboard to customize them.

To reuse all information from one controller to other you have many ways, but never coding is impossible call a xib from a Storyboard, but is simple to do let me explain:
1) To reuse your information class follow this step:
- open your class.h for your.xib and in the first line use this:
#import <UIKit/UIKit.h>
//here you have to import your voting view so:
#import "VotingView.h"
#interface ViewController : UIViewController {
}
#end
- now you have to import all function from you voting view to you xib view to reuse them:
#import <UIKit/UIKit.h>
#import "VotingView.h"
//change you UIViewController in to your VotingView like this
#interface ViewController : VotingView {
}
#end
ok now you can call all function in your ViewController.m from VotingView for example if in your VotingView have a - (void)voting {} now in your ViewController.m under ViewDidLoad you have write only [self voting];
Ok now how to call XIB from Storyboard:
go in your ViewController in storyboard and add a Button in Interface, using a IBAction to call a xib:
- (IBAction)callXib:(id)sender{
ViewController *viewController=[[ViewController alloc]initWithNibName:#"MyStarSystem" bundle:nil];
[self presentViewController:viewController animated:YES completion:nil];
}
Now to dismiss an back from xib to a storyboard create other Button and use this funcioon:
- (IBAction)backTo:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}
That's it hope this help you!

Related

Convert TableViewController to ViewController

I have started with a TableViewController but now I would like to change from a TableViewController to a ViewController in the storyboard. Is there an easy way to do this? I want to avoid creating a new View scene in the storyboard and copying the code over.
You have to delete the current TableViewController and create a new ViewController.
If you will set an TableView inside, you also must set DataSource/Delegate, and conform with the Table Views protocols in the ViewController Class.
You can do that using ContainerView. Take one UIViewController and add a ContainerView inside it. Control drag from the ContainerView to TableViewController and select EmbedIn. This is the only way to keep existing TableViewController, but you need to move your code from TableViewController to ViewController. Do this only if it is necessary to use TableViewController and you have to display a View outside the TableView.
Just start implementing ViewController protocol and remove TableViewController, TableViewControllerDelegate and TableViewControllerDataSource.
Also remove the methods from the TableViewDataSource and that's it.

How do I resolve "Static table views are only valid when embedded in UITableViewController instances" when creating an app?

I am an Objective-C beginner and I am going through the tutorial to create an IOS app using Apple developer articles.
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/SecondTutorial.html#//apple_ref/doc/uid/TP40011343-CH8-SW1
I have created an unwind segue and I have gotten stuck. I have gone through SO posts such as given below
StoryBoard issue in Xcode 6.1
Change a UIViewController to a UITableViewController inside a storyboard?
Want to create a cool static UI but : "Static table views are only valid..."
I tried to modify the story board source to use "tableViewController" instead of "viewController", but the storyboard won't open.
I am sure that there is an easy solution, but I don't know enough Objective-C or IOS development to know what it is, or how to implement it.
I have my controller implementing UITableViewController and my view as UITableView. I have attached the screenshot below.
and the error message:
My source for ToDoListTableViewController.h is given below:
#import <UIKit/UIKit.h>
#interface ToDoListTableViewController : UITableViewController
- (IBAction)unwindToList:(UIStoryboardSegue *)segue;
#end
and the implementation
#import "ToDoListTableViewController.h"
#interface ToDoListTableViewController ()
#end
#implementation ToDoListTableViewController
. . . Other methods
- (IBAction)unwindToList:(UIStoryboardSegue *)segue {
}
#end
Your picture is kind of small so it's a bit hard to tell what we're looking at. Plus your descriptions are a bit vague.
The deal is that in order to set up a table view as a static table view, it must be managed by a UITableViewController, not a regular UIViewController.
A UITableViewController is a special subclass of UIViewController. When you go to add a new scene to your storyboard, you go to the list of UI objects, find the UITableViewController, and drag one onto the storyboard.
An annoying thing about UITableViewController objects is that the ONLY thing they manage is a table view. You can't use them to set up labels, buttons, and other UI elements. Just a table view and nothing else.
You said:
'I tried to modify the story board source to use "tableViewController" instead of "viewController"...'
I have no idea what that means. What is a "story board source"? You don't "Modify the storyboard source", you drag a UITableViewController onto your storyboard.
Then you said "...but the storyboard won't open." I also don't know what that means. You're going to have to explain that.
Luckily, there is an easy solution to this.
What you want to do is to create a regular UIViewController to manage everything but the table view, and then put a "Container View" in that view controller and set up an "embed segue" that installs a UITableViewController inside that container view.
Here's how you do that:
Search in the list of UI elements on the right side for "container". Drag a container view onto your view controller where you want your table view to appear. Then drag a UITableViewController onto a blank space on your storyboard to create a new storyboard scene. Then control-drag from the container view in your first view controller onto the UITableViewController. This creates an embed segue, which causes the UITableViewController to be loaded as a child view controller with its view inside and sized to fit in the container view.
Now you can have a window that has both a table view managed by a UITableViewController AND other contents.
There are more details to setting this up that are beyond the scope of a SO post. I suggest you do some googling on container views and embed segues and try to find a tutorial on setting them up.

Xcode: Control drag only works in first storyboard view controller

When I make a project in Xcode and choose template to Single view application, I can only just control drag the first storyboard's view controller elements to ViewController .m and .h. I understand, I chose SINGLE view application, but I think there should be another option.
I tested "Tabbed Application" and that template creates own .h and .m files for every view controller. I tested to rename ViewController.h and .m to FirstViewContoller.h and .m. It was messed now. So, I deleted the project and here I am.
What should I do?
Assuming that you want to create IBOutlets and IBActions in new view controllers.
When you create a new view controller in storyboard, go to Identity Inspector and set Custom Class to the new View controller. Then you can Control drag the elements to the view controller.
When you renamed the files did you rename the #interface and #implementation to match your new class name as well? In the header (.h file) it should say
#interface FirstViewController : UIViewController
and in the implementation (.m file) it should say
#implementation FirstViewController
Hope that helps

Unable to link UIViewController IBOutlets with StoryBoard

I have two UIViewControllers (A and B) in storyboard. The A view controller has a property
#property (nonatomic, retain) IBOutlet UIViewController *viewController;
that I want to link via storyboard to B.
The outlet shows under the the IBOutlets section in storyboard menu but I'm unable to link.
It might seem strange why I'm trying to do that but I need it. Someone knows how to do that?
IBOutlets are connections within a single view controller. Create references to objects inside the view controller so you can use those objects in code.
You cannot create IBOutlets from one view controller to another one. A property is the correct way to go, but you have to assign the property in code. Normally when one view controller creates another one, it might set a reference to itself.
OtherViewController *otherViewController = [OtherViewController alloc] init];
otherViewController.masterViewController = self;
// at this point "otherViewController" has a reference to the current view controller
Now I understand what I need to do. I need to create a custom segue to achieve the same result as when you link a UINavigationController to other ViewController and mark as RootViewController. It is done by segue and not by IBOutlet.
I am a bit late to the party however I put together a segue class to help with this job. View the git repository to see the class in action: https://github.com/datinc/DATOutletSegue.
Basically it uses the identifier of the segue to connect to the parent controller

An UIViewController inside other UIviewController and IBActions

I'm designing an iPad app where little UIScrollViews have their own UIViewController . Those controllers have in their view a button that call an IBAction method. But it isnt working, in fact, it doesnt seem that they are being pressed in the simulator.
Here is some code to give you an idea of what im am doing .
// In UIViewController A (say the parent or root that have several UIScrollViews)
MiniViewController * mini = [[MiniViewController alloc]init];
[scrollView1 addSubview:mini.view];
//repeat the same process a couple of times with different uiscrollsviews and instances of miniviewcontrollers
Now The MiniController is very simple as you can guess, i only post the .h file
#interface MiniControlador : UIViewController {
IBOutlet UIButton * button;
}
#property (nonatomic, retain) IBOutlet UIButton * button;
- (IBAction)doSomething:(id)sender;
#end
You can see that i used Interface builder to connect an UIButton "button" to a method called doSomething. But as i already said, it isnt working.
One more thing. I also tried to add a button to the UIScrollView with the Mini Controller instance programmatically.And it worked! But I certainly believe that it's extremely hardcoded.
What do you think? I'll appreciate any suggestion(s).
Apple's View Controller Programming Guide is an important read, and explains a lot about Apple's philosophy of one-view-controller-per-screen.
Much of the behavior of view controllers is built on the assumption that there is only one view controller operating at a time. When that assumption is violated, the behavior is undefined (or at least undocumented). In this case, your description suggests that the normal view controller behavior of inserting the controller into the responder chain between its root view and that root's superview (usually the previous screen) isn't working.
While you may find methods of initialization that do work properly, they're not going to be guaranteed to work, and the behavior is liable to change with future OS updates.
Edit: A relevant quotes from the View Controller Programming Guide:
Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy. In iPhone applications, the views in a view hierarchy traditionally cover the entire screen, but in iPad applications they may cover only a portion of the screen. The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content.
Thanks guys, I finally solve this using objects of a class (that I called GenericViewController). It actually acts like a regular UIViewController, the IBActions responds well to any event (i.e. buttons being pressed).
I used an IBOutlet UIView in order to contain UILabels, buttons...and so on.
Here is some code if anyone is interested.
#interface GenericViewController : NSObject {
/* Some IBOutlets here*/
//like a regular UIView of an UIViewController, this holds the rest of the outlets
IBOutlet UIView * view;
}
//some IBActions here
}
Then the UIScrollView only add the view of each GenericViewController object
[scrollView addSubview:genericViewControllerObject.view];
If anyone has a better solution, please let me know :)
Are you sure you are loading the view from the xib you made in InterfaceBuilder?
I'm doing something similar in my app, and it's working for me.
I'm implementing the init method like this:
- (id)init
{
if (self = [super initWithNibName:#"__your_xib_name__" bundle:[NSBundle mainBundle]])
{
// TODO: Add additional initializing here
// ...
}
return self;
}
If you are not loading the view from the xib, then there will be no connections made (no IBOutlets initialized and no IBActions triggered).

Resources