I finally made the switch to Storyboards and i am having issues loading custom controllers which was pretty easy to do when using interface builder.
I have ViewControllerOne with two components: A UIView and UITableView as the subview.
I want the UITableView to be controlled by a custom tableview controller. If this was Interface builder i would have dropped a tableview controller onto the XIB, linked to the custom controller and made the connections and it would have been done.
Using storyboard, i don’t believe its possible to drop a UIViewController/UITableViewController onto a scene which already has a view controller, i relied on Objects to achieve this.
So i added a Object onto the scene and linked it to my custom tableview controller. I set up delegate/date source for my UITableView to point to the custom controller. I finally connected the UITableViews outlet to the custom controller.
When i compile this, the custom controllers delegate (for the table view) gets called but the viewDidLoad is never called.
The only way i can invoke viewDidLoad is if i move the UITableView out of ViewControllerOne. My understanding was that even though there is one view controller for a scene i can still manipulate the subviews using custom controllers.
Am i misunderstanding something or is there is a solution for this ?
Some screenshots
There is a bit of magic in that. Call self.view from awakeFromNib and flow will back to the rails
- (void)awakeFromNib
{
[super awakeFromNib];
// here comes the magic - call self.view and view will load as expected
NSLog(#"awakeFromNib %#", self.view)
}
you can call it from initWithNibName:bundle:
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(#"awakeFromNib %#", self.view);
}
return self;
}
the point is to call self.view because apparently something is done inside.
If I have understood your question correctly:
1 Open the storyboard and navigate to the table view controller that you would like to be of your custom type.
2 Click on the identity inspector in the right hand side panel.
3 Set the class to what it should be.
Related
This is puzzling me.
The context
The original tutorial I'm following.
Where the segue is added to the Main View via a custom segue:
- (void) perform {
MainViewController *source = (MainViewController *)self.sourceViewController;
UIViewController *destination = (UIViewController *) self.destinationViewController;
for(UIView *view in source.main.subviews){
[view removeFromSuperview];
}
source.currentViewController = destination;
destination.view.frame = CGRectMake(0, 0, source.main.frame.size.width, source.main.frame.size.height);
[source.main addSubview:destination.view];
}
The TextField is connected as delegate in the child View Controller. All things being equal I get the app crashed without any message.
The workaround
In the Main View Controller, in -(void)prepareForSegue: I've added [segue.destinationViewController setDelegate:self]; in the meantime I've added a property in the child View Controller id<UITextFieldDelegate> delegate and modified the textfield delegate as self.delegate.
This works, but the trouble is that I've to set the delegated methods in Main View Controller which is not quite efficient as I have more View Controllers to add.
The Objective
How do I set each View Controller to be the delegate for itself without crashing?
The immediate cause of your error is that the view controller that your views belong to is being deallocated. The fact that your views are on screen while their view controller is deallocated highlights a fundamental flaw in the approach of taking views off one view controller and adding them to another. View controller containment is the correct way to solve an issue like this.
Changing the currentViewController property to strong will fix the memory management issue you're seeing, but it's just a bandaid. Your currentViewController will still be missing rotation methods, appearance and disappearance methods, layout methods, and so forth. View controller containment ensures these methods get called for the view controller whose views are on screen.
Here is an altered version of your project that illustrates how to use view controller containment. I think that will be a better solution than manually removing and adding subviews of the view controllers themselves. See the Apple docs for more info on custom view controller containers.
At first, let's see crash report. Please, do the following:
1. Add Exception Breakpoint
2. Edit it as in the picture
You should create a custom class for the destinationViewController wich will implement UITextFieldDelegate
#interface DestinationViewController <UITextFieldDelegate>
#end
And from storyboard add the class to UIViewController that has TextField
And make the connections for elements and TextField delegate.
Implement delegate methods.
You will not need the implementation of prepareForSegue: anymore. You will have two different classes with different elements. Only if you need to pass something from source to destination then you use prepareForSegue:
Hope you'll understand
iOS 6 introduced the Embed Segue, allowing custom container controllers to be used in Storyboards. Is there anyway to duplicate this for iOS 5?
The challenge here is that the child view controller's view is often to be added as a subview of some container view of the parent view controller. Since you can't have segues from random UIView controls, that defies creating segues from a UIView container view to the child's scene. Thus, you just have to write the code yourself.
Fortunately, it's just those four lines of code referenced in Adding a Child Controller from the View Controller Programming Guide. Personally, I'd might even modify that code slightly, having the following method defined in my view controller:
- (void) displayChildController:(UIViewController*)childController
inContainerView:(UIView *)containerView
{
[self addChildViewController:childController]; // 1
childController.view.frame = containerView.bounds; // 2
[containerView addSubview:childController.view];
[childController didMoveToParentViewController:self]; // 3
}
I have, though, done custom segues for changing the active child controller from one scene to the next, but it essentially just a variation of the code listed later in the above referenced document. But that's not an embed segue question, so that's not relevant here
I duplicated the functionality by subclassing UIStoryboardSegue.
In Interface Builder, I create a custom segue and set its' class to my subclass (QCEmbedSegue). In my parent view controller's viewDidLoad, I call performSegueWithIdentifier:sender.
QCEmbedSegue simply overrides perform:
- (void)perform
{
[self.sourceViewController addChildViewController:self.destinationViewController];
[[self.sourceViewController view] addSubview:[self.destinationViewController view]];
[self.destinationViewController didMoveToParentViewController:self.sourceViewController];
}
http://www.quentamia.com/blog/embed-segue-in-ios-5/
I have a custom UIView (MyCustomUIView) which is built using Interface Builder. I'd like to place this custom view in MyViewController's view, which is also designed using IB. I've placed an UIView as a subview in MyViewController's XIB and set it's class to MyCustomUIView. The problem is, when I run the code, only a blank view appears. (When I instantiate MyCustomUIView in code, it displays well.)
I'm only overriding the initWithFrame: method the following way in MyCustomUIView.m:
- (id)initWithFrame:(CGRect)frame
{
[[NSBundle mainBundle] loadNibNamed:#"MyCustomUIView" owner:self options:nil];
self = self.view;
return self;
}
What should I do to make the view load properly? How should initWithCoder: look like?
You are correct. IB uses initWithCoder. initWithCoder should look very similar to your other init methods:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// CUSTOM INITIALIZATION HERE
}
return self;
}
Once you assign your class within IB, you won't need to instantiate it from the bundle unless I'm misunderstanding your intention.
Do not put anything in the view part of the view controller in IB. Instead, set the nib name for the view controller in IB to the name of the nib containing the view. In the nib containing the view, set the file's owner to the view controller class and hook up its view property to the view.
The result will be that when the view controller is instantiated, if it is instantiated from the nib (which you have not proved is what's really going to happen, but let's just say it is), it will find the nib and load the view from it.
Basically the rule is that it makes no difference where a view controller comes from, it will go through the same steps looking for its view in the same order, as I explain in my book:
http://www.apeth.com/iOSBook/ch19.html#_view_controller_and_view_creation
and in this webcast:
http://www.youtube.com/watch?v=zIufcKpDIRo
Hy
i have two classes uiviewcontroller and uiview. I have one view controller. Inside i have uiview. Inside uiview i have textfield and when i write a text and click done i need to refresh uiviewcontroller.
I tried with this in uiview class:
-(IBAction)textFieldReturn:(id)sender
{
ViewController *vc = [[ViewController alloc] init];
[vc viewDidLoad];
}
i need refresh the same as you click the button and open viewcontroller.
I am guessing you mean that you want to "refresh" the view, not the view controller. To do that simply call [self setNeedsDisplay] from the view, or [self.view setNeedsDisplay] from the view controller. Also make sure that the textfield is a subview of the uiview. Either do that in the nib file or in code by calling [self addSubview: (textfield here)].
Also, if you want to access the view controller from the view you will need to create an IBOutlet, simply allocating a new ViewController object within the view does not mean that the created view controller controls the view. Hopefully that makes sense. I'd recommend going through some ios starter tutorials as well. Just google that there are a lot.
I've been converting an application to use storyboards. I'm sure this is a simple problem, but somehow I can't seem to figure out the 'correct' way of doing it, coming as we are from the old XIB world.
One of the subsections of it contains a UITabBarController, each with some subviews within it.
The action that launches this set of tabs works perfectly; I detect the segue, and set some data properties within my (custom) UITabBarController.
Next, I would like to be able to pass that data to the child views when they get created. But - because these tabs are simply 'relationships' and not segue's, I can't do what I do everywhere else, which is override the 'prepareForSegue' function.
In the old XIB universe, I'd simply bind some IBOutlets together between the tab controller and the child views. But I can't do that in storyboards, because the parent and children are separate 'scenes'.
I've tried making my UITabBarController class implement its own delegate, override 'didSelectViewController' and doing 'self.delegate = self' which almost works, except for the fact that it is never called with the first tab when the view is initially shown.
What's the "correct" (or 'best') way to do this? Please don't tell me to get/set some value on the app delegate, as this is 'global variable' territory - nasty.
Try looping through the view controllers on the UITabBarController, e.g. in this example the setData method is called from the segue in to the UITabBarController, and it then loops through the child view controllers, making a similar call on the child controller to set the data on that too;
- (void)setData:(MyDataClass *)newData
{
if (_myData != newData) {
_myData = newData;
// Update the view.
[self configureView];
}
}
- (void) configureView {
for (UIViewController *v in self.viewControllers)
{
if ([v isKindOfClass:[MyDetailViewController class]])
{
MyDetailViewController *myViewController = v;
[myViewController setData:myData];
}
}
}