If labels are equal change view - iphone

hi I got this code and when I hit button a lot of things happen, images get set etc. But I also want it to compare 2 labels and when They are equal I want it to change view. It doesn't work can someone have a look why?
-(IBAction)play {
if (labelsText.text == textview.text){
GoedwoordViewController *Goedwoord = [[GoedwoordViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Goedwoord animated:YES];
}
labelsText is my input label, textview is a label in which a random word will appear.
GoedwoordViewController is the destination view. and GameViewController is the current view.
hope someone knows

Change Following line...
if ([labelsText.text isEqualToString textview.text])
Hope, this will help you..

When you do:
if(someString==anotherString){}
You're checking if the pointer of someString is the same of anotherString, so never use == to compare strings.
The best is to use the #Nit solution that check if the strings are equals.

Related

How do I save values from a UITextField in the FlipsideViewController and use them in the MainViewController?

SEE EDIT AT BOTTOM
I am making a UtilityView application for the iPhone and I want to be able to change values from the FlipsideViewController. I have a problem that whenever I switch back to the MainView from the FlipsideView, the values of the text fields that I had changed in the FlipsideView are seemingly reset to 0. In the MainViewController.m, under the flipsideViewControllerDidFinish:
playToInt = [controller.playTo.text intValue];
computerMoveSpeed = [controller.aiSpeed.text intValue];
The text field 'playTo' coordinates with the integer 'playToInt' and the text field 'aiSpeed' coordinates with the integer 'computerMoveSpeed'. I think this should save the values, even when the ViewDidLoad runs again, but they both seem to be reset to 0.
if (playToInt != 10 || computerMoveSpeed != 3)
{
playBool = true;
}
else
{
playBool = false;
}
I know that the boolean is working because I can change the integers' values with it, but they always default back to 0. Any help is appreciated. Thanks in advance.
NOTE: I have consulted the previous question 'Passing UISegmentedControl values from FlipSideViewController in an Utility application to the mainviewcontroller…' and that does not work for me... I am have too many possible values to use an index or a switch.
EDIT: I am trying something that I think is really close to working... I feel like it just has bad syntax or something... I have in the MainViewController.m:
computerMoveSpeed = [FlipsideViewController.aiSpeed.text intValue];
playToInt = [FlipsideViewController.playTo.text];
EDIT: I have something that is similar to the last one but I think it will have a better chance of working with some help...
in the FlipsideViewController.m I have
_playToNumber = [_playTo.text intValue];
and in the MainViewController.m
playToInt = [controller.playToNumber];
Any help is greatly appreciated, but as some additional info, on the string in the MainViewController it says it needs an identifier on the playToNumber variable, but it is declared as int... I don't know if there is an identifier for ints but if there is, please let me know!
EDIT: Is there any way to save the values in a usable format for the MainViewController using the prepareForSegue function in the FlipsideViewController?
EDIT: I figured out that it was just getting the initial values when it loaded the ViewDidLoad, so I started using a timer to retrieve the values. This seems to be working, I just need to know how to access these values from the MainViewController?
Use delegate to send values back to MainViewController. See this tutorial to learn how to use delegates.
You have many way to do this task,
1. NSUserDefault
Follow ma answer Link
2. Using Getters, setters, property
Follow ma answer Link
EDIT
If you want int or NSInteger then no need for retain
just do
#property(nonatomic) NSInteger YourIntValue;
and
_playToNumber = [_playTo.text intValue];
MainViewController *MVC = [[MainViewController alloc] init];
MVC.playToInt=_playToNumber;
[self.navigationController pushViewController:MVC animated:YES];

Select multiple images

I have this UITableView which is displaying images, downloaded rom a database, as a matrix: 4 images in each table row.
To be able to select images from the views I'm using a UITapGestureRecognizer. To make each selection unique I've been trying to tag each tap recognizer and each imageView. That's where the problem is...
I've put a log within the for-loop that is creating and tagging the imageViews and recognizers and I can see in the output that they pass through all the values. However when I try to get the tag by later pressing an image I always get "3" (the last number in the table row). This makes me think the tags are simlpy overwriting eachother even though I'm creating a new object in each loop. Either that or I'm reading it out the wrong way.
Unrelated parts cut out.
for (NSInteger i = 0; i < 4; i++){
asyncImage = [[AsyncImageView alloc]
initWithFrame:frame];
[asyncImage loadImageFromURL:url];
asyncImage.tag = i;
NSLog(#"TAG %d", asyncImage.tag);
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap)];
tapRecognizer.view.tag = i;
NSLog(#"TapTAG %d", asyncImage.tag);
[asyncImage addGestureRecognizer:tapRecognizer];
}
And the method:
- (void)handleTap{
NSLog(#"TAP %d", self.tapRecognizer.view.tag);
}
If you think I'm doing it all totally wrong, a light push in the right direction is always welcome!
Thanks in advance, Tom
The following line has no effect until the gesture recognizer has been added to a view:
tapRecognizer.view.tag = i;
This is because tapRecognizer's view is initially nil. Make the assignment on the last line of your for loop to correct this problem.
Also your NSLog is always showing the tag of the last recognizer that you have added
self.tapRecognizer.view.tag // Instance variable
not the one that fired the event. Change handleTap as follows:
- (void)handleTap:(UITapGestureRecognizer*) tapRecognizer{
NSLog(#"TAP %d", tapRecognizer.view.tag);
}
You should also replace the tapRecognizer instance variable with a local variable in the method that adds the recognizer to the view, and add a colon : to your selector name:
action:#selector(handleTap:)
// HERE: ----^
I think you're doing it wrong in the loop.
Your loop is run 4 times and every time you run the loop you store AsyncImageView to the asyncImage variable (local or instance?). So the first time you run the loop, you create an object and store it at the asyncImage location, the second time this is overwritten, the third....
You have initialized 4 ImageViews, but you are only referencing to the last one. And the last one holds the correct GestureRecognizer, you want.
When do you add the ImageView to a view?
If you use the instance varable and overwrite it directly, all the other ImageViews you added to the screen point to the pointer of asyncImage. And the pointer - after running 4 times the loop and exchanging asyncImage data - points to the last image in the loop.
Hope you understand, what the problem is here.

Edit the toobarItems of topViewController

I want to change dinamycally the ToolbarItems of my topviewcontroller.
I have a class that does my business logic and as a result returns an array of uibarbuttonitem.
so, i get the reference of the topviewcontroller like this:
ViewController *topVC = [appDelegate.navController topViewController];
now if i try to do:
[topVc setToolbarItems: myArrayofUibarbuttonItems];
But nothing happens.
What I'm missing?
Guess you could do it with the setItems function
[topVc.toolbar setItems:theArray];
just typed, not tested
[topVc.navigationController setToolbarItems:myArrayofUibarbuttonItems];
this should help u.. happy coding :)

iOS - Navigation Title Not Updating

What I'm doing with this bit of code is grabbing an image sequence, grabbing it's name. Then setting the view's title as the sequences name playing the sequence then setting the title back to the old title.
Problem is the title doesn't seem to be changing on the navigation bar. The NSLogs are outputting the correct values though.
I remember having this issue before and solving it with some "refresh" method.
Here is the pertinent code.
-(void)playSequence
{
if (([animatorViewController isAnimating] == FALSE) && (btnDisable == FALSE))
{
Sequence *tempSequence;
tempSequence = [fullStepList objectAtIndex:lastPlayedSequence];
self.title = [NSString stringWithFormat:#"Assembly - %#", tempSequence.subName];
NSLog(self.title);
[self startAnimator:tempSequence.imageNameScheme forNumFrames:tempSequence.numberOfFrames playInReverse:FALSE];
tempSequence = nil;
tempSequence = [fullStepList objectAtIndex:queuedSequence];
self.title = [NSString stringWithFormat:#"Assembly - %#", tempSequence.subName];
NSLog(self.title);
tempSequence = nil;
}
}
EDIT: This method of changing the title is working else where in this class. The problem seems to come in when trying to set it twice that is causing the issue.
EDIT2: It's actually running both title changes one after another... Fact that it was setting it back to previous was throwing me off.
A navigation item may contains four things: leftBarButtonItem, rightBarButtonItem, title, and titleView. When you need to change the title you should assign your title to navigationItem's title property, not to your view's title property. So it would look like:
self.navigationItem.title = [NSString stringWithFormat:#"Assembly - %#", tempSequence.subName];
self.title points to your viewControllers title which don't shows up in the navigationBar.
The title won't appear to update until after the main run loop executes. Unless -startAnimator:forNumFrames: invokes the main run loop to execute before it returns, you won't see the change.
If you are hosting the UINavigationController inside another view, you need to do this:
self.yourNavigationController.title = #"title";
instead of
self.title = #"blah"
Otherwise you are just changing the title of the view hosting the view navigator. Just a guess.
I recently found myself in a similar situation. My view's title was comprised of a label with an NSMutableAttributedString. Calling [self reloadInputViews] whenever I updated my title worked for me.
The dreaded P.E.B.K.A.C. error was the cause of this confusion. It's actually running both title changes one after another... Fact that it was setting it back to previous was throwing me off.

UITextField settext not working

This is frustrating the hell out of me. I am a beginner programmer and cannot figure out why the text is not being changed.
Here is my method which is supposed to set the text of a UITextField:
-(void)updateDays:(NSInteger)days
{
NSString* daysString = [[NSString alloc] initWithFormat:#"%d", days];
[daysTextField setText:daysString];
[daysString release];
}
For whatever reason nothing is happening.
Help appreaciated!
Anytime you have a frustration along the lines of "why isn't this line working", use the debugger or just add an NSLog before it to print out the relevant data:
NSLog(#"updateDays: %# %# <= %#", daysTextField, daysTextField.text, daysString);
Then you know the line (a) is getting executed, (b) the variables are the ones you think they are, and (c) the values are reasonable.
I have experienced this several times, but I think all of them was reasoned by that the UITextField was not initialized at that point.
Set a breakpoint and use debugger to make sure that your UITextField is not nil. You should also check the connection between the .xib file and your code.
Check if the method is called. If yes check if the textfield is set up properly. Cross check to see if the IBOutlet connections are made to the correct object.
I meet this problem too .
It's my code:(in viewdidload)
UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,50)]];
tv.text = #"12345";
tv.textColor=[UIColor blackColor];
[self.view addSubView:tv];
And the text(12345) doesn't show;
Lastly,I found that when i set textColor to gray,or any color not black,it works;
I think that it's a bug of the simulator.
I use xcode 4.2 and the iphone5.0simulator.