Retain UILabel value between random views - iphone

I'm making a view based quiz app for the iPhone where the player is going randomly between three different views when answering the questions. They get +1 when answering correct and -1 when tapping the wrong answer. But after answering the question, when you go to the new random view, I need the UILabel to show the score from the previous view. How do I do that?
Here is my code:
ViewController.h
#interface ViewController : UIViewController {
IBOutlet UILabel *labelQuestion;
IBOutlet UILabel *labelAnswer1;
IBOutlet UILabel *labelAnswer2;
IBOutlet UILabel *labelAnswer3;
IBOutlet UILabel *labelScore;
int score;
}
#property (nonatomic, retain) IBOutlet UILabel *labelScore;
#property (nonatomic) int score;
ViewController.m
#synthesize labelScore;
#synthesize score;
-(IBAction)CorrectAnswer; {
score = score +1;
labelScore.text = [NSString stringWithFormat:#"%i", score];
}
-(IBAction)WrongAnswer; {
score = score -1;
labelScore.text = [NSString stringWithFormat:#"%i", score];
}

Make the variable score global i.e. define this in your AppDelegate and you can access it throughout your application.

the quickest but dirty way is to store it in app delegate.. and if you want it to persist between app launches, store it in user defaults..
but as i said this is dirty..

As they're properties, when you create the next view you can set the label's text to the current view's text and the score to the current view's.

Related

how to make a drop down list for iphone application

I want to make a drop down list for iphone application i want that when user click it show list and when click on any button it shows that value and hides list i have implemented using the follwoing code but this work only for one value not for others
#interface DropDownViewController : UIViewController {
IBOutlet UILabel*ddText;
IBOutlet UILabel*ddMenu;
IBOutlet UIButton*ddMenuShowButton;
}
#property (nonatomic, retain) IBOutlet UILabel *ddText;
#property (nonatomic, retain) IBOutlet UIView *ddMenu;
#property (nonatomic,retain) IBOutlet UIButton *ddMenuShowButton;
- (IBAction)ddMenuShow:(UIButton *)sender;
- (IBAction)ddMenuSelectionMade:(UIButton *)sender;
#end
#implementation DropDownViewController
#synthesize ddMenu, ddText;
#synthesize ddMenuShowButton;
- (IBAction)ddMenuShow:(UIButton *)sender
{
self.ddMenu.hidden = NO;
[sender setTitle:#"▲" forState:UIControlStateNormal];
}
- (IBAction)ddMenuSelectionMade:(UIButton *)sender
{
self.ddText.text = sender.titleLabel.text;
[self.ddMenuShowButton setTitle:#"▼" forState:UIControlStateNormal];
self.ddMenu.hidden = YES;
}
I have three button red grenn blue first is red it works only for red only not for others how to solve this i have used this following this tutorial
http://www.edumobile.org/iphone/iphone-programming-tutorials/a-simple-drop-down-list-for-iphone/
Add UIbutton near your textfield.While clicking that button create UITableview according to your textfileld size.(Hope you know about creating Uitableview)and then load your listview contents in "cellForRowIndex".While selecting the row of the table,you can get the value from "didselectrowatIndexPath" and load it in textfield.After that remove UItableview.
It would be good if you use iPhone tools in your application. You should use UIPickerView in the place of drop down list. It looks good and provides better interface in mobile application

Get reference to custom UISlider

I have implemented a custom class of UISlider so the touchable areas are larger
Everything is working great except now I want to change the initial values depending on other factors in the app.
The only code I have gets the slider's ID from (sender)
I need to address the slider like so...
slider.value = 25.0;
But the only code I have is
-(IBAction) slider1Changed:(id) sender{
UISlider *slider = (UISlider *) sender;
int progressAsInt =(int)(slider.value);
NSLog(#"Slider is %#",slider);
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:progressAsInt forKey:#"SharedSkilledWorkforce"];
}
In the xib #property (nonatomic, retain) UISlider *slider1; doesn't show up as a conncetion
How do I address the custom UISlider?
Have you tried adding IBOutlet to your slider1 property?
This:
#property (nonatomic, retain) UISlider *slider1;
Needs to be:
#property (nonatomic, retain) IBOutlet UISlider *slider1;
in order to show up in IB. IBOutlet is a hint to the designer similar to IBAction.

Objective C / iPhone - how to bind UISlider to code

I'm just starting with Objective C, but I'm already running into a problem:
When I did this in the past, I had no problem with it:
I made a new UISlider, clicked it, CTRL dragged a blue line to the code, and somehow I was then able to read it's values.
However, I am now running into the problem that I can't draw those lines anymore.
By messing around I managed to get buttons to work, but the slider just won't work.
my Mainwindow.xib has got the iSaturateViewController class,
iSaturateViewController.xib has got the whole interface in it.
How can I get the UISlider to work without being able to draw that line?
How I have it now:
(in iSaturateViewController.h)
#import <UIKit/UIKit.h>
#interface iSaturateViewController : UIViewController {
UISlider * satSlider;
UILabel * satLabel;
}
#property (nonatomic, retain) IBOutlet UISlider * satSlider;
#property (nonatomic, retain) UILabel * satLabel;
-(IBAction) changeSaturation:(id) sender;
#end
(in iSaturateViewController.m):
#synthesize satLabel,satSlider;
-(IBAction) changeSaturation:(id)sender {
satLabel.text = [[NSString alloc] initWithFormat:#"Saturation: %d ", (Float32)satSlider.value];
NSLog(#"%f", satSlider.value);
}
Ath the moment the NSLog shows: 0.00000000
Also, I've set the Label in the Identity properties to: satSlider (slider) and satLabel (label).
Just to clarify my goal:
I want the slider's (satSlider) value to show in the UILabel (satLabel) via the function changeSaturation.
How can I do this?
Add the method declaration:
-(IBAction) changeSaturation:(id)sender;
to the header file iSaturateViewController.h
Then, assign the Value Changed action of your UISlider to your changeSaturation method. Right click on the slider and drag the Value Changed to the File's Owner and connect them. It should work.
You can also try this:
-(IBAction)changeSaturation:(id)sender {
UISlider *slider = (UISlider *)sender;
satLabel.text = [NSString stringWithFormat:#"Saturation: %f", slider.value];
NSLog(#"Saturation: %f", slider.value);
}
Regarding UILabel - add IBOutlet and connect it in the Interface Builder. The interface should be like that:
#import <UIKit/UIKit.h>
#interface iSaturateViewController : UIViewController {
IBOutlet UISlider * satSlider;
IBOutlet UILabel * satLabel;
}
#property (nonatomic, retain) IBOutlet UISlider * satSlider;
#property (nonatomic, retain) IBOutlet UILabel * satLabel;
-(IBAction) changeSaturation:(id) sender;
#end
Let me know if it works.
Looks like the key thing you’re missing is that the satSlider outlet isn’t hooked up, so its value is nil. You connected the slider’s action to the -changeSaturation: method, but it sounds like you didn’t also connect the satSlider property to the slider itself. Sending a message (like value in this case) to nil doesn’t do anything, and returns 0, so 0 is the value you’re getting out of it. Hook up your outlet by Ctrl-dragging from the property in the code to the slider in your UI and it should work.
You need to edit your header and add the method declaration. Then in IB, associate the first responder for value changed to the UISlider. Also slider.value, you need to read article: Setting interval value for UISlider

How to send text from text field to another view controller

I'm making an app that behaves something like the default Messages.app in iPhone where a user composes a text message in a UITextField and upon tapping the Send button, the value of the UITextField in ComposeViewController will be transferred to the table cell's UILabel in a custom cell in MasterViewController and also to the DetailViewController where another UILabel will get the text value from the UITextField. The DetailViewController is the ViewController loaded when the user taps the cells from the UITableViewCells.
I actually read related articles below but it doesn't work on my end.
How to send the text from textfield to another class?
How to see text from one text field in another text field?
How to send text field value to another class
Can you please guide me on how to properly implement this? I know it's easy. I just don't know why it's not working. ComposeVC is a ModalViewController while DetailVC is the view that loads when the user taps the cell in the MasterVC's table.
Thanks a lot!
Below is my code for ComposeVC.h:
UITextField *messageTextField;
#property (nonatomic, retain) IBOutlet UITextField *messageTextField;
- (IBAction)buttonPressed:(id)sender;
for ComposeVC.m
synthesize messageTextField;
-(IBAction)buttonPressed:(id)sender
{
DetailVC *detailVC = [[DetailVC alloc] init];
detailVC.messageText = messageTextField.text;
}
for DetailVC.h
NSString *messageText;
#property (nonatomic, copy) NSString *messageText;
for DetailVC.m
#synthesize messageText;
- (void)viewLoad
{
testLabel.text = messageText;
}
testLabel is the UILabel inside my DetailVC.
You can simply create property on another viewController. Suppose your textField is on view1 and you want to send it on view2 then:
in view 2
.h file
#interface view2:UIViewController {
NSString *str;
}
#property (nonatomic, retain) NSString *str;
in .m file
#synthesize str;
Now you can send your text from view1 to view 2 like:
objView2.str = txtField.text;
For viewing one textfield's text in another use
secondTextField.text = firstTextField.text;
Hope this helped let me know if you are looking for something different.
take one variable in the app delegate like in appdelegate.h
#interface appdelegate
{
NSString *str1;
}
#property (nonatomic, retain) NSString *str;
In .m file synthesize it.
set the text after editing the textfield(app del.str=textfield.text i.e setting value).And use the same wherever you want.
NSString *str = appdel.str(getting value);
Try this one:
#interface SecondView:UIViewController
{
NSString *stringSecond;
}
#property(nonatomic, retain) NSString *str;
In First View u have to create an reference like this:
#import "SecondView.h"
SecondView *detailViewController = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
detailViewController.stringSecond = #"Some string";

label and textview not responding to setText:

I have two properties setup properly in Objective-C:
#property (nonatomic, retain) IBOutlet UILabel *label;
#property (nonatomic, retain) IBOutlet UITextView *textView;
I have synthesised them and linked them in the NIB file however when I come to use them like:
if (row==0) {
NSLog(#"First text");
[self.label setText:#"LABEL 1"];
[self.textView setText:#"LABEL 1"];
}
else if (row==1) {
NSLog(#"Second text");
[self.label setText:#"LABEL 2"];
[self.textView setText:#"LABEL 2"];
}
The text is not changing however the NSLog is being called and not the setText: and I was wondering why...
It should be a IBOutlet for you to link them in xib.
Now that you updated your question, the problem may be you have not connected it to your viewcontroller's property. Hence the result. Add more details to your question.
Make sure that Xib and IBOutlet were correct linked.
Make sure that your label and testView are visible. You can set their background color red or other any color different with bg to check their visibility.
Make sure that the line "[self.label setText:#"LABEL 1"];" had ran. you can check the NSLog.
Make sure that the line "[self.label setText:#"LABEL 1"];" was running in main thread.
If your properties are outlets you should declare them as follows:
#property (nonatomic, retain) IBOutlet UILabel *label;
#property (nonatomic, retain) IBOutlet UITextView *textView;
The behaviour you are seeing suggests that you haven't set up the outlet correctly.
FIRST METHOD:
It should be declared as an IBOutlet and you should link it to a label and textView in XIB as Praveen S has said.
SECOND METHOD:
Other option if it is not a label in XIB then you need to allocate and initialize the label to set its text i.e. to access its properties/methods/attributes.
UILabel *label = [[UILabel alloc] init];
[label setText:#"Text"];
label.frame = CGRectMake(0,0,20,20);
Hope this helps.