ios making a button change view when text field equals string - objective-c

I am making this kind of password to skip levels in a game. But I dont know what code I need to make a button change View Controller...
- (IBAction)button:(id)sender {
if ([txt.text isEqualToString:#"passwordToSkipLevel1"]) {
// Code to change View Controller???
}
}
I am not sure if I have done it correct to far, and I am not sure how I can make it change View Controller either...
Any ideas?
Extra: Is there any method to make the password not case sensetive?

What you have seems fine as far as it goes. To make the comparison case-insensitive, you can use
if ([txt.txt localizedCaseInsensitiveCompare:#"passwordToSkipLevel1"] == NSOrderedSame) {
// ...
}
To change ViewController... is this a storyboard-based design? If so, you could write
if ([txt.txt localizedCaseInsensitiveCompare:#"passwordToSkipLevel1"] == NSOrderedSame) {
[self performSegueWithIdentifier:#"SegueToLevel2VC" sender:self];
}
where SegueToLevel2CV is the identifier you've given the segue that goes to the next level, typed in to Interface Builder when you constructed the storyboard.

Related

Enabling Done Button When User Changes Values - Xcode

I am trying to make it to where if any value is typed on the keyboard without hitting the return key and two of the labels have been changed from "Select" to something else, the button up at the top becomes enabled. However, I have tried using an IBAction saying:
- (IBAction)valuesChanged {
if (textField.text != nil && ![labelOne.text isEqualToString:#"Select"] && ![labelTwo.text isEqualToString:#"Select"]) {
NSLog(#"Success");
}
else {
NSLog(#"No Success");
}
}
But I have realized that this does not work because:
The textfield does not work when I put the IBAction Sent Event as "Value Changed"
The labels won't accept an action.
How do I go about doing this?
The text field delegate method that tells you that the user is typing a character in the text field or otherwise changing its contents is textField:shouldChangeCharactersInRange:replacementString:. Implement it in your text field delegate and respond as appropriate. You will also just return YES.
One reason your original code couldn't be hooked up might be that you have not used the canonical form of an IBAction method; it should be
- (IBAction)valuesChanged:(id)sender {
Another problem in your original code is that a UITextField does not emit Value Changed. What you wanted was Editing Changed. But the delegate method works just as well.
Try UITextField delegate methods.
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;

Why won't my custom view become First-Responder, iOS?

I am following the book, iOS Programming Big Nerd Ranch Guide, and I have come to a lesson where I am to create a custom view, HypnosisView. Now, I am suppose to have this view change it's color on shake, but it says I am suppose to make it the First-Responder.
I used,
- (BOOL)canBecomeFirstResponder
{
return YES;
}
and
BOOL success = [view becomeFirstResponder];
if (success) {
NSLog(#"HypnosisView became the first responder"):
} else {
NSLog(#"Could not become first responder");
}
However, whenever I run my app, it always says that it could not become the first responder.
Any help would be appreciated.
UPDATE
I forgot to mention I get this output message.
Application windows are expected to have a root view controller at the end of application launch
Alright. I figured it out. I needed to put the delegate method
- (BOOL)canBecomeFirstResponder
{
return YES;
}
In the CustomView.m file, not my App Delegate file. Easy fix.

How do I know if a view is visible or not?

Say I have two view controllers: xVC and yVC. I have used the shake API and and have used the methods -(void)motionBegan,-(void)motionEnded: and -(void)motionCancelled in xVC. What happens is when the device is shaken, it fires a simple animation. Now the thing is that this animation is fired even when the I have yVC open that is, when yVS.view has been added as the subview. What I am looking for is some if condition which I can use in -(void)motionEnded: like this:
if(yVC == nil)
{
//trigger animation
}
By that I mean that the shake shouldn't work when yVC is visible. How do I do that? Please help.
The general advice I have seen and used is to ask a view if it has a non-nil window property:
if( ! yVC.view.window) {
// trigger animation
}
But note that this doesn't always equate with being visible; though in most apps it's about as good as you can performantly get (the basic case where it's not accurate is when a different view completely obscures it, but this may still satisfy your needs)
Add this to both of your view controllers:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
visible = YES;
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
visible = NO;
}
Now, just check the variable isVisible of both the view controllers and trigger your animation likewise.
The previous answers all work to some degree, but fail to take modally presented view controllers into account. If view controller A presents view controller B, of the previous answers will tell you that A is still visible. If you, like me, want to know whether or not the view is actually visible (and not just a part of the view hierarchy), I would suggest also checking the presentedViewController property:
if (self.isViewLoaded && [self.view window] && !self.presentedViewController) {
// User is looking at this view and nothing else
}
This works since presentedViewController will be non-nil whenever the current view controller OR any of its ancestors are currently presenting another view controller.

Cocoa-Touch UIButton isSelected clarification

I'm rather new to programming in Cocoa, but I've been working on learning the language quite diligently up until I hit this snag that I can't seem to circumvent/hack my way around, (not that I'd want to. I'd prefer to do it right!)
Where I stand, In IB I have a toolbar that has a button and what I'm trying to do is mimic the maps app. I want to be able to press the button and then have my location pop up, while keeping the button selected, then when it's pressed again, deselect it and thus remove the blue blip location from the map.
ideally, I would like to use the following code, but the if statement doesn't seem to want to work on the simulator (which I presume wouldn't change if I tried on the iPhone.)
-(IBAction) showLocation: (id) sender
{
if([sender isSelected]) // this doesn't work!!
{
[sender setSelected:NO];
mapView.showsUserLocation = FALSE;
}
else
{
[sender setSelected:YES];
mapView.showsUserLocation = TRUE;
}
}
obviously if I get rid of the if statement, I know that I can show the location and set the selected as I liked, but I can't seem to "get" the selected property from the button... what's the right way of doing this?
try
- (void)methodName:(UIButton *)sender
{
if (sender.selected == YES) ...

Dynamically change the textview.keyboardType using segment control

-(void)segmentAction
{
if(segmentedControl.selectedSegmentIndex == 0)
{
textview.keyboardType = UIKeyboardTypeNumberPad;
}
else
{
textview.keyboardType = UIKeyboardTypeDefault;
}
}
This is an objective C/iPhone question.
Is the function 'segmentAction' hooked up to the segmentedControl properly in Interface builder ? Make sure this gets called, for instance, on 'valueChanged' for segmented control, and it should change your keyboard.
Either segmentAction needs to be an IBAction or it needs to be called from one.
Make sure segmentedControl is properly hooked up, as an IBOutlet.
Also, where is 'textview' coming from. Your textView delegate functions return a variable called textview, if this is a member variable called textView things are going to get confusing quickly for you.