cycle through entries in NSDictionary - iphone

Im writing a game that has two sets of buttons. The first set is like blank spaces, and the second set is a custom keyboard.
So I've created a dictionary to keep track of selected buttons (blank spaces) in my interface. The user can select say 5 of these and update all of their titles at once. When the user presses a key (on my custom keyboard made of buttons in a separate subview) I want to change all of the titles to show the letter on the key the pressed.
I have my keys numbered (alphanumerically) from 1 to 26 (ie. a=1, z=26) and the current set of labels on the blanks will be kept in similar numeric form in an array called "currentSolution"
Im imagining i can set up a for loop to go through selected blanks and give them all the title of whatever key is selected in the keyPressed method. Can anyone give me any guidance here? im kinda a noob and dont really know how i should go about this
thanks

You can use (only in ios 4)-
[yourDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
NSLog(#"dic = %# : %#", key,object);
//Do what you want for every object...
UILabel *label = (UILabel*)object;
label.text = #"bla bla";
}
}];
Hope it will help

Related

Updating an NSTextField while controlling an NSTableView [duplicate]

What's the easiest way to have an NSTextField with a "recommendation list" dynamically shown below it as the user types? Just like Safari's address bar that has a menu of some sorts (I'm pretty confident Safari's address bar suggestions is menu since it has rounded corners, blue gradient selection, and background blurring).
I've tried using NSTextView's autocompletion facility but found it was inadequate:
It tries to complete words instead of the whole text fields – in other words, selecting an autocomplete suggestion will only replace the current word.
It nudges the autocompletion list forward and align it with the insertion point instead of keeping it align with the text field.
In the sample screenshot above whenever I selected the autocomplete suggestion the text field only replaces K with the suggested item in the list, which results in Abadi Abadi Kurniawan.
These are what I'd like to achieve:
Whenever a suggestion is selected, the entire text field is replaced with the suggestion.
Keep the suggestion list aligned with the text field's left side.
Note: This is not a question about adding progress indicator behind a text field.
The Safari address bar uses a separate window. Apple has example project CustomMenus and it only takes an hour or two to customize it.
Developer session explaining what has to be done Key Event Handling in Cocoa Applications
If you want to be able to select multiple words you need to provide own FieldEditor (credits should go for someone else)
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(nullable id)client;
{
if ([client isKindOfClass:[NSSearchField class]])
{
if (!_mlFieldEditor)
{
_mlFieldEditor = [[MLFieldEditor alloc] init];
[_mlFieldEditor setFieldEditor:YES];
}
return _mlFieldEditor;
}
return nil;
}
- (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag
{
// suppress completion if user types a space
if (movement == NSRightTextMovement) return;
// show full replacements
if (charRange.location != 0) {
charRange.length += charRange.location;
charRange.location = 0;
}
[super insertCompletion:word forPartialWordRange:charRange movement:movement isFinal:flag];
if (movement == NSReturnTextMovement)
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"MLSearchFieldAutocompleted" object:self userInfo:nil];
}
}
This only addresses half of your answer, but I believe you need to subclass NSTextView and implement the - (NSRange)rangeForUserCompletion method, returning the range of the entire string in the text field. This should make sure that it doesn't just autocomplete the most recently entered word.
If you want a custom menu, you're going to have to do that yourself, probably by implementing the -controlTextDidChange: method and displaying a custom view with a table when appropriate.

Objective C Collect Data from TextField and List in TextView

On View1, I have an empty textView that will populate with data from a textField. The textField is on View2. Every time I swipe right to view the data in View1, it never lists in the textView. It overwrites whatever is currently there.
Here's my code: https://github.com/dward1289/Objective-CII/tree/master/week4Proj
The assignment is already done, but even after it was graded I never found out what I was doing wrong. Please take a look, and tell me what I can change so that the textField data will populate in the textView and not overwrite the current text.
I guess you want to append the new input to the old information
. If so,
-(void)DidClose:(NSString*)nameString
{
txtView.text = [txtView.text stringByAppendingString:nameString];
}

Display a series of user selected integers, separated by a comma

I need to produce a list of integers in a label.
On the iPhone screen there are a series of 12 buttons and a label-
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
[___________]
The user is to click a button, and the buttons's id should then be displayed in a label.
If the user clicks 0,4,7,3,10
Then I want the label to display- [0,4,7,3,10]
This is similar to a calculator app, but a calculator is base10 and its numbers combine to form a string. I would like to keep each integer separate so that other calculations may be performed on the user selected order.
in my #implementation i have tried to modify my calculator app's code, but have had no progress.
Any ideas?
Sounds like you need to keep an array (or some sort of stack) of the numbers pressed, and append to the label's text every time the user hits a button. What are you having issues with?
Your buttons could fire this IBAction:
- (IBAction)addNumberToLabel:(UIButton *)sender
{
[numbers addObject:[[sender titleLabel] text]];
[label setText:[NSString stringWithFormat:#"[%#]", [numbers componentsJoinedByString:#", "]];
}
Since you want the numbers to be displayed [1, 2, 3, 4], it is necessary to change the whole text of the label every time a new number is added to numbers, an NSMutableArray you initiate in your setup code.
Create a new string StrOldValue, and get the actual value of label, in the set to label use
NSString *strOldValue= yourLabel.text;
yourLabel.text= [NSString stringWithFormat:#"%#,%#",strOldValue, strNewValue)];
Keep your things in a Mutable array.
Then call componentsJoinedByString method like
NSLog(#"%#",[pathArray componentsJoinedByString:#","]);
You can use any string in place of 'Comma' as your delimiter.

How i can fetch what is typed between 2 UITextViews on iphone?

i have two UITextView items, how can i fetch what is written using a button on iphone?
Imagine something like a translate app, the user enters a word in UITextView 1 and by pressing the button the UITextView 2 is getting filled with data.
UITextView has a property text. Simply use this.
Set up IBOutlets for textView1 and textView2. Then have the button do something along these lines:
-(IBAction)moveTextOver:(id)sender {
[textView2 setText:textView1.text];
}
To get fancier, you can have a method -(NSString *)transformText:(NSString *)text that translates or does whatever you like. Then use
-(IBAction)moveTextOver:(id)sender {
[textView2 setText:[self transformText:textView1.text]];
}
Create an IBAction method that is linked to a button and in that method read the "text" property of the textView or textField, do your calculations on it and assign the results to the text property of thee second field.

UITextView or UIWebView: How to display pages of a simple editable TXT file

All I want to have is a full-screen simple text editor. However, I don't want the scrolling component, but rather let the user flick through the pages (instead of scrolling). So I need to import or open a TXT and then format it by braking it down (e.g. by dividing its contents to 10 lines per screen/page).
My question is how I will display the txt? UITextView is scrollable (even though I can disable this in IB)... I did not find any method for UIWebView to let it format my contents on different 'pages' or screens.
Where will I need to start? Ideally I'd need some sample code. All the samples for UIWebView do not tell me anything about how to format editable text on several pages.
So all I really want is an UITextView which is not scrollable and opens up a new page/screen if I run out of space on the first page/screen.
Thanks for any help to get me started.
first thing first ...... there is no particular method to achieve this
1.You need to break your single string into multiple strings, to do that you can use
int pageNumber; // suppose this keep track of on what page you are
int count; //suppose this keep track of how long string your one screen support
NSString* completeString; //suppose this is your string
NSRange range = NSMakeRange(pageNumber * count, count);
NSString* temp = [completeString substringWithRange:range];
2.Now instead of using UITextView (if you don't want user interaction ) you should use UILable
just change the property of UILabel (this one is of your interest)
UILabel* myLabel; //suppose this is that label
myLabel.numberOfLines = 0; //this will chage your label to go multyline.
You should be able to achieve this by putting your UITextView into a UIScrollView and setting pagingEnabled on the UIScrollView to YES.