having trouble to assign multiple picker views to a single view controller - uipickerviewcontroller

(void)viewDidLoad {
[super viewDidLoad];
picker1Data=#[#1,#2,#3,#4,#5];
picker2Data=#[#"a",#"b",#"c",#"d",#"e",#"f"];
self.picker1.dataSource=self;
self.picker1.delegate=self;
self.picker2.dataSource=self;
self.picker2.delegate=self;
Am i doing wrong by assigning single view controller as the datasource and delegate of multiple pickers?

No you can assign multiple UIPicker View in signgle ViewContoller

Related

How to make a multiple view app with a class linked to each view

Apologies if this has been replied already, I've been searching and I quiet cannot find what I am looking for.
I am trying to make a multiple view application (based on the single view template) and I want to have an object linked to each of the views.
So my intention is to have the same I have coming from the single view template: 1 view on the storyboard which is linked to the ViewController class.
I found some tutorials by using Navigation Controllers, Page View controllers, etc... but I don't want to use that functionality, I prefer to create my own buttons and link them to those pages.
I have a few questions, if you can help:
1 - Should I have an array of view objects?
I've seen an old tutorial where the guy creates a new view object when a button is pressed and then set the view as modal (which leads to the other class) and afterwards it destroys it immediately (I guess when the code come back once the modal view is closed).
In my case I would like to have an array of views and initialize all of them at the beginning of the app and just swap from one to the other depending on the user actions.
Which one is a better approach?
2 - How I create the class linked to the other view?
Can I create a normal class and make it subclass of UIViewController, is that enough or I need something else?
3 - Once I have the class created, how can I 'link' it to a view on the storyboard?
Basically I would like to have the outlets of each view in its correspondent class (and actions, etc...).
I am not sure if what I am trying to do is actually the correct way, any comments or help is more than appreciated :)
EDIT: I´ve managed to create a class, link it to a view of the storyboard and lunch it when the user press a button, but the new view appears completely black - any ideas?
Here is my code:
TestView.h:
#import <UIKit/UIKit.h>
#interface TestView : UIViewController
#end
TestView.m:
#import "TestView.h"
#interface TestView ()
#end
#implementation TestView
- (id) init
{
self = [super init];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#end
Then in the storyboard, I dragged in a new View, and in the "Identity Inspector" in the field "Class" I selected the TestView.
The in the file ViewController (the one created by default by the single view wizard), I added an action when a button is pressed:
- (IBAction)goToView1:(id)sender
{
[self presentViewController:view1 animated:YES completion:nil];
}
Also in the ViewController, function 'ViewDidLoad' I initialize the TestView class:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
view1 = [[TestView alloc] init];
}
So when I pressed the button it seems is doing something right or attempting to move to a different view, but the whole screen appears black - Am I missing something?
Thanks a lot in advance!!

How to create simple table view controller in code only

I need the same type of table view controllers in my app many times and would like to create a more generic table view controller which I can use over and over again.
These table view controllers are quite simple and show only the contents of an array, put a check mark to the selected table view cell and return the index of the selected table view cell to the calling view controller after the Done button in the toolbar has been tapped.
Currently I create each one of these table view controllers directly in Storyboard and instantiate them by using segues.
Would it be possible to do this in code only (without using Storyboard or xibs)?
What would be the best way to instantiate and push them onto the navigation controller stack (each one will be shown in a view controller).
It's trivial to do this in code. You create your view controller class just like you normally would (extend UITableViewController). Implement all of the same table view data source and delegate methods. All of that is the same.
When you want to use the table view controller you just do:
MyTableViewController *vc = [[MyTableViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
I would define your MyTableViewController init method like this:
- (id)init {
if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
// any other initialization
}
return self;
}
BTW - I have an app with over 100 view controllers in it and I've never used Interface builder or storyboards. It's all code.
Would it be possible to do this in code only (without using Storyboard or xibs)?
Yes. Anything you can do in a storyboard or .xib file, you can do in code:
MyViewController *vc = [[MyViewController alloc] initWithNibName:nil bundle:nil];
Note: the default behavior for a view controller is to load its view from a .xib with the same name as the view controller's class when you pass nil for the .xib name, e.g. MyViewController.xib for the example above. So the line above creates the view controller in code, but will still load the view from the .xib. If you want the view created programmatically as well, override -loadView.

Show different view on navigation controller when data is empty using storyboard

I have a navigation controller with a tableview controller as the first controller, however when the database is empty I want to show the user a view that asks the user for some information to fill the database and show it in the navigation controller instead of the view assigned in storyboard, I guess I would have to do this programmatically, but I'm new to iphone development and honestly dont know where to start.
Here's how you can do it. Create a new scene in your storyboard for your view controller where the user will submit the info (let's say it's called CollectInfoViewController). Then create a modal segue from your tableview controller to the CollectInfoViewController. In the Attributes Inspector, set the Identifier property of the segue to "CollectInfo" so you can identify it later.
Then when your tableview controller's viewDidLoad method runs, ask the table view delegate whether there are any rows to show. If there are not, programmatically perform the segue to your CollectInfoViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self.tableView.dataSource tableView:self.tableView numberOfRowsInSection:0] == 0) {
[self performSegueWithIdentifier:#"CollectInfo" sender:self];
}
}
In your CollectInfoViewController, once the user has entered the information and you have stored it in your database then you can call the following from within CollectInfoViewController to dismiss that view. This will return the user to the tableview.
[self dismissModalViewControllerAnimated:YES];
Finally, back in your tableview controller, you might need to reload your table with the new data that's been collected. You can do that in viewWillAppear, which will be called when the modal view controller is dismissed.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}

UITableView delegate not called when UIViewController is popped

I have a view controller that contains a UITableView. When the view controller gets pushed onto the stack, the table view delegate methods get called and it populates the table.
Then I push another view controller onto the one that contains the table view. What I would like to make happen is that - when the second view controller gets popped and I return to the previous view controller, those uitableview delegate methods get called again (so as to repopulate the table).
In other words, how do you repopulate a uitableview when popping to a view controller.
Thanks
In viewDidLoad or viewDidUnload, viewWillAppear, viewWillDisappear (whichever of these is right for your situation) you can put [myTable reloadData];:
// If you can include some code it would help as I am a bit uncertain
// about exactly what you are trying to do from the question but
// you should use whichever of these is correct for project:
- (void)viewDidLoad {
[super viewDidLoad];
[myTable reloadData];
}
- (void)viewDidUnload {
[myTable reloadData];
}
- (void)viewWillAppear {
[super viewWillAppear];
[myTable reloadData];
}
- (void)viewWillDisappear {
[super viewWillDisappear];
[myTable reloadData];
}
What you should do is add a [self.tableView reloadData] (or whatever your table variable is) call inside the viewWillAppear method of its view controller. This will cause the table view to be reloaded both when being pushed (as it does now) and when other view controllers are popped to reveal it.

UISearchBar Not appearing

I have view controller, into the view i have put a table view, and a search bar into the table's header... the search bar is not showing up, just the empty table view.
Do i need to do something additional? I'm pretty sure its to do with the view outlet of the UIViewController, set to View...
Thanks
For anyone else who may be landing on this question, I had a very similar situation where a UITableViewController with a UISearchBar added to it wasn't displaying. If you find yourself in this situation, double check that you are actually calling:
initWithNibName:#"MyNibName" bundle:nil
to init your view controller instead of the common Table View init of:
initWithStyle:UITableViewStylePlain
I was foolishly adding the search bar to the Nib, and then loading it with the style init (which skips the Nib entirely and loads the table view from scratch)
try setting the tableHeaderView of your tableView to your searchBar.
self.tableView.tableHeaderView = searchBar;
if you are using IB, be sure to connect the outlets so that it gets referenced.
Try this,l it worked for me:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.tableView.tableHeaderView = _searchBar;
[_searchBar becomeFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[_searchBar resignFirstResponder];
self.tableView.tableHeaderView = _searchBar;
}
-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
searchBar.frame = CGRectMake(0,MAX(0,scrollView.contentOffset.y),320,44);
}
If you set your view controller as a part of tabbar controller, besides "Class" property in IB set the "NIB name" property. It takes me several hours to figure it out.
Fixed using a UISearchBar with controller.