How iOS UI component controller works? - ios

This code works well:
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[pickerView addTarget:self
action:#selector(pickerChanged:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pickerView];
Why i don't need to add pickers controller to my UIViewControllers hierarchy?
[self addChildViewController:pickerView.controller];
I know, UIView has no controller property. But how UIPIcker component adds his controller to controllers hierarchy? Or UIPicker have no controller at all? Or this controller don't need to be in controllers hierarchy?
I need to know this to develop my own custom UI components, that should should be easy to integrate.

UIDatePicker is a subclass of UIControl that is a subclass of UIView
So its not a UIViewController, adding UIView to the views is adding using addSubView
If UIDatePicker was a subclass of UIViewController then indeed you would have to add it using addChildViewController
When you create your custom views its your coice, it will depend on what class you extend, if you extend UIView then you will need to use addSubView, if it will extend UIViewController then use addChildViewController

Related

How to make a view controller without a xib or a storyboard vc identifier?

I wanted to know how to make a viewcontroller without the use of xib or storyboard. So in this case, I would be getting the viewcontroller as such
PopupViewController* vc = [[PopupViewController alloc] init];
[self addChildViewController:vc];
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
I know it has to do something with overriding the init method since we are not using initWithCoder ?
I know I have to create a view and set it as the self.view for the PopupViewController, but I was wondering how I could do that.
EDIT: Yes it may be much easier just to make an xib file or add a view controller to the storyboard, this is to understand deeply how view controllers work.
The best place to init the view is in PopupViewController's loadView method. Something like:
- (void)loadView {
self.view = [MyViewClass new];
}
Then, in MyViewClass initWithFrame: method build all subviews and set constraints to it. If you're not using constraints, override layoutSubviews method.

Proper way to dismiss multiple UIViews?

I have a UIViewController (HomeView) that shows my UIView called GameView via a Segue.
[self performSegueWithIdentifier: #"segue_playgame" sender: self];
The GameView calls a UIView (PauseView) when the use presses a button. This pause view is shown via just adding the PauseView to the UIView.
UIView *pv = [[PauseView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:pv];
To remove the PauseView I call
[pv removeFromSuperview];
Is there a way to call an "End Game" method in the PauseView that will remove both the PauseView and the GameView taking the user back the HomeView (UIViewController)?
And side note, is there a better way to handle showing views and removing them? Or is how I am doing it pretty much standard?
What you are missing here is a UINavigationController. From the official documentation:
The UINavigationController class implements a specialized view
controller that manages the navigation of hierarchical content. This
navigation interface makes it possible to present your data
efficiently and makes it easier for the user to navigate that content.
You generally use this class as-is but you may also subclass to
customize the class behavior.
With your views managed by the UINavigationController stack you can use:
- (NSArray<__kindofUIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated
To pop back to the root view controller which in your case is your home view controller.

SearchDisplayController table not showing in custom UIView

I have an UIViewController. Inside the view controller there is a custom UIView class object added as subview. Now, inside the custom UIView class, I have a search display controller. When I do this
SearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:SearchBar contentsController:self]
it is giving warning as self is UIView object, not UIViewcontroller object, so search result is not showing.
I want to show search result from the UIView only.
How can I do that? Any kind of help is appreciated.
You need to first create a UIViewController with the custom view you need and then add this controller's view to your view. Something like:
[self.view addSubview:[[[UIViewController alloc] init] view]];
Remember that UISearchDisplayController have a UITableView and that needs a UIViewController as a delegate. Just a UIView will not do.

It's confusing where to put init code

I have a UIView, for which I implemented the init method. This works for me because I always initialize my UIViews with the simple code below.
MYView *view = [[MYView alloc] init];
[self.view addSubview:view];
[view setFrame:CGRectMake(0,0,99,99)];
QUESTION: Can this view be used with storyboards? or do I need to implement other "init" methods? Also, what happens if someone instantiates the view with initWithFrame? Do I need to implement all the "init" methods to handle all these cases?
Storyboard and XIB files will use the initWithCoder: methods.

iOS uiview and uiviewcontroller

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.

Resources