Comparing two strings in objective-c causes app to quit - iphone

I am attempting to compare two strings using "isEqualToString" and i cant seem to get it to work. Once it begins to run the if statement it quits the app. I'm quite sure that the strings are in fact equal to each other and despite my best attempts I'm out of ideas. Any help would be appreciated.
-(void)createTextfields{
name = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 140, 25)];
name.borderStyle = UITextBorderStyleRoundedRect;
name.textColor = [UIColor blackColor];
name.placeholder = #"Password";
name.textAlignment = UITextAlignmentCenter;
[self addSubview:name];
entry = [[UITextField alloc] initWithFrame:CGRectMake(170, 0, 140, 25)];
entry.borderStyle = UITextBorderStyleLine;
entry.textColor = [UIColor blueColor];
entry.textAlignment = UITextAlignmentCenter;
[self addSubview:entry];
}
-(void)createSubmitButton{
UIButton *submit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
submit.titleLabel.textAlignment = UITextAlignmentCenter;
[submit setTitle:#"Submit" forState:UIControlStateNormal];
submit.frame = CGRectMake(90, 50, 60, 30);
[submit addTarget:self action:#selector(runSubmit) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:submit];
UIButton *savePass = [UIButton buttonWithType:UIButtonTypeRoundedRect];
savePass.titleLabel.textAlignment = UITextAlignmentCenter;
[savePass setTitle:#"Save" forState:UIControlStateNormal];
savePass.frame = CGRectMake(20, 50, 60, 30);
[savePass addTarget:self action:#selector(save) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:savePass];
}
-(void)save{
tempPass = name.text;
name.text = #"";
entry.text = tempPass;
}
-(void)runSubmit{
// password = name.text;
if ([tempPass isEqualToString:name.text]) {
//[viewController displayAlertFromViewControl];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Correct" message:#"Alert" delegate: self cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
}

Probably one of your strings (pretty sure that it is tempPass) is autoreleased

Where is tempPass being declared? Is it possible that it isn't being retained long enough to be referenced in the if?

If you run your program with this menu item: Run -> Run with Performance Tool -> Zombies
then Instruments will do all the hard work of tracking down the problem for you. It will show you which object was released early and where it was allocated.
It is probably tempPass. Once you determine if that is the case, it would be best to make tempPass a property with a copy attribute and then use self.tempPass=name.text instead of just tempPass=name.text.

Related

MapView annotations change their layer order when touched

Small question, i've got a few annotations on a map view. Sometimes they tend to overlap when they are very close to each other and that's ok, but when i touch them they switch their layer position. Those that were in the back are now in the front and vice versa.
So i checked my didSelectAnnotation function but there is literarily no code in that function so it couldn't be it.
I couldn't think of anything else that might cause it.
Do you know how to fix it so it won't change layer position??
This is my viewForAnnotation code:
annImg = [[szAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
szPointAnnotation *pointAnnotation = annotation;
if (appDelegate.homePage || ([pointAnnotation.tag intValue] >= 10000))
{
annImg.canShowCallout = NO;
annImg.backgroundColor = [UIColor clearColor];
annImg.frame = CGRectMake(0, /*0*/-78, 70, /*78*/156);
UIImageView *currentUserPictureView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 60, 60)];
int newTag = [pointAnnotation.tag intValue]-10000;
UIImage *currentUserPicture;
if ([pointAnnotation.tag intValue] >= 10000) {
currentUserPicture = [UIImage imageWithData:[usersData getKeyForIndexWithKey:#"UserImageData" forIndex:newTag]];
}else{
currentUserPicture = [UIImage imageWithData:[usersData getKeyForIndexWithKey:#"UserImageData" forIndex:[pointAnnotation.tag intValue]]];
}
currentUserPictureView.image = currentUserPicture;
userLocationButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 78)];
[userLocationButton setBackgroundImage:[UIImage imageNamed:#"pin.png"] forState:UIControlStateNormal];
[userLocationButton addTarget:self action:#selector(centerOnUser:) forControlEvents:UIControlEventTouchUpInside];
if ([pointAnnotation.tag intValue] >= 10000) {
userLocationButton.tag = newTag;
}else{
userLocationButton.tag = [pointAnnotation.tag intValue];
}
[userLocationButton setBackgroundColor:[UIColor clearColor]];
[annImg addSubview:userLocationButton];
[userLocationButton addSubview:currentUserPictureView];
[userLocationButton release];
[currentUserPictureView release];
[currentUserPicture release];
}

Label refuses to disappear with [label setHidden:YES]; command in a if-method?

I am trying to customize my tableView in my IOS app. When my tableView(or rather array) is empty, I want to display a customized label instead of the items in the tableView. The label I am referring to is "label0". But something is terribly wrong, my [label0 setHidden:YES]; or [label0 setHidden:NO]; only works in the first block of the if "method"? In the second block (if else) nothing happens no matter what I try to set the label as (hidden or shown).
What have I missed? I cannot see my own fault?
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UILabel *label0 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width - 0, 100)] autorelease];
if ([self.searchResults count] == 0){
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lista2.png"]];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
label.text = #"Information";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
label0.text = #"Test test test";
label0.textColor = [UIColor blackColor];
label0.backgroundColor = [UIColor whiteColor];
[tableView addSubview:label0];
[label0 setHidden:NO];
}
else {
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lista2.png"]];
UILabel *label2 = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
label2.text = #"Search results";
label2.textColor = [UIColor whiteColor];
label2.backgroundColor = [UIColor clearColor];
[headerView addSubview:label2];
[label0 setHidden:YES];
}
return headerView;
}
EDIT
I have moved the code to viewDidLoad and set the property for the UILabel. This have unfortunately not solved my problem....
UILabel *label0 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width - 0, 100)] autorelease];
[tableView addSubview:label0];
if ([self.searchResults count] == 0){
label0.text = #"Test test test";
label0.textColor = [UIColor blackColor];
label0.backgroundColor = [UIColor whiteColor];
[label0 setHidden:NO];
}
else {
[label0 setHidden:YES];
}
This is because your label0 is created every time this method is called so in "else" you are referring to totally different object (not the one that you added to tableView when array was empty).
You shouldn't be adding subviews to tableView from this method. Consider using viewDidLoad. That way you will be adding label0 only once. To achieve that add label0 as property of your viewController.
You forgot to add label0 as subview please put this line in the else statement
[tableView addSubview:label0];
also I cannot see any benefit from doing so. I believe you can just hide the table view and show another view that has the label. but nesting views that way is not good when you come back to debug this code after 1 month you will struggle to understand it.
You say in your edit that you set the property for the UILabel (I assume you mean that you have a property called label0?). If this is so, then when you alloc init your label, it should be self.label0 = ..... not UILabel *label0 = .....

Action Sheets On The iPad

On the iPhone I like how action sheets take up the entire width of the screen and you can choose the height of it (so I can include other views). Here is my entire method that creates the action view (you probably on need the las few lines though)...
-(void)createActionView {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:#"Done",nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
UIButton *downHoleScore = [UIButton buttonWithType:UIButtonTypeCustom];
downHoleScore.frame = CGRectMake(93, 75, 35, 35);
[downHoleScore setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
downHoleScore.backgroundColor = [UIColor clearColor];
downHoleScore.tag=1;
[downHoleScore addTarget:self action:#selector(HoleScoreButtonTap:) forControlEvents:UIControlEventTouchUpInside];
[downHoleScore setBackgroundImage:[UIImage imageNamed:#"Left-Down-Arrow.png"] forState:UIControlStateNormal];
[actionSheet addSubview:downHoleScore];
UIButton *upHoleScore = [UIButton buttonWithType:UIButtonTypeCustom];
upHoleScore.frame = CGRectMake(193, 76, 35, 35);
[upHoleScore setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
upHoleScore.backgroundColor = [UIColor clearColor];
upHoleScore.tag=2;
[upHoleScore addTarget:self action:#selector(HoleScoreButtonTap:) forControlEvents:UIControlEventTouchUpInside];
[upHoleScore setBackgroundImage:[UIImage imageNamed:#"Right-Up-Arrow.png"] forState:UIControlStateNormal];
[actionSheet addSubview:upHoleScore];
golferScoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 70, 45, 45)];
golferScoreLabel.text = #"0";
golferScoreLabel.backgroundColor = [UIColor clearColor];
golferScoreLabel.font = [UIFont fontWithName:#"ChalkDuster" size: 45.0];
golferScoreLabel.textAlignment = UITextAlignmentCenter;
golferScoreLabel.shadowColor = [UIColor grayColor];
golferScoreLabel.shadowOffset = CGSizeMake(1,1);
golferScoreLabel.textColor = [UIColor whiteColor];
[actionSheet addSubview:golferScoreLabel];
CGRect pickerFrame = CGRectMake(0, 120, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet showFromRect:CGRectMake(0, 0, 320, 585) inView:self.view animated:YES];
}
How would I edit this to make it look more like an action view on the iPhone?
In this code...
[actionSheet showFromRect:CGRectMake(0, 0, 320, 585) inView:self.view animated:YES];
That above is where I think I am messing up how would I make this cover the entire bottom half of the screen on the iPad?
Read the docs on UIActionSheet. On iPad, they are presented in popovers.
If you want behavior like on the iPhone, I think you'll have to write your own actionSheet-like view and animations (which is really not that hard, it just takes a little time).
Anyway, presenting actionSheets on an iPad with the full width wouldn't be very good design anyway - you probably don't want buttons that are as wide as the whole screen. I would advise you to rethink your design.
But maybe you don't really want actionSheets. Maybe you only want that nice animation, to present some content of your own like that? That's certainly doable (see above), but then you have asked the wrong question.

How to change UIAlertView text color?

I'm currently using http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color for my custom UIAlertView. Everything is working fine but I'm trying to change the title and message text colors and I would like to change the button color. Is there any way to do this? I've tried using:
UILabel *theTitle = [AlertView valueForKey:#"_titleLabel"];
[theTitle setTextColor:[UIColor redColor]];
but I can't get it to work. Any help would be appreciated. Thanks.
You just need to include UIAlertViewDelegate in your .h file and override this method in your .m file.
-(void)willPresentAlertView:(UIAlertView *)alertView{
UILabel *theTitle = [alertView valueForKey:#"_titleLabel"];
theTitle.font = [UIFont fontWithName:#"Copperplate" size:18];
[theTitle setTextColor:[UIColor whiteColor]];
UILabel *theBody = [alertView valueForKey:#"_bodyTextLabel"];
theBody.font = [UIFont fontWithName:#"Copperplate" size:15];
[theBody setTextColor:[UIColor whiteColor]];
}
Check to see if setTextColor is depricated.
I thought it was
theTitle.text.color = [UIColor redColor];
or you could use
[theTitle.text setColor:[UIColor redColor]];
Not totally sure on the second one, the first example should do the trick though.
By default functionality, you could not change alert view title and message but with the help of accessory view, you can achieve this functionality.
Try below method :
-(void)customAlertTitle:(NSString*)title message:(NSString*)message{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 270, 50)];
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.numberOfLines = 2;
titleLabel.textColor = [UIColor redColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
[subView addSubview:titleLabel];
UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 270, 50)];
messageLabel.text = message;
messageLabel.font = [UIFont systemFontOfSize:18];
messageLabel.numberOfLines = 2;
messageLabel.textColor = [UIColor redColor];
messageLabel.textAlignment = NSTextAlignmentCenter;
[subView addSubview:messageLabel];
[alertView setValue:subView forKey:#"accessoryView"];
[alertView show];
}
The above code is working perfectly on latest XCode 8.3.1. Kindly comment if facing any problem.

How to create a passcode view iphone

I'd like to add a passcode lock in my app...
I created the view but I don't know how to get it working...
This is what I'd like it must do:
- If an user is setting his passcode, he must type it twice and the code must verify if the passcode typed the second time is the same of the first time.
- If the passcode controller is called by a setting view, for example, to set the passcode, it must have a cancel button on the navigation bar but if it is called at app launch, the cancel button mustn't be enabled.
summaryLabel is the label that show a message like "Passcode didn't match. Try again." when the passcode is not the same as the one written previously or saved.
EDIT1: How can I use textField:shouldChangeCharactersInRange:replacementString method to do this?
This is the code:#import "PasscodeController.h"
#implementation PasscodeController
#synthesize panelView;
#synthesize summaryLabel;
#synthesize titleLabel;
#synthesize textField1;
#synthesize textField2;
#synthesize textField3;
#synthesize textField4;
#synthesize hiddenTF;
-(void)viewDidLoad {
self.title = #"Passcode";
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 22, 270, 30)];
titleLabel.font = [UIFont boldSystemFontOfSize:15];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.textColor = [UIColor colorWithRed:66.0/255.0 green:85.0/255.0 blue:102.0/255.0 alpha:1.0];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
[titleLabel release];
summaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 130, 270, 40)];
summaryLabel.font = [UIFont boldSystemFontOfSize:12];
summaryLabel.numberOfLines = 0;
summaryLabel.baselineAdjustment = UIBaselineAdjustmentNone;
summaryLabel.textAlignment = UITextAlignmentCenter;
summaryLabel.textColor = [UIColor colorWithRed:66.0/255.0 green:85.0/255.0 blue:102.0/255.0 alpha:1.0];
summaryLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:summaryLabel];
[summaryLabel release];
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(25, 60, 60, 60)];
textField1.borderStyle = UITextBorderStyleBezel;
textField1.textColor = [UIColor blackColor];
textField1.textAlignment = UITextAlignmentCenter;
textField1.font = [UIFont systemFontOfSize:41];
textField1.secureTextEntry = YES;
textField1.backgroundColor = [UIColor whiteColor];
textField1.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:textField1];
[textField1 release];
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(95, 60, 60, 60)];
textField2.borderStyle = UITextBorderStyleBezel;
textField2.textColor = [UIColor blackColor];
textField2.textAlignment = UITextAlignmentCenter;
textField2.font = [UIFont systemFontOfSize:41];
textField2.secureTextEntry = YES;
textField2.backgroundColor = [UIColor whiteColor];
textField2.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:textField2];
[textField2 release];
textField3 = [[UITextField alloc] initWithFrame:CGRectMake(165, 60, 60, 60)];
textField3.borderStyle = UITextBorderStyleBezel;
textField3.textColor = [UIColor blackColor];
textField3.textAlignment = UITextAlignmentCenter;
textField3.font = [UIFont systemFontOfSize:41];
textField3.secureTextEntry = YES;
textField3.backgroundColor = [UIColor whiteColor];
textField3.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:textField3];
[textField3 release];
textField4 = [[UITextField alloc] initWithFrame:CGRectMake(235, 60, 60, 60)];
textField4.borderStyle = UITextBorderStyleBezel;
textField4.textColor = [UIColor blackColor];
textField4.textAlignment = UITextAlignmentCenter;
textField4.font = [UIFont systemFontOfSize:41];
textField4.secureTextEntry = YES;
textField4.backgroundColor = [UIColor whiteColor];
textField4.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:textField4];
[textField4 release];
hiddenTF = [[UITextField alloc] initWithFrame:CGRectZero];
hiddenTF.hidden = YES;
hiddenTF.delegate = self;
hiddenTF.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:hiddenTF];
[hiddenTF release];
[hiddenTF becomeFirstResponder];
}
Thanks a lot!
Another solution, KVPasscodeViewController (mine), is available here: https://github.com/Koolistov/Passcode (BSD license).
If this can be helpful for other, I solved my problem with this source code on GitHub: PTPasscodeViewController.
I changed it a little bit to adapt it to my needs and now works greatly :)
If you want to use it, there are all the information about how to use it on the project page or in a file if you've downloaded it ;)
Hope this help!
P.S.: Thanks a lot to Lasha Dolidze to provide this code!
PINCode 1.0