One IBOutlet for two UITextFields - iphone

I have two UITextField one for Portrait and other for Lanscape View but I want that editing the first should reflect the changes to other text field too. I have to use separate views for both landscape and portrait. My last option is to make two outlets for them but I want to use only a single one.. Any help would be appreciated.

One option is to make use of IBOutletCollection. You can have one NSArray which you declare as an IBOutletCollection (instead of IBOutlet). In order to know what text field you are getting out of the array, you can set a tag on each one in IB and just pull from the array the text fields that match specific tags.
There is another workaround. You store the values from the text fields before you use the nib for the changed orientation like
NSString *firstText = firstTextField.text;
NSString *secondText = secondTextField.text;
then after exchanging the nibs use this to populate the text fields in the new nib like
firstTextField.text = firstText;
secondTextField.text = secondText;

You might implement appropriate method(s) in UITextFieldDelegate.....
Such as
//not real code here just off the top of my head
(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSString* updatedText = [textField text];
firstTextField.text = updatedText;
secondTextField.text = updatedText;
return YES;
}

u can use 2 diiferent xib files and do the same thing
To load a new xib file use the following code
[[NSBundle mainBundle] loadNibNamed:#"tests" owner:self options:nil];
u can still use the existing xib file by duplicating it and and loading it with the above code when the orientation changes.
There wont be much changes in the code as u can use the same iboutlets in both the orientations without even requiring to manually link them

Related

Access UITextField with tag on a static Table

So I have a static TableView setup in the interface builder with multiple sections, labels, and text fields.
I'd like to be able to something along the lines of
[self.view viewWithTag:kNameTextfield]
and get the UITextField with that tag that I've set.
For some reason I cannot access the UITextField and it comes up Nil. I imagine because it's somewhat deeply nested? I thought about creating an Outlet collection of all the text fields and other than iterating through them every single time I need to change something seems like a waste?
What I'm trying to do is on initial load, it populates the values of the textfield by values in a dictionary. So it's useful to be able to target a specific textfield.
Try iterating through the subviews:
for (UIView* viewsLevel1 in [cell.contentView subviews])
{
for (UIView* viewsLevel2 in viewsLevel1)
{
UITextField *textField = [viewsLevel2 viewWithTag:kNameTextField];
}
}
Go as far deep as you need to go

How to pass a value from one view to another view

I have two views of two subpages. Here I want to pass the value from one sub class of view to another. I searched the web, but can't find a clear answer. Can anybody provide an example for passing a UITextField value from one to another?
You can maintain a common string in appDelegate.
Define a string
NSString *commString;
in appDelegate.
And then when you submit the uitextfield value just assign that text field value to the
appDelegate.commString
use this in the next view.
ProjectNameAppDelegate *appDelegate = [[UIApplication sharedApplicatoin] delegate];
appDelegate.commString = textField.text;
This will work for sure.

UITableView don't reuse cells, good case to do this?

I have 5 cells in a UITableView. Each has a UITextField as a subview, where the user will input data. If I DO use cell reuse, the textfield gets cleared if the cells are scrolled out of view. I don't want to have to deal with this. Is there a way to NOT reuse cells so that I don't have this issue, if so, how?
Is this a bad idea?
I have same feature in one of my apps and I used below code to accomplish that and I never had this kind of problem.
First of all you need to store all your textField value temporary in Array. Make array like this.
arrTemp=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:#""],
[NSString stringWithFormat:#""],
[NSString stringWithFormat:#""],
[NSString stringWithFormat:#""],
[NSString stringWithFormat:#""],
[NSString stringWithFormat:#""],
[NSString stringWithFormat:#""],
[NSString stringWithFormat:#""],
[NSString stringWithFormat:#""],nil];
Then Give all textField tag = indexPath.row;
After that You need to replace textField value in below two methods.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}
At Last You need to set that value in cellForRowAtIndexPath datasource Method. So that whenever user scroll tableview it set previous value from temp array. Like this.
cell.txtEntry.text = [arrTemp objectAtIndex:indexPath.row];
It might possible I forgot some of the code to paste here. So if you have any problem please let me know.
You can give each cell a unique ReuseIdentifier, maybe by appending the indexPath.row to the name. If you have only 5 cells, this will probably be fine, but you're losing one of the main benefits of a UITableView. In this case, you may want to use a UIScrollView instead.
I would say 5 textview's is a perfect case for not queueing and de-queueing the cells, just create them all in view did load, store in an array and return as requested.
If you open up Apple's Recipes sample application, you will see how Apple uses a xib file to load UITableViewCells.
In the IngredientDetailViewController file:
#property (nonatomic, assign) IBOutlet EditingTableViewCell *editingTableViewCell;
// ...
[[NSBundle mainBundle] loadNibNamed:#"EditingTableViewCell" owner:self options:nil];
// this will cause the IBOutlet to be connected, and you can now use self.editingTableViewCell
Although it looks like they are reusing the cell, you could use the same method to load the 5 cells into 5 separate IBOutlets, and then in cellForRowAtIndexPath, you just return those 5 rather than calling the dequeue method.
Note: you will probably need to store the cells as strong properties (rather than assigning them).

maintaining selection in UISegmentedControl

Can i maintain the selected state of UISegmentViewControl segments??
i.e can keep a segment appear selected even if the user selects another segment??
I dont seem to find anything that does this anywhere!!
This isn't possible out of the box. (See How can I enable multiple segments of a UISegmentedControl to be selected?.)
You could try something like this code to provide similar functionality.
I found a way arround this.I placed dark colored image behind each segment and set their hidden property to true.Then i decresed the alpha value of the uisegmented control.Then in the code when a segment is clicked i turn the visibility on or off accordingly,so multiple segments appear selected
Another solution might be using a category:
#import <UIKit/UISegmentedControl.h>
#interface UISegmentedControl (MultiSelect)
#end
Doing this, you have in principle access to private member variables of UISegmentedControl . In particular, you have access to the array holding the button segments, which you can manipulate according to your needs by overriding setSelectedSegmentIndex:selectedSegmentIndex: .However, for various reasons, the attributes declared as private still shouldn't be accessed directly, see this link. As also suggested there, you can rather abuse KVC. So the implementation could look as follows:
#implementation UISegmentedControl (MultiSelect)
- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
NSMutableArray *pArraySegments = [self valueForKey:#"segments"];
if ((pArraySegments) && (selectedSegmentIndex >= 0) && (selectedSegmentIndex < [pArraySegments count])) {
UIButton *pSegment = (UIButton*)[pArraySegments objectAtIndex:selectedSegmentIndex];
pSegment.selected ? (pSegment.selected = NO) : (pSegment.selected = YES);
}
}
#end
This works for me. However, since I now read this discussion, I'm not quite sure if this is really a valid approach.

how can I copy a string displayed in a UITableViewCell into another NSString?

I am writing a program, and one part of my program has a UItableView in which I set cell contents manually by using the following...
cell.textLabel.text = #"CELL CONTENTS HERE!";
How can I copy a string displayed in a particular TableView Cell into another NSString?
Try something like this:
NSString *anotherString = [NSString stringWithString:cell.textLabel.text];
Keep your #"CELL CONTENTS HERE!" string as a #property of your view controller. Then set the cell's text property to it:
cell.textLabel.text = cellContentsHereProperty;
inside the table view delegate method -tableView:cellForRowAtIndexPath:. You want to do this because you can only access cell.textLabel.text inside this method, or by calling the delegate method to retrieve the cell, which is awkward.
As a general concept, you want your view controller ("control") to keep the string value ("model") separate from how it is displayed ("view"). Keeping things compartmentalized lets you retrieve and change the data without worrying about how it is displayed.
This separation of responsibilities is called the MVC or Model-View-Control design pattern, which Apple subscribes to for iPhone application design.