Comparing two UITextFields - iphone

I have no idea why this isn't working and I feel it's something really very stupid.. I'm trying to compare two textfields (like a confirming the password type of thing) but it just isn't working!! Ive tried several different options and none of them work. I've tried:
if (passwordCheckField1.text == passwordCheckField2.text) {
[self performSegueWithIdentifier:#"segue4" sender:nil];
}
I've tried:
NSString *retrivedPasswordCheckField1 = passwordCheckField1.text;
NSString *retrivedPasswordCheckField2 = passwordCheckField2.text;
if ([retrivedPasswordCheckField1 isEqualToString:retrivedPasswordCheckField2]) {
[self performSegueWithIdentifier:#"segue4" sender:nil];
}
I've tried:
if ([passwordCheckField1.text isEqualToString:retrivedPasswordCheckField2.text]) {
[self performSegueWithIdentifier:#"segue4" sender:nil];
}
I've implement the <UITextFieldDelegate> in the .h and i've also set the delegates for both fields, (passwordCheckField1.delegate = self;
passwordCheckField2.delegate = self;)
What am I missing???? Thanks for your help!
EDIT
I probably should mention that I'm using a Navigation Controller, with several child view controllers. Each contains a text field, so when the view controller about the first password comes up, the user enters it and then clicks next taking them to the confirm password view controller. All are linked to the same class files.

This should work.
NSString *password = passwordCheckField1.text;
NSString *confirmPassword = passwordCheckField2.text;
NSLog(#"password: '%#'", password);
NSLog(#"confirmPassword: '%#'", confirmPassword);
if([password isEqualToString: confirmPassword]) {
NSLog(#"Match");
} else {
NSLog(#"Not Match");
}

if (passwordCheckField1.text == passwordCheckField2.text) {
[self performSegueWithIdentifier:#"segue4" sender:nil];
}
The above code doesn't compare two string values. It actually compares two pointers value.So it is not going to be useful for you.
but code
if ([retrivedPasswordCheckField1 isEqualToString:retrivedPasswordCheckField2]) {
[self performSegueWithIdentifier:#"segue4" sender:nil];
}
should have compared the string values. May be string your values are not same or nil. Have you printed the passwordCheckField1.text and passwordCheckField2.text.What is the values for two strings ?

chk your values.
if ([passwordCheckField1.text isEqualToString:passwordCheckField2.text]) {
//Match it's working for me
}

try this
if([[passwordCheckField1 text] isEqualToString:[passwordCheckField2 text]])
{
[self performSegueWithIdentifier:#"segue4" sender:nil];
}
else
{
//not matched
}

As a precautionary measure, try triming both the password strings for leading and trailing spaces before comparing like
NSString *pwd1 = [pwdTxtField1.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]];

Related

ABUnknownPersonViewController crashes

Im using this code to show an ABUnknowPersonViewController for showing a record from a person which was created from a vCard String:
- (ABRecordRef)person {
if (person == NULL) {
ABPersonCreator *creator = [[ABPersonCreator alloc] initWithVcardString:vcardString];
person = creator.person;
CFRetain(person);
[creator release];
}
return person;
}
- (UIView *)fullscreenView {
unknownPersonController = [[ABUnknownPersonViewController alloc] init];
unknownPersonController.displayedPerson = self.person;
unknownPersonController.allowsAddingToAddressBook = YES;
unknownPersonController.allowsActions = YES;
unknownPersonController.unknownPersonViewDelegate = self;
return unknownPersonController.view;
}
Can someone help me out with this?
thx Philip
The solution to this problem for me was that I wasn't passing the right stuff when building the ABRecordRef. For instance, I tried to simply set a string to a property when it instead wanted a kABMultiStringPropertyType. The exception would only occur after trying to launch the UnknownPersonViewController.
Check your datatypes and make sure that you're building the right thing.
Shouldn't have unknownPersonViewController have an autorelease, as it's view's returned?

Three20 : how to pass a class of objects between 2 views

I have a TTableView. The items in this table a mapped to an url, so that when I click on an item, another view appear with informations about this item.
All these informations are attributes of a class. So, how can I build my TTableTextItem URL in order to transmit the class containing informations to the view responsible for the display of these informations ?
Thanks in advance.
One way of doing it is to use a TTURLAction. When the user selects a row in your table, which will call your didSelectObject (of TTTableViewController) method, extract the object or set of objects you want to pass and build a TTURLAction like this:
TTURLAction *action = [[[TTURLAction actionWithURLPath:#"tt://showUser"]
applyQuery:[NSDictionary dictionaryWithObject:user forKey:#"kParameterUser"]]
applyAnimated:YES];
Then open the action:
[[TTNavigator navigator] openURLAction:action];
The controller you want to open as a result of this action should be registered in your TTURLMap and should have a constructor thus:
- (id) initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
self = [super init];
if (self != nil) {
self.user = [query objectForKey:kParameterUser];
}
return self;
}
I tend to create categories on classes for objects I want to be able to open another controller and display themselves.
The big problem with directly using TTURLAction is that you can't really use them with TTTableViewItem. The only way to really do it is override -didSelectObject:atIndexPath: and build your custom TTURLAction with your desired object in the query dictionary. But this breaks the nice separation of Model and View Controller, and gets complicated once you have multiple objects to pass.
Instead of this, I've been using a small category which automatically takes the userInfo property of the table item (which I set to whatever I need), and automatically adds it as a URL parameter.
And then you use this to retrieve it in your mapped view controller.
- (id)initWithNavigatorURL:(NSURL *)URL query:(NSDictionary *)query {
if (self = [self initWithNibName:nil bundle:nil]) {
id myPassedObject = [query objectForKey:#"__userInfo__"];
// do the rest of your initlization
}
return self;
}
You can download it as a GitHub Gist here. The code is also below. We're considering merging this into the main branch at some point.
TTTableViewDelegate+URLAdditions.h
#interface TTTableViewDelegate(URLAdditions)
#end
TTTableViewDelegate+URLAdditions.m
#import "TTTableViewDelegate+URLAdditions.h"
#implementation TTTableViewDelegate(URLAdditions)
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
id<TTTableViewDataSource> dataSource = (id<TTTableViewDataSource>)tableView.dataSource;
id object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath];
// Added section to automatically wrap up any TTTableItem userInfo objects. If it is a dictionary, it gets sent directly
// If it is not, it is put in a dictionary and sent as they __userInfo__ key
if( [object isKindOfClass:[TTTableLinkedItem class]] ) {
TTTableLinkedItem* item = object;
if( item.URL && [_controller shouldOpenURL:item.URL] ) {
// If the TTTableItem has userInfo, wrap it up and send it along to the URL
if( item.userInfo ) {
NSDictionary *userInfoDict;
// If userInfo is a dictionary, pass it along else create a dictionary
if( [item.userInfo isKindOfClass:[NSDictionary class]] ) {
userInfoDict = item.userInfo;
} else {
userInfoDict = [NSDictionary dictionaryWithObject:item.userInfo forKey:#"__userInfo__"];
}
[[TTNavigator navigator] openURLAction:[[[TTURLAction actionWithURLPath:item.URL]
applyQuery:userInfoDict]
applyAnimated:YES]];
} else {
TTOpenURL( item.URL );
}
}
if( [object isKindOfClass:[TTTableButton class]] ) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
else if( [object isKindOfClass:[TTTableMoreButton class]] ) {
TTTableMoreButton* moreLink = (TTTableMoreButton*)object;
moreLink.isLoading = YES;
TTTableMoreButtonCell* cell
= (TTTableMoreButtonCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.animating = YES;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if( moreLink.model )
[moreLink.model load:TTURLRequestCachePolicyDefault more:YES];
else
[_controller.model load:TTURLRequestCachePolicyDefault more:YES];
}
}
[_controller didSelectObject:object atIndexPath:indexPath];
}
#end
I think the documentation on the official website does describe very clearly the navigation scheme for Three20. Your question is the very common task of any application, and Three20 provides powerful support for that.
I have little fix this awesome code :)
as posted version strip callback from TTTableButton.
Correction is:
if( [object isKindOfClass:[TTTableButton class]] ) {
if (item.delegate && item.selector) {
[item.delegate performSelector:item.selector withObject:object];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

iPhone SDK: Can I nab in instance variable from one view and access it in another?

I'm pretty new to OOP in general and just really started working with Obj-c a few months back. So, please be gentle! I appreciate your help in advance. Now to the question!
I have 3 Text Fields where a user inputs name, phone, and email.
I collect and place them in a label using an NSString like this [this is the one for the name]:
- (IBAction)changeGreeting:(id)sender {
self.name = textInput.text;
NSString *nameString = name;
if([nameString length] == 0) {
nameString = #"I Forgot";
}
NSString *greeting = [[NSString alloc]
initWithFormat:#"Hello, my name is %#! Really!", nameString];
label.text = greeting;
[greeting release];
}
With this I have been able to place the text from text.input into my label (as stated in label.text = greeting;)
I have another view where I'd like to have someone review this information (view a label too). I need to have access to name or Textinput.text in that other view.
How can I accomplish this?
If you don't need to communicate changes between the two view controllers, you may want to pass it in using a custom init method. This may be best for a confirmation screen, where the prompt would make no sense without this string.
- (id)initWithFrame:(CGRect)aRect username:(NSString*)aName {
if((self = [super initWithFrame:aRect])) {
_myName = [aName retain];
}
return self
}
Another option is to implement a method on the first view controller and call it from the second.
- (NSString*)enteredUsername {
return _myName;
}

Possible View Caching problem?

I'm building an iphone app and I've got a table view with some textfields inside the cells, the content of the fields is set in viewWillAppear (its a grouped TableView w/ 3 fields that are always the same). The content of the text fields is retrieved from getter methods that return values from various class variables.
The problem I'm having is the getter seems to be returning the original value, not the value that is modified by the setter method. The class variable is an NSMutableString. Is it possible the view is caching the method call?
//header file
#implementation ManageWorkoutViewController : UIViewController {
NSMutableString *workoutDifficulty;
}
-(void)setWorkoutDifficulty:(NSString *)value;
-(NSString *)getWorkoutDifficulty;
#end
//implementation file
-(NSString *)getWorkoutDifficulty {
if (nil == workoutDifficulty) {
workoutDifficulty = [NSMutableString stringWithString:#"Easy"];
}
NSLog(#"getter: Returning workoutDifficulty as: %#", workoutDifficulty);
return workoutDifficulty;
} //end getWorkoutDifficulty
-(void)setWorkoutDifficulty:(NSString *)value {
workoutDifficulty = [NSString stringWithFormat:#"%d", value];
NSLog(#"setter: workoutDifficulty set as: %#", workoutDifficulty);
}//end setWorkoutDifficulty
//elsewhere in the implementation another table view is
//pushed onto the nav controller to allow the user to pick
//the difficulty. The initial value comes from the getter
workoutDifficultyController.title = #"Workout Difficulty";
[workoutDifficultyController setOriginalDifficulty:[self getWorkoutDifficulty]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[(UINavigationController *)self.parentViewController pushViewController:workoutDifficultyController
animated:YES];
//then in that workoutDifficultyController it calls back into the first controller to set the selected value:
[manageWorkoutController setWorkoutDifficulty:selectedDifficulty];
You've got many issues here. First, you're creating your accessors incorrectly. The problem that's particularly causing you trouble is this line:
workoutDifficulty = [NSString stringWithFormat:#"%d", value];
value is an NSString here. You should be receiving a warning about this. I believe "Typecheck Calls to printf/scanf" is turned on by default, and should catch this. workoutDifficulty is being set to some random number (probably taken from the first 4 bytes of value).
Here is what you probably meant. I would probably switch workoutDifficulty to an enum, but I'm keeping it an NSString for consistency with your code. I'm also doing this without properties because you did, but I would use a property here.
//header file
#implementation ManageWorkoutViewController : UIViewController {
NSString *_workoutDifficulty;
}
-(void)setWorkoutDifficulty:(NSString *)value;
-(NSString *)workoutDifficulty; // NOTE: Name change. "getWorkoutDifficulty" is incorrect.
#end
//implementation file
-(NSString *)workoutDifficulty {
if (nil == workoutDifficulty) {
_workoutDifficulty = [#"Easy" retain];
}
NSLog(#"getter: Returning workoutDifficulty as: %#", _workoutDifficulty);
return _workoutDifficulty;
} //end workoutDifficulty
-(void)setWorkoutDifficulty:(NSString *)value {
[value retain];
[_workoutDifficulty release];
_workoutDifficulty = value;
NSLog(#"setter: workoutDifficulty set as: %#", _workoutDifficulty);
}//end setWorkoutDifficulty
You have to retain workoutDifficulty whenever you set it to a new value (and release the old value).

UITextField text value returns garbage

I am trying to get a string value out of a textField when the user dismisses the keyboard. However, it seems that whenever I try to get the value, I get garbage (attempting to print out textField.text gives out garbage). What could I be doing wrong?
(The control displays fine, and I can put text values into it even).
Here's my code:
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
NSInteger currenttag = textField.tag;
NSLog(#"%d",textField.tag);
if (currenttag == 0) {
NSLog(#"%x %s",(unsigned int)textField.text,textField.text);
username = textField.text;
} else if (currenttag == 1) {
password = textField.text;
}
[textField resignFirstResponder];
return YES;
}
The fields username and passwords are nil NSString*'s, but since I will merely hold on to the NSStrings held by textField.text, it should be fine.
NSLog(#"text field text:%#",textField.text);
Have you tried using breakpoints? Have you tried NSLog(#"%#", textField.text); ?
Have you tried rewriting the function so it only displays the text?
Is the textField a valid object?
Inserting [textField retain]; as the 1st line will probably fix the problem. Just remember to add a [textField release]; at the end of the method.