How Do I validate UITextfield with Correct Answer String? - objective-c

I have a UITexfield in Xcode that the user inputs their answer to a question. If the answer is correct they are taken to another screen. All the help I have found only shows how to verify the amount of characters or numbers in a string.
I have declared
NSString *answer = #"The Correct Answer";
and
if ([UITextField1.text isEqualToString:#"The Correct Answer"])
then ...
This is where I get lost (sorry total newbie) I have been able to compare these in NSlog in foundation.h file but get really lost when trying to deal with .h and .m files in xcode.
Can anyone please check if with the above I am on the right track and please explain how I get to display the new screen when the string/answer is correct.
Thanks in Advance

The check you are making is correct. It checks the precise text of the TextField1 with the precise text of #"The Correct Answer". If it matches you can make your navigationController push a new view.
ea. Like this:
MyViewController *myVC = [[MyViewController alloc] init];
//do something with your new ViewController.view
[self.navigationController pushViewController:myVC animated:YES];
Of course you will have to adjust the new View to your own needs. Also make sure you have access to that navigationController.
But yeah, this is all very basic and you should be able to figure it out for yourself with some proper tutorials.

Well I'm not clear on your question.
If you are asking how to show a screen once a user typed the right answer in textbox then use:
Set textfield's delegate to your view controller then handle this method in your view controller:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Just do the needful there, something like:
if ([textField.text isEqualToString:#"The Correct Answer"])
{
[self presentModalViewController:correctVC animated:NO];
return NO;
}
return YES;

Related

UITextView beginningOfDocument returns nil

I have to get the position on the screen of a string contained in a UITextView. To do that, I tried to get the corresponding UITextPosition(s). The problem is that the UITextView method beginningOfDocument returns nil (same for endOfDocuement). Could someone explain me why? And which solution I could use.
My code looks like this:
- (UITextRange *)getRangeOfString:(NSString *)string inTextView:(UITextView *)tv
{
UITextPosition *beginning = [tv positionFromPosition:tv.beginningOfDocument offset:[tv.text rangeOfString:string].location];
UITextPosition *end = [tv positionFromPosition:beginning offset:[tv.text rangeOfString:string].length];
return ([tv textRangeFromPosition:beginning toPosition:end]);
}
This is about a month late, but make sure you have the "selectable" property enabled in Interface Builder.
Credit: http://jon-nolen.blogspot.com/2013/10/uitextview-returns-nil-for-uitextinput.html
Make sure the UITextField is added as a subview of another view. If it exists just in memory, the beginningOfDocument and endOfDocument properties will return nil for some reason.

NSTokenField selection list shows empty space while scrolling

While using NSTokenField something strange is happening, as shown in the images below :
As I type A, selection from popup is shown.
I scrolled it
Some more scroll, and it went below the visible area.
This is a behaviour with all the tableviews. The view behind the rows are visible , but it automatically springs to normal position. But not in this case.
It is fine in Mail app, it is working fine.
My implementation is :
Created an NSTokenField.
Set its delegate to AppDelegate.
In the implementation file
-(NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex{
return #[#"A",#"B",#"C"];
}
Even the sample code from apple documentation behaves incorrectly.
How can I make it auto-spring or restrict by some code?
What you see in Mail.app is not an actual NSMenu (Apple cheats, shocking!). Turns out, it's actually a custom NSTextField linked to an NSTableView stuck in a transparent window.
It's a fairly old trick to get around the extremely poor version of scrollWheel: NSMenu happens to have implemented. MTTokenField is a mature alternative to pulling your hair out trying to stick a scroll view in an NSMenu.
You need to predicated the substring with the array content.This will list you the exact matching records(this is the plus point). The other is this will avoid you scrolling as well.
You got to change the delegate method in the following way which will fix the issue.
-(NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex
{
NSArray *arrayContents = #[#"A",#"B",#"C"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF beginswith[cd] %#", substring];
return [NSArray arrayWithArray:[arrayContents filteredArrayUsingPredicate:predicate]];
}
Hope this will help you.

Passing text from UITextView to string in different ViewController

I need to pass the text of a UITextView in viewController1 to viewController2 and set the text to a string in viewController2. I thought I had the code figured out, but it turns out that nothing is being passed.
In viewController2.h I added
viewController1*v1
and assigned a property to it.
in the viewController2.m
-(void)method{
v1=[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil];
NSString *text=[[NSString alloc]init];
[v1.tweetText setText:text];
}
This is probably the wrong way to go about it, so does anyone have other solutions to this problem?
Thats the correct way to send data to another vc, but you are not setting the variable text to anything. Try setting it to hello and see if you get hello as the tweetText in the other vc.

UITableViewCellStyle

I would like to create a table like the iPhone (calls) which are displayed three labels.
UITableViewCellStyleSubTitle displays only two options as well as UITableViewCellStyleSubValue1.
Do I have to manually create it?
Thanks.
/ / English Google
A third place to put something is the accessoryView, which shows up to the right of the cell.
The best way to have full control over a cell is to make a custom UITableViewCell. An example.
Yes, you have to manually create it. Watch this YouTube video for a tutorial.
Also, next time you ask a question, provide a more descriptive title, and you will likely get more views and answers.
As Peter DeWeese was saying you have to make a custom UITableViewCell.
Let me point out that since iOS 6 this is the only correct way to do it.
Make a subclass UITableViewCell.
In the initializer (.m file) you can rewrite the style that is given when the super class is called.
code example:
(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:--------UITableViewCellStyleSubtitle------- reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
Go to your TableViewController and registrate that cell in the viewDidLoad method.
[self.tableView registerClass:[---------FriendCell---------- class] forCellReuseIdentifier:#"vriendCel"];
//And if someone would help me out to put my code properly, can't seem to fix it. :)

Keyboard not show onload

I want the keyboad show automatically onload, but it did not work as i expect
here is the code i use:
- (void)viewDidLoad {
[super viewDidLoad];
[seachBar2 setDelegate:self];
}
- (BOOL)Searchbar2ShouldReturn:(UISearchBar *)searchBar2 {
[searchBar2 becomeFirstResponder];
return YES;
}
Coud somebody will point me how to fix this
thank you somuch
- (void)viewDidLoad {
[super viewDidLoad];
[seachBar2 setDelegate:self];
[seachBar2 becomeFirstResponder];
}
Just came across this, and it helped but the answers are vague.
All you need to do now in iOS 5 is make an outlet connection to your object (for example a UITextField) and then in viewDidLoad method type;
[myTextField becomeFirstResponder];
or for your search bar
[searchBar2 becomeFirstResponder];
Make sure searchBar2 in your code points (IBOutlet) to the searchBar in the Interaface Builder.
in your code:
IBOutlet UISearchBar *searchBar2;
In IB:
goto the search bar's Connections Inspector (apple-2) and drag the Referencing Outlet to File Owner and select searchBar2
Hope this helps.
You do need to override viewDidAppear:, and verify it's actually being called (put a breakpoint or an NSLog() statement in there). You should also determine the language you're coding in (it's Objective C).
Your -Searchbar2ShouldReturn: method will never be called by the system. I think you may need to go back and work through a few of Apple's tutorials here; your grasp of the frameworks seems tenuous, at best.