Putting two picker control on single UIView - iphone

I have picker controls on a single view with two outlets for each. When any of them are selected the same method is called. How should I distinguish between which picker control has called the delegate?
How can I change the frame size of the pickers to fit in one view?
Thanks,
Amit

Set the view to a UILable and set the font size or adjustsFontSizeToFitWidth:
UILabel *myView;
myView = [[[UILabel alloc] initWithFrame:myFrameRect] autorelease];
myView.adjustsFontSizeToFitWidth = YES;
return myView;

Two pickers in a view sounds crazy ugly. Would it really not work for you to have a picker with two components (ie, columns)?
Nevertheless, the first parameter in every method defined by UIPickerViewDelegate is the picker that called you. If you have wired an IBOutlet to each UIPickerView, then you should be able to compare this parameter to those outlets to figure out which one has called you. Something like this:
IBOutlet UIPickerView *fooPicker;
IBOutlet UIPickerView *barPicker;
// ...
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (pickerView == fooPicker)
return [NSString stringWithFormat #"Foo row %d", row];
else
return [NSString stringWithFormat #"Bar row %d", row];
}
It might be cleaner to connect to different objects as your delegates, but with two pickers in one view, "clean" probably isn't an option for you anyways.

I've had great success with putting two pickers right on top of one another and then using a UISegmentedControl to select which is visible, by setting the .hidden properties of each.

Instead of having two picker, you can make it as one.
You can have the picker controller in a view . Once the user touch a button rise up the view and make them to select from the picker and remove the view.This will reduce the space in the Main viewcontroller.

Related

UITableView in customCell hiding one button also hide the next button of other cells

e.g as you are watching the tableView having cell with plus Button, when I press that button it hides the current button but when I scroll the tableview Some other button on other customCells are also get hide but I didn't hide them. please help me out. How to fit this problem.
The UITableView is caching the cells, which means it doesn't store all cells at all time, which is brilliant in terms of memory. The problem with it, that it has no reference to the value stored in that cell when the cell is reused. What is often done, is that all values stored in a dynamic cell is stored in separate NSMutableArrays.
For your problem, you could add an array with the boolean values indicating whether they are hidden or not, and read from that in the tableView:cellForRowAtIndexPath: delegate method with
cell.hidden = [[self.yourArray objectAtIndex:indexPath.row] boolValue];
And in the button callback method you should change the hiddenproperty as well as updating the value in the array.
i'll do it as follow:
first you have to track the button's state:
Shown
or
Hidden
this is done by holding the state in an NSMutableArray
in the viewDidLoad method add the following
NSMutableArray *shownButtons = [[NSMutableArray alloc] init];
then in your tableView:cellForRowAtIndexPath do the following
NSString *tmpIndexPathString = [NSString stringWithFormat:#"%d",indexPath.row];
if ([shownButtons containsObject:tmpIndexPathString])
{
[cell.myButton setHidden:YES];
}
else
{
[cell.myButton setHidden:NO];
}
In the tableView:cellForRowAtIndexPath: delegate method you need to loop through all the visible cells:
for (UITableViewCell *cell in [self.tableView visibleCells]) {
// now you have a cell that you can update
}
You also have to remember to update your data source so scrolling the table will update the cells accordingly and not show the plus button. If you're updating your data source what you can do is reload the cell in the above for loop for example.

image view with table view in the same view controller

I have a new controller defined as follows:
#interface view1: UITableViewController
I've created (in viewDidLoad) an image view (logo image) and added this image view as subview for view1, but the problem is that table view cells still appear behind this image view, how can completely separate the image view from the table view ?
thanks in advance.
To have a logo type view you either need to set a custom headerview for the tableview via
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
and
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
the other method would be overriding -loadView and creating your own view that has two subviews, your imageview and a tableview.
In the first method, once your scroll some the logo will eventually disappear. The second method makes the logo static.
Try adding it in:
- (void)viewDidAppear:(BOOL)animated
This method is only called once you have called viewDidLoad so if you want something over everything else you might call this one or add the subview to:
[[[UIApplication sharedApplication] keyWindow] addSubview:yourView];
Hopefully it helps you.
2 options:
1.create a UIViewController to hold your UITableViewController controller view and your imageView, then position their frame so they wont overlap
2.add the imageView as a TableView Section Header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImageView* imageView = [[UIImageView alloc] initWithImage:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"]]];
return imageView;
}
and make sure you have at least 1 section of course in:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
What is seems like you want is for your logo to be at the top, above your table view? If so then you can, in -viewDidLoad, set the tableView's tableHeaderView to the view you want, e.g.:
tableView.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image"]]; // assuming ARC, else autorelease or put in variable first...
If you want it to float on top when scrolling then DanZimm's use of -tableView:viewForHeaderInSection: is what you want.

self.tableView insertRowsAtIndexPaths from within tableView delegate

So I thought I'd have a go at building my own simple app. Please go easy on me I'm new to all this! The idea is this. For iPad have a single view controller with a text box and a text field. Text box takes a title, and text field takes the body of a report. There's a button on the page to submit the report, which bundles the two texts into an object and adds it to a table view within the same view controller. I have set the view controller as a delegate with <UITableViewDelegate, UITableViewDataSource> in my header file. My table view works fine for adding items in the viewDidLoad method. But adding items from the text inputs via a UIButton connected to -(IBAction) addItem falls over with: Property 'tableView' not found on object of type 'ReportsViewController'
- (IBAction)addReportItem
{
int newRowIndex = [reports count];
ReportObject *item = [[ReportObject alloc] init];
item.title = #"A new title";
item.reportText = #"A new text";
[reports addObject:item];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
I understand that I'm trying to call a method within my object but I have other method calls to tableView which work fine. i.e.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [reports count];
}
I thought this was the point of delegation. I know I'm missing something, but as I say I am new to all this and have looked everywhere for an answer before posting. What do I need to do to send my IBAction message to tableView?
Do you have a tableView instance variable setup in your .h file of the view controller?
The reason you are able to access it in the delegate and data source methods is because they are passed in as part if the methods.
You will need to add the IBOUTLET tableView ivar and connect it to the tableView in your .xib.
Or perhaps your ivar for the tableView is named something else?
Good luck.
I had the same problem.
What helped was to inherit the View Controller from UITableViewController, instead of UIViewController. Not using the protocol names in angled brackets.
The TableView is then linked to the dataSource and delegate via the storyboard (resp. InterfaceBuilder).
The parent class UITableViewController has an IBOutlet tableView defined.
MyViewController.h:
#interface MyViewController : UITableViewController

How to pass values from one page to other page and display values in the page's textlabel of cell in iPhone?

I have an application in which when I click on the table item a new page opens, which allows me to enter details in textfield and textview, and clicking on the submit button it should navigate to the previous page which is a tableview and populate the the textlabel of the cell of the table view with the value which are entered in the textfield and textview.
How is this possible? How could I populate the tableview cell with the values entered in the textfield and textview?
Everything depends on how the data are stored in your TableView. For example if you have a plist file you must re-write this file when you submit new value.
After that, on :
- (void)viewWillAppear:(BOOL)animated {}
You must reload plist file and invok[self.tableView reloadData];
However there is an other method. If you have just few values you can use NSUserDefault class Reference to store data in your app.
You Need to use following delegate function:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
objInf.nameLable =[NSString stringWithFormat:#"Hello %# Have a Good Day :)",[listData objectAtIndex:indexPath.row]];
NSLog(#"Next View Value =%#",[listData objectAtIndex:indexPath.row]);
NSLog(#"Next View Value =%#",objInf.nameLable);
[self presentModalViewController:objInf animated:YES];
}
As
objInf is second class's object and nameLable is NSString which you can set as text of label of your second class where u want to display the data..
By using property you can do this. string properties of textfield strings on input page then simply by making object of that page you can access these property.
remember you need singletone object if you make new object then it reintialise the properties.
so use this for making object of that input class.
YourClass *obj= (YourClass *)[self.navigationController.viewControllers objectAtIndex: [self.navigationController.viewControllers count]-2];
this gives object from navigation stack.
That can be achieved by 2 ways.
One is, You can have 2 NSString property in another ViewController. 1 Method to assign data in that. like
#property (nonatomic,retain) NSString *textfieldValue; and Synthesize it.
#property (nonatomic,retain) NSString *textviewValue; and Synthesize it.
-(void) contentToDisplay: (NSString *)pTxtFieldValue txtViewValue(NSString*)pTxtViewValue{
self.textfieldValue = pTxtFieldValue;
self.textviewValue = pTxtViewValue;
}
While switching to another ViewController by submitting use its object to assign text like
[objViewController contentToDisplay:#"textfieldvalue" txtViewValue:#"textviewValue"];
Hope it helps.

UITextField Delegate in UITableView

G'day guys, I'm building an application that allows users to rapidly input numbers into a UITableView.
I've currently built the framework, but I'm having a bit of a hitch hooking up the view to have the keyboard load and make the text in the cell editable from a user action.
I remember there being an iOS example in the dev code somewhere, (like a year ago so it wouldn't be under NDA) where you added items in and edited them within the context of the UITableView without going to a detailed subview.
Just need a hint on how to hook up the delegates, or how to structure the code.
I have code where I create custom cells for a UITableView which have UITextField and UITextView controls on them. In the UITableViewController code I do this:
Interface
#interface MyTableViewController:
UITableViewController <UITextFieldDelegate, UITextViewDelegate> {
....
}
Implementation
-(UITableViewCell *) tableView:(UItableView *) tableView
cellForRowAtIndexPath: (NSIndexPath *) indexPath {
....
UITableViewCell * cell = ....
cell.myTextField.delegate = self;
cell.myTextField.tag = 1; //This should be unique.
return cell;
}
-(void) textFieldDidEndEditing: (UITextField * ) textField {
// Decide which text field based on it's tag and save data to the model.
}
-(void) textViewDidEndEditing: (UITextView * ) textView {
// Decide which text view based on it's tag and save data to the model.
}
You can add a UITextField or UITextView to any cell. If you have custom cells, make them a delegate for their text view, or if you compose the cells in your table view delegate, then make this the text fields' delegates.