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.
Related
I have a controller named as "firstViewcontroller" where i have a UITableView named as "discoveredInstanceTableView". I want to load that UITableView in to another UIViewController named as "secondViewcontroller"
I have used the below code but it is not working, It says property "discoveredInstanceTableView" not found ...Anybody please help me:
In the firstViewcontroller:
IBOutlet UITableView *discoveredInstanceTableView;
In the Secondviewcontroller:
firstViewcontroller *vc1 = [[firstViewcontroller alloc]initWithNibName:#"firstViewcontroller" bundle:nil];
[self addChildViewController:vc1];
[self.myTableview addSubview:vc1.discoveredInstanceTableView];
What you asked is valid only if you curious to know why the above thing is not working answer would be
You are doing something that is not allowed, this can not be done as per the documentation.
However, If we forget about the right wrong approach, you probably adding a table view as a subview over a table view itself and I am sure you passing a table view to a table view which might not be allocated.
First think about the UITableView how it works? it simply a ScrollableView which display content over its cells.
Eventually would recommend you read about TableView
EDIT: From the Above Comments
IMPORTANT: You should not embed UIWebView or UITableView objects in
UIScrollView objects. If you do so, unexpected behavior can result
because touch events for the two objects can be mixed up and wrongly
handled.ยป As UITableView is a UIScrollView, this applies here as well.
Possible Alternatives of displaying TableView inside the SecondViewController
Use #Rajath Kornaya's Answer And In my opinion that is not right approach since whenever you required callback action like on cell tap, you want to display an alert(or something else), you can't get the delegate callback inside the SecondViewController
But there are so many other right approaches available, that you should follow up.
Create a TableView separately either programmatically or through the XIB/Storyboard
Add delegate and data source (methods which responds when something interesting happened e.g Cell going to populate called cellForRowAtIndexPath) to current SecondViewController
Define all required data source methods and write proper code.
If you required to do something on cell tap, add specific delegate method too.
But if you want to reuse the FirstViewController Class TableView simply create a CustomView and add TableView inside there and simply add that view to each view controller class.
I hope it may helps you!!!
declare in viewcontroller2
#property (nonatomic, strong) UITableView *table;
create table in viewcontroller1
tableView=[[UITableView alloc]initWithFrame:CGRectMake(10, 10, 250, 300) style:UITableViewStylePlain];
[self.view addSubview:tableView];
tableView.backgroundColor=[UIColor greenColor];
while calling viewcontroller2 pass table to viewcontroller2
ViewController2 *v2=[[ViewController2 alloc]init];
v2.table=tableView;
UINavigationController *navigation=[[UINavigationController alloc]initWithRootViewController:v2];
[self presentViewController:navigation animated:YES completion:nil];
in viewcontroller2 access the table using the global variable
[self.view addSubview:self.table];
I have a UIView controller and a UIView called GCView.
If my code is just this :
self.view = [[GCView alloc] init];
I can see my custom view.
When I drag and drop a UIView in the storyboard, assign it to a property called customView
and use that code:
self.customView = [[GCView alloc] init];
nothing is shown.
I want to use the second approach, because it is more convenient to have some stuff inside the UIView created and handled dynamically, and the others to be statically inside the storyboard and handled in the UIViewController.
Make sure your view object in the story board is of class GCView. This will call initWithCoder: in your GCView class when the view loads. Once you have that, remove this:
self.customView = [[GCView alloc] init];
as the object will have already been initialized by the storyboard loading. Make sure all of your initialization calls in the GCView class are in initWithCoder: and not init.
I would like to use my viewDidLoad function in my tableViewController.
How can I make viewDidLoad run in my controller?
tableViewController = [[TableViewController alloc] init];
UITableView *tableView = [[UITableView alloc] init];
tableViewController.view = tableView;
....
From Apple documentation:
This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.
So you can try to instantiate it from NIB or overwrite the loadView method. Another step from Apple documentation:
If you cannot define your views in a storyboard or a nib file, override the loadView method to manually instantiate a view hierarchy and assign it to the view property.
viewDidLoad will be called when the view is actually loaded, which will happen after you present your view controller, by, e.g.:
adding it to a navigation controller,
adding it to a tab bar controller,
presenting it modally.
This is the missing bit in your code. If you explain how you would like to present your view controller, I may help further. Also, have a look at this: Presenting View Controllers.
(I assume the fact that you tried to override the view property of your table view controller was just an attempt "to make things work" -- but you do not need to do anything about that, the view controller will be correctly set up with a table view inside of it).
tableViewController = [[TableViewController alloc] init];
tableViewController.tableView // This is your newly generated tableview
viewDidLoad will be called after you assign the tableView to another parentview
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.
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